Sometimes I think I spend too much time optimising my work environment. I just can’t help it.
With tmux, I think it was a good investment of time. I use it all day long every day. Also, according to xkcd, if it saves me 30 seconds at least 3 times a day, it was a good investment.
What is tmux
I use Tmux to manage terminal sessions. It allows me to do stuff like this
btw, that prompt is folder name (in blue) and a dagger: † (U+2020)
Why do I use it
It allows you to have multiple sessions that can have multiple windows (tabs) that can have multiple panes (split).
How do I use it
I use sessions for different work contexts. For example, if I’m working on a personal project at home, I might have two sessions (you can name them), tabs of work in one session, tabs of a specific personal project on another session.
I use different windows (really, they are tabs, but tmux calls them windows) to separate what I want the terminal to show. For example, one might be a series of split panes with server log outputs or “
tail -f”s of a logfile and others a main pane with a seperate “
watch ...” command.
It may take a while to get used to tmux. I’ve been using it daily for +1 year and now and then I learn new stuff about it.
Initially the keyboard shortcuts may seem silly, but once you realise that every shortcut starts with a prefix (so it doesn’t conflict with apps that use the keyboard like Vim) and that you can change said prefix, then everything gets much much simpler.
The default prefix is
C-b (that is Ctrl+b). But you can change it in the configuration file (
e.g. change it to Ctrl+a:
# remove the default binding to the prefix unbind-key C-b # set a new one set-option -g prefix C-a # in case some program needs Ctrl+a, # hitting Ctrl+a twice will send it to the program bind-key C-a send-prefix
Now that you know what the prefix is, you can learn some key bindings like:
<prefix>+"- splits the current pane in two horizontal tabs
<prefix>+%- splits the current pane in two vertical tabs
<prefix>+<direction_keys>- move the focus to another panel
<prefix>+c- creates a new window
<prefix>+n- moves to the next window
<prefix>+p- moves to the previous window
You can learn more shortcuts if you check the manual
man tmux or with the shortcut that shows all key bindings in tmux:
After installing it, you can start tmux at any time from the terminal:
The tmux cli accepts commands, the default is
For me, this is not enough. I always want to be tmuxing. So I configured my terminal application (on Ubuntu I used gnome-terminal and on OSX I use iTerm2) to run this command at startup:
tmux && exit
This command runs tmux, and when tmux exits, it also terminates the terminal session. In most terminal applications you can configure it to automatically close a session when it ends, so this is pretty useful.
But nowdays my actual configuration is this:
(tmux attach || tmux) && exit
Tmux uses a background server to run the sessions and the CLI is just a client to it. Sessions don’t get destroyed if you close the client. So you can do
tmux attach to restore your session. In fact, if you open two terminals, type
tmux in one of them and
tmux attach in the other one, they will mirror eachother.
If I accidentally close the terminal app, there is no problem. The terminal will run this command, that will first try to attach to an existing session, and if there is none, create a new one.
What if I need a plain terminal?
I have this configuration for my default terminal profile, and I have another profile that doesn’t start tmux. In truth, I find that I only use it when I ssh into a machine and I want to run tmux on the other side.
Another, quicker alternative is to open a new window, that, with this configuration will start by mirroring you current session, and the following keybinding:
<prefix>+Ctrl+Z,Ctrl+Z this will put the current tmux in background.
Scrolling and finding text
You can scroll with
<prefix>+Ggup, this will put tmux in a selection mode, then just hit PgUp/PgDown or Ctrl+up/down to move, to exit the selection mode hit either esc or q.
This config will make the selection mode behave a bit like Vim.
# Vi copypaste mode set-window-option -g mode-keys vi bind-key -t vi-copy 'v' begin-selection bind-key -t vi-copy 'y' copy-selection # hjkl pane traversal bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R # Setup 'v' to begin selection as in Vim bind-key -t vi-copy v begin-selection
On OS X
After one year on OS X, I moved to Ubuntu and loved it. I sticked with it for about 7 months but then the glorious 12h-battery-powered 13” MBA hardware made move back to OS X.
After using tmux on Ubuntu for some time, I got a bit frustrated with some quircks when I moved to OS X.
The default Terminal OS X app globs up the some of the key bindings, like scrolling.
Solution: Install and use iTerm2.
Solution: Use brew to install reattach-to-user-namespace:
$ brew install reattach-to-user-namespace
and then prepend to your
# make pbcopy and pbpaste work set-option -g default-command "tmux rename-window bash; reattach-to-user-namespace -l bash" bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
Put that mouse to work
You can get the mouse to work on other systems. I just haven’t tried it yet. I have been using tmux without a mouse for a long time. You don’t need it, but having the option is nice.
Put this in the
set-option -g mouse-select-pane on set-option -g mouse-select-window on set-window-option -g mode-mouse on
Then you should be able to select panes and windows with the mouse. If you have trouble selecting text for copying, just hold down the ⌥ option key (alt).
If you want, you can take a look at my messy tmux.conf.
That is all!