Make your life easier by using tmux
This article covers:
- Why to use tmux
- Installing tmux (using an ubuntu machine as an example)
- Example configuration file for tmux
Why use Tmux?
Tmux (short for terminal multiplexer) helps you organise your terminal session by letting you split the console into panes, creating multiple windows and allowing you to detach a session from the shell and then later reattach to the session (all programs in the session continue to run in the background).
The attach/detach function is really useful if you have programs running on a machine in the cloud. If you use ssh to connect to a machine remotely you are probably aware that you can lose the connection to the server at any time, any scripts running on the server at that time will be terminated. By running all your scripts inside a tmux session on the server losing your ssh connection will not terminate your scripts. You can just reconnect to the server using ssh and reattach to your tmux session.
The following commands are for installing tmux on ubuntu. Installing tmux is easy:
sudo apt-get install tmux
Now you can start a tmux session by typing ‘tmux’, but it is better to name each session so we can identify it later when we have multiple sessions. The command is:
tmux new -s octopress
You can list existing sessions by typing:
tmux ls octopress: 1 windows (created Fri Dec 13 12:56:05 2013) [215x64] (attached)
To detach a tmux session you type the prefix and then d. By default the prefix to activate a tmux command is Ctrl-b, so typing Ctrl-b d will detach the session.
To attach to a session that is running in the background the command is:
tmux attach -t octopress
The default prefix for tmux is not ideal, it is difficult to type easily so I prefer to change it to Ctrl-a. You can modify the configuration for tmux by creating a .tmux.conf file in your home directory, here are the settings I use:
tmuxp is a python library that has some shortcuts for using tmux. To install:
pip install tmuxp
Now you can use the following shortcuts:
t ls (0) blog | 4 Windows
To attach to an existing session:
t a (0) blog | 4 Windows Pick session by id:
Type ‘t -help’ to see the other options.