Introduction
Tmux is a terminal multiplexer. It allows you to run and manage several command prompts simultaneously from one tmux session. It is the equivalent of a graphical window manager, for command prompts.
Tmux uses a client/server model which allows it to persist connections. This means that you can start a session from one computer (say your work computer), have several programs running on it, leave work and connect back to the same session from a different computer (say your home computer). When reconnecting to your session, you will continue where you left – the same programs will be running.
Installation
On Debian/Ubuntu systems use:
sudo apt-get update && sudo apt-get -y install tmux
On Redhat/CentOS systems use:
sudo yum install update && sudo yum -y install tmux
Note: “-y” in both cases above auto answers “yes” during installs. Above commands were tested on Ubuntu 12,14 & 16 and CentOS 6 & 7.
Terminology
Pane – A pane is simply a terminal prompt.
Window – A window holds multiple panes (terminals) on one screen.
Session – A session has multiple windows. Sessions are similar to applications that create virtual/multiple desktop workspaces like GNOME on Linux and VirtualWin on Windows.
Usage
Start a new tmux session
tmux new -s start
You will see a bar at the bottom of the screen with the session name “start” in brackets. What just happened, was that tmux started a new session named “start” that contained a default window holding one pane (terminal). Upon creating a pane, tmux automatically logs you in with your user account.
Maneuvering in tmux
Once inside a tmux session, you use a prefix key to trigger commands to tell tmux what to do. The default prefix key is CTRL + b. For example, if you want to tell tmux to create a new pane by splitting your screen into two vertical sections, you first hit CTRL + b, then %.
Create Multiple Panes
If you already keyed in CTRL + b, then % above, then you will see that you already have two panes on your screen.
Split the current pane into two horizontal sections by keying CTRL + b, then “.
To rotate/cycle through all three panes use CTRL + b, then o.
Create Multiple Windows
Lets create a new window inside of the same “start” session that we are already in. Hit CTRL + b, then c. You will see a new blank terminal.
Split this terminal into two horizontal panes – CTRL + b, then “.
Create a third window CTRL + b, then c. You will see a new blank terminal again. Issue the command tmux list-windows
, then press ENTER to confirm that you have 3 windows opened.
Use CTRL + b, then n to cycle between the three windows created.
Attach and Detach to/from a Session
Use CTRL + b, then d to detach from your current session (this should be the “start” session created earlier).
Reconnect back to start session using tmux attach -t start
. Use CTRL + b, then n to cycle between the three windows in start session.
When at another location (or on a different machine), SSH into your server. After logging in, issue the tmux attach -t start
command. You will see that it connects you to the start session with all three windows running intact.
Conclusion
As already stated, tmux is the equivalent of a graphical windows manager, but for command prompts. It is sure to boost your productivity significantly, and is comparable to tabs for browsers.
You can take things further by using configuration tools for tmux like teamocil and tmuxifier to define better workflows.
Some Useful Commands
Common key bindings
CTRL + b, then c – Create new window.
CTRL + b, then , – Rename window.
CTRL + b, then n – Move to the next window.
CTRL + b, then p – Move to the previous window.
CTRL + b, then & – Kill current window.
CTRL + b, then % – Split current pane into two (vertically).
CTRL + b, then “ – Split current pane into two (horizontally).
CTRL + b, then o – Switch to next pane.
CTRL + b, then q – Show pane numbers (then type a # to switch to it).
CTRL + b, then d – Detach from current session.
CTRL + b, then ? – List all key bindings.
Common session commands
tmux list-sessions
– List existing tmux sessions.
tmux new -s session-name
– Create a new tmux session named session-name.
tmux attach -t session-name
– Connect to an existing tmux session named session-name.
tmux switch -t session-name
– Switches to an existing tmux session named session-name.