This post introduces tmux, a terminal emulator similar to GNU screen.
What is tmux?
Basically, tmux allows one to split a terminal window up into smaller panes, create new terminal windows, and detach and reattach windows (and probably more). This would be a very useful tool, like GNU screen, if one were working on a server, and did not have access to a more advanced terminal emulator such as iTerm.
To install tmux manually, see this post. Alternatively, assuming you are running OSX and
brew is installed, in a terminal simply use:
brew install tmux
Starting a tmux session
To start a tmux session, in a terminal simply the command:
The window will change appearance, because you will be in a tmux session.
The Prefix key
While in a tmux session, all tmux commands starting with a
prefix key, which by default is
Ctrl b (hold the
Ctrl key and press the b key; such a combination is usually abbreviated to
This default can be changed using a
tmux.conf file (see below).
A list of useful tmux shortcuts can be found in this post.
Detach a tmux session
To exit a tmux session (without killing it), while inside tmux use the shortcut:
When using the default prefix key: hold the
Ctrl key, press the
b key (the prefix), then press
This is known as 'detaching' a session.
Attach a tmux session
After detaching a session, if you want to reattach it, first see a list of the existing tmux sessions using:
which will show a list of the existing tmux sessions, e.g.
0: 3 windows (created Thu Apr 7 16:27:36 2016) [162x36]
5: 1 windows (created Sat Apr 9 20:31:00 2016) [162x36]
This example shows to sessions labelled
5. To attach session
tmux a -t 0
which is a shorthand for
tmux attach -t 0
Kill a tmux session
In order to kill a tmux session, first detach it (
Prefix d) and see a list of existing sessions using
tmux ls. Suppose you want to kill the session labelled
0. On the command line, use:
tmux kill-session -t 0
Creating a tmux conf file
To customise the appearance of tmux, and change the default keyboard shortcuts, one can create/edit the file
~/.tmux.conf. This will work whether you use your personal computer or a server (with a personal home directory).
Since there are many options that can be changed/specified in a
~/.tmux.conf file, this is discussed in another post in detail.
~/.tmux.conf file I use can be downloaded here.
Saving tmux sessions
Sometimes your computer may need to be restarted. However, a restart will kill any open or detached tmux sessions. An enterprising developer created tmux-resurrect to allow tmux sessions to survive a reboot by 'saving' them.
Following the instructions on the GitHub page, clone the repo to some location (e.g.
git clone https://github.com/tmux-plugins/tmux-resurrect ~/path/to/resurrect
Then, place the following line at the end of the
Finally, in order to have the pane contents restored, add the following line to the
set -g @resurrect-capture-pane-contents 'on'
See this post for more details on this last step.
That's it! It is ready to use.
While in a tmux session, use the following shortcuts:
||save current tmux session|
||reload saved tmux session|
How it works
It saves files to:
and then reloads the data from there.