As a sysadmin, most of my time is spent working on remote machines and different task. tmux is a terminal multiplexer, meaning it allows you to run multiple terminals in the same windows.
This tutorial will explain the basics features of tmux that should help you be more productive with your every day task.
If you do now have tmux installed yet, well, it is time to fire up a terminal and get it installed:
# apt-get install tmux
Just like screen, tmux has the great feature of being able to detach the sessions, this means that you can start a session on a remote machine from your office, do your work there during the day, detach the session and go how and finally, the day after re-attach to your session and have everything set up just like you left the office the previous day.
So, let’s start with handling sessions first
If you plan to only run a single session on your session, it will be enough to just run:
This should give you a session called  and is nothing more than shell.
If you want to exit that process, you can exit like you would do with a normal command prompt, and that would kill the session, or you can type Ctrl-b d and you will detach the session.
The next time you want to attach the session, you will run
$ tmux attach
and that will get you back to it.
Nice, we can now create a session, do some stuff in it, detach it and re-attach it later.
Let’s get a step further and handle multiple session, but before that I think it is worth understanding the jargon used in tmux lingua.
Tmux can create multiple session, each session is composed of windows, which can then be split vertically and horizontally.
In the picture, you can see that there is 2 sessions (session1 and session2) which respectively contain s1win1, s1win2 for the former and s2win1, s2win2 for the latter.
The window which has the focus is s2win1 in session2. The active session is mentionned between square brackets [session2] while the active window is marked with an asterisk s2win1.
Managing Sessions and Windows
Here are a few commands that we will be using:
- Ctrl-b c create a new window
- Ctrl-b , rename window
- Ctrl b n next window
- Ctrl b p previous window
- Ctrl-b : command prompt
- Ctrl-b w choose window
- Ctrl-b s choose tree
- Ctrl-b $ rename session
- Ctrl-b ? help
Now, let’s create some sessions and windows and play a bit with them to get a better understanding of what can be done.
First, we will create a first session called session1 which will have its first window called s1win1:
$ tmux new -s session1 -n s1win1
then, we will create a second session from within the running tmux client instance we are using and will call the sesion session2 with its first window called s2win1. Type Ctrl-b :, this gives you a prompt. now type:
new -s session2 -n s2win1
You should now have created a new session with a new window and this should be your active window.
Let create a new window within that session:
and rename it to s2win2:
At the prompt, change the name of the window to s2win2 and hit enter.
Good, we now have 2 sessions with some window in them. Let’s detach from it and play a bit more with it.
now, from the shell prompt, we can list the sessions with:
$ tmux ls
and re-attach session 1 using:
$ tmux a -t session1
Now, lets rename session1 to myfirstsession:
and type the the name of the session.
Now, let get back to session2 s2win2 by typing:
And using the left-right arrows to fold/unfold the sessions’windows list and up-down to move around.
Finally, once the cursor is on (4) └─> 1: s2win2* hit enter.
To go back to s2win1, type:
To switch back to s2win2, or we could have used
to move between windows.
Finally, get close all this up and press
twice, this should tell you that the session [exited].
Let’s check the sessions left:
$ tmux ls myfirstsession: 1 windows (created Fri Dec 7 00:00:42 2012) [238x56]
Let’s attach to it:
$ tmux a
and close it
That’s it! we have created multiple sessions and windows, moved around, rename things and finally cleaned everything. There is much more that can be done with tmux but that will be it for this post, and hopefully, more to come soon!
In the meantime, dont forget to use Ctrl-b ? within tmux to access the help along with man tmux for even more help!