
A minimalist guide to tmux - peterxjang
https://medium.com/@peterxjang/a-minimalist-guide-to-tmux-13675fb160fa
======
sls
Best thing I ever did for tmux was change the command key prefix to "C-\".
With caps lock mapped to control, this makes tmux commands available with
almost no hand motion, just a slight reaching out with the pinkies. It also
restores access to C-b for "back one character" in emacs and all programs
using emacs key bindings, which is hardwired into my fingers at this point.
(You're blocking emacs' function "toggle-input-method" \- "Enable or disable
multilingual text input method for the current buffer". For some users that
would probably be an issue, but hasn't been for me.)

Another useful thing is to "bind-key C-\ last-window". That makes C-\ C-\ jump
back to the last window you visited, which facilitates creating short-lived
windows for specific tasks.

Final small tweak I like is "set -g base-index 1" so that C-\ 1 is the first
window, 2 is the next, etc. instead of having the first window way over on 0.

My other tweaks are probably more idiosyncratic to my workflow.

~~~
nandkeypull
Best thing I ever did for tmux was to get rid of the annoying command prefix
altogether and just use bindings with Ctrl/Shift and the arrow keys - it's
much faster, e.g.

    
    
        # Window switching
        bind -n S-up    new-window
        bind -n S-down  confirm-before -p "kill-window #W? (y/n)" kill-window
        bind -n S-left  prev
        bind -n S-right next
    
        # Pane switching
        bind -n C-left  select-pane -L
        bind -n C-right select-pane -R
        bind -n C-down  select-pane -D
        bind -n C-up    select-pane -U
    
        # Special pane operations
        bind -n C-M-up    respawn-pane -k
        bind -n C-M-down  kill-pane
        bind -n C-M-left  swap-pane -U
        bind -n C-M-right swap-pane -D
      
        # Window splitting
        bind -n C-M-\ split-window -h
        bind -n C-M-] split-window -v

~~~
xfalcox
Have you ever heard about byobu? It's basically a tmux wrapper with crtl/shift
hotkeys by default.

------
zhann_dc
If you use your terminal all day, do yourself a favor and learn readline
shortcuts [1]. Once you have you might stop overwriting these, because imho
C-a is a terrible prefix and very handy to move to the beginning of a line.

