tmux is great, as a web developer I use it all the time. At first I thought "who needs a terminal multiplexer" but after having used tmux for a while now, I can't go without it. If you find yourself switching projects and restarting the same tools over and over again, this post is for you.
What is tmux?
So, tmux is a terminal multiplexer. It means that you can have different terminal sessions (for different projects) and easily switch between them. Even more interesting is that with a plugin, you can even keep those sessions when you restart your computer.
A quick demo:
Installing tmux
To install tmux on OS X you should use homebrew. You can easily install tmux with brew install tmux
. On any other *nix distro you would use the associated package manager respectively.
You can now start a new tmux session by just typing tmux
.
However, I like to start a named session. It's easier to remember and easier to attach to. To start a named session run:
tmux new -s [name]
You can detach from that session by pressing ctrl+b + d
(we'll change these later on). If you want to reattach to that session later you run:
tmux attach -t [name]
Configuring tmux
I personally think ctrl+b + [key]
is horrible. I have it remapped to ctrl+a
and have my Caps Lock key remapped to ctrl
.
To remap Caps Lock to ctrl
on OS X go to System Preferences -> Keyboard -> Modifier Keys and set Caps Lock key to "^ Control".
Your Caps Lock key will behave as a ctrl
key from now on.
Great, this helps a bit but Caps Lock + b
is still horrible. Fire up your editor and create ~/.tmux.conf - this is your tmux config file as you might have guessed.
To change Caps Lock + b
(from now on called ctrl) to Ctrl+a
add this to your ~/.tmux.conf
# use ctrl+a rather than ctrl+b
unbind C-b
set-option -g prefix C-a
bind C-a send-prefix
The changes will take effect next time you start a new session. If you already have a session active you press ctrl+b + :
and type source ~/.tmux.conf
. From that moment on you can use ctrl+a
.
The important shortcuts
As with most tools there are shortcuts. I will list the ones I use all the time below. Prefix means ctrl+a
or whatever key you have set it to. Keep in mind that you can change any key binding yourself in tmux.conf.
- prefix + % - split view vertically
- prefix + " - split view horizontally
- prefix + [arrow keys] select active pane
- prefix + c - create new tab
- prefix + , - rename tab
- prefix + x - kill pane
- prefix + & - kill window
- prefix + p or n - go to previous / next tab
- prefix + [0-9] - go to tab 0 - 9
- prefix + ( or ) - go to previous / next session*
- prefix + d - detach from current session
- prefix + [ - go in copy mode (press q to leave copy mode)
* At the beginning of the post you see me using tmux attach -t [name]
, you can switch much faster by using prefix + ( / )
.
There are of course a lot more shortcuts but I think these are essential. If you want a nice overview of all shortcuts you should check this gist out.
Copying
The first thing I ran into with tmux is that I can't use | pbcopy
(a command to copy output from the terminal). Luckily there's a fix for that.
You need reattach-to-user-namespace. Install it with brew (brew install reattach-to-user-namespace
) and add the following line to ~/.tmux.conf
set-option -g default-command "reattach-to-user-namespace -l zsh"
I personally use vim-like keybindings for copying specific lines. It works like this:
In this demo I am pressing ctrl+a + [, navigating to the lines using hjkl
(vim bindings) and selecting using v
. To copy the actual lines to my clipboard I just press y
like in vim.
The config for this:
# use vim keybindings in copy mode
set-option -g mode-keys vi
# setup 'v' to begin selection like in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
Using the mouse
After showing some of my friends and colleagues tmux that have never used vim, they complained about it. Whilst I believe that hjkl
is a skill for life much like walking, you might prefer to use the mouse to switch between panes and to scroll. If so, add this to your ~/.tmux.conf.
# enable mouse
set-option -g -q mouse on
Plugins, plugins and more plugins
There are a few plugins that make tmux even more awesome. To install plugins you need tpm (tmux package manager).
To install run:
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
This will create a .tmux directory in your home directory where it saves your plugins.
At the beginning of this article I promised that you could restart your computer and keep your terminal sessions, I wasn't lying. The plugin you need is called tmux-resurrect. If you want to automatically save your sessions you need another plugin tmux-continuum and the setting set -g @continuum-restore 'on'
.
To install all these plugins at once add the following to the bottom of your ~/.tmux.conf and press prefix + I (in tmux) to install the plugins.
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# last saved environment is automatically restored when tmux is started.
set -g @continuum-restore 'on'
# initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
If you restart tmux now (tmux kill-server
) you will have tpm and your sessions will be saved. You can restart your computer and run tmux
, it will restore all your sessions, windows and running tools (vim, grunt, gulp, maven etc).
Another plugin that I use frequently is tmux-open. To install add set -g @plugin 'tmux-plugins/tmux-open'
to your ~/.tmux.conf and prefix + I again.
You can open files / urls by selecting the path / url and pressing o (without prefix because you are in select mode). Or edit the highlighted file by pressing ctrl+o.
That's a wrap!
So that's tmux. It takes a few hours to get used to but you will never want to go back to a "normal" terminal. If you liked this post don't hesitate to share it or leave some feedback on twitter.
My personal and minimal ~/.tmux.conf is hosted on GitHub as a gist.