[1]
[http://www.bigsmoke.us/readline/shortcuts](http://www.bigsmoke.us/readline/shortcuts)

~~~
justsid
This is why I love macOS so much! With the command key, there is no overlap
for things like copy and paste and ctrl + c/v. Also Cocoa implements things
like ctrl + a/e etc for every native textfield and textview, so I‘ve gotten
really used to it.

Too bad now I‘m mostly on Windows and miss all these shortcuts. In programs
where I can edit the keymapping, I usually change ctrl + a from Select All to
jump to beginning of line. I barely use Select All anyway.

~~~
Graziano_M
I use a mac, but sometimes have to use office tools. They don't use readline
shortcuts and it drives me absolutely mad when I hit Ctrl-H (backspace) and
the help window pops up.

------
rmetzler
Add mosh [0] to connect to the server and tmux on the server and you can close
your laptop lid, travel, open it again and your processes still run on the
server and you're connected.

[0] [https://mosh.org/](https://mosh.org/)

~~~
mhd
Yeah, if only I could have the same thing for ssh tunnels, I'd be in suspended
heaven.

~~~
nirv
There's an open bug bounty[1] for that feature[2]. Just in case.

[1] [https://www.bountysource.com/issues/4471419-ssh-port-
forward...](https://www.bountysource.com/issues/4471419-ssh-port-forwarding-
doesn-t-work)

[2] [https://github.com/mobile-
shell/mosh/issues/337](https://github.com/mobile-shell/mosh/issues/337)

------
NelsonMinar
Somehow I've never made the jump from screen to tmux. 20+ year old habits die
hard. This guide is nice but doesn't really show anything you couldn't do with
screen too.

~~~
aorth
I used GNU screen for ten years or so and then switched to tmux in 2013. My
tmux config to mimics screen's in most places, (prefix key, copy mode, etc).
As others have pointed out, tmux has a newer code base, support for newer
terminal features, less security vulnerabilities, etc. I've never looked back.

[https://github.com/alanorth/dotfiles/blob/master/.tmux.conf](https://github.com/alanorth/dotfiles/blob/master/.tmux.conf)

~~~
marktangotango
I’ve never found a solution for this, so I’ll ask here; I always have a
problem where vim in a screen session fails to display syntax highlihting, and
TERM is xterm in both, any idea? Specifically I’ll ssh to a remote and vim an
Apache config is highlighted, start screen, vim the same file and no
highlighting.

~~~
tfmatt
Two things to try after opening a new screen window:

1) Enter "bash" then vim file.java (some file) 2) Enter :syntax on

------
riazrizvi
Also the all important ‘C-b D’ for when your tmux session is frozen. That’s
upper case d. When the wireless connection on your machine changes, tmux
freezes. Attempting to detach other clients with this command, immediately
restores responsiveness.

~~~
elldoubleyew
Great tip. Did not know this command, I've been killing my terminal and
opening a new one to reattach for the last two years.

------
joatmon-snoo
Question: what do people use as their prefix?

I started using tmux last week, and although it's _mostly_ been a good
experience, I still haven't been able to settle on a good prefix. I find C-a
to be a terrible choice because I actually use that for command line
navigation, and so in the meantime I've tried settling on C-q, but it just
feels awkward at times.

(Note that I've remapped Caps Lock to Ctrl.)

~~~
LeapingLennie
I use C-Space. Easy to type and doesn’t interfere with any readline or vim
keybindings.

~~~
psynapse
Same here. I also use space as my Vim leader, so it feels natural.

~~~
biggerfisch
Same here to both. Only real conflict is with dunst [0] which also likes to
use Ctrl-Space by default.

[0] [https://github.com/dunst-project/dunst](https://github.com/dunst-
project/dunst)

------
softinio
i use iterm2 and neovim. Is it worth using/adding tmux for local development?

As Iterm2 has split panels and some level of saving your session and neovim
built in termnial I feel tmux may not be needed for local development or am I
missing something?

~~~
crispinb
I use tmux for doing stuff on servers, but rarely bother locally. The main
reason I can think of to do so is if you routinely use a specific multiple
shell setup for some particular project —- eg. a tail, an editor and a ready
prompt. Otherwise I don’t see much point (though it’s not an issue I’ve
explored much).

Btw as you’re an iTerm user you might want to read up on its tmux integration,
which is quite cute.

~~~
jfoutz
for a little while i used

    
    
        #!/bin/bash
    
        tmux split-pane -b -p 30 -d;
        tmux send-keys -t {up-of} 'nyancat^M'
        "$@"
        tmux send-keys -t {up-of} '^C'
        tmux kill-pane -t {up-of}
    

to pop up an obvious animation for longer running processes. like vm startup.
When the animation disappears the thing is ready to go. Haven't been bothering
with tmux local lately though.

~~~
crispinb
`nyancat` (as a shell command) puzzled me briefly. Now I see. Not available on
macOS, except for unfortunates with a new macbook 'appointed' with a fake
keyboard:
[https://github.com/avatsaev/touchbar_nyancat](https://github.com/avatsaev/touchbar_nyancat)

~~~
jfoutz
Brew has a terminal animation. It’s cute.

[http://brewformulas.org/Nyancat](http://brewformulas.org/Nyancat)

------
Bromskloss
> Keeps windows and panes in a session (which stays alive even when the
> internet disconnects)

Does that mean that tmux eliminates the need for mosh [0]?

[0]
[https://en.wikipedia.org/wiki/Mosh_(software)](https://en.wikipedia.org/wiki/Mosh_\(software\))

~~~
zbentley
No. Mosh handles automatic reconnection (and a bunch of other stuff, like
predictive typing so you don't notice flickering or slow connections, and
performance improvements); tmux runs a persistent process on the remote side
that preserves the state of your sessions.

Using mosh + tmux allows you to have both a) state persisted on the remote
side, and b) automatic reconnects if your internet drops, or if you change
connections. It's a very good combination, if you can handle some of the
drawbacks of mosh (no in-terminal scrolling, mostly, but you can outsource
that to tmux as well at the cost of a little lag).

A good additional piece of the puzzle is tmux-resurrect [1], which saves and
automatically restores a lot of your tmux state to the filesystem so it's
proof against power loss and system restarts/relogins. It persists window
arrangements and similar, though it obviously can't save everything--the state
of programs that were open can't be programmatically preserved (though that's
getting closer and closer to seamless with the advent of things like CRIU).

The combination of mosh + tmux + tmux-resurrect is a very popular one and
creates a very resilient system for "don't make me think; just log me in to my
usual environment"-type systems, or environments where internet connectivity
is spotty or the tmux server environment is unreliable or prone to rebooting.

[1] [https://github.com/tmux-plugins/tmux-resurrect](https://github.com/tmux-
plugins/tmux-resurrect)

------
neals
I read a lot of you are really customizing the configuration. How do you
manage that? I work on dozens of servers and spawn and destroy a few
everyweek. Do you run some kind of sync tool to get your personal config up
and running? Where do you store that?

~~~
tga
Take a look here for several good options:
[https://news.ycombinator.com/item?id=15196141](https://news.ycombinator.com/item?id=15196141).

------
girishso
if only tmux could scroll through the screen using mouse. Keyboard scrolling
is a big no for me.

~~~
TechHermit
`set -g mouse on` should be all that's necessary to enable mouse-wheel
scrolling for tmux version 2.1 and up. This works for me.

[https://superuser.com/questions/209437/how-do-i-scroll-in-
tm...](https://superuser.com/questions/209437/how-do-i-scroll-in-tmux)

~~~
supermdguy
Wow, thanks! I've been using tmux for about a month now to connect to my
server, and I never knew this was possible.

~~~
misnome
Just to add a note that I updated recently and the mousewheel scrolling
behaviour stopped and sent up/down terminal history commands instead. If this
happens, there is a thread on the tmux github issues with some workarounds.

I’m pretty new to using tmux as more than a basic screen replacement, but it
seems flexible enough that almost anything you want can be configured.

------
cf
What are people doing to cycle through windows on nested tmuxes? I often hide
the outer tmux captures keybindings before the inner one can see them.

~~~
sls
As I noted above, the tmux command send-prefix will send the command prefix
character to the process tmux is managing. So if you open a connection to a
remote system and run/connect to a tmux session there, and you have the same
command prefix for both systems, you can <command prefix>:send-prefix<enter>
to e.g. disconnect and reconnect from a shell that's not running tmux. If
you're going to be doing this on purpose and fairly often, you might consider
binding a key for send-prefix.

I would prefer not to have to remember which tmux I was talking to all the
time and so I would just connect from a normal shell, but ymmv of course.

------
mxuribe
Nice guide; short and to the point!

------
ekianjo
This article a useless. It's like telling you that you can live with knowing
0.1% of Tmux's functionalities. Does not even tell you how to create panes,
resize them, move them around... which should be in the basic stuff you
describe to any user.

~~~
Cyberdog
It's a start. That 0.1% will be enough or some people, and once they get that
much down, learning about the other functionality will be simple should they
desire to seek it out.

~~~
ekianjo
It would not have taken much more time to explain panes at least. This just
sounds like a lazy article.

