Ceci est une ancienne révision du document !
In the previous article, we have learnt what is tmux, and how to install and start it. Now it is time to begin the real usage of this amazing tool. We are going to learn how to send commands to the program, how to manage different sessions, and how to organize our workspace with windows and panes.
Command tmux
The tmux is an application framework that can run several virtual terminals inside. As users, we can run several command-line programs inside these virtual terminals. Therefore it is an essential need to be able to send any commands directly to tmux without disturbing the running virtual terminals and programs.
In order to achieve this goal, tmux uses the so-called “command prefix”. Its default value is <Ctrl-b>. Every command that is intended to be sent to tmux, has to be started with this key combination. The exact usage is: • type the prefix combination • release the keys • immediately press the command key.
Let's try it with an example. Start tmux and type the following: <Ctrl-b t> (press “Ctrl-b” then press “t”). You have to see a blue digital clock in the middle of the screen. Pressing “Esc” or “Enter” will eliminate the clock.
From now on, we will use the “Prefix” word instead of “Ctrl-b”. The benefit of this is to avoid any misunderstanding caused by a further reconfiguration of the Prefix key combination.
Session handling
The most powerful feature of tmux is the session handling. In a normal terminal environment, if you start an application, then you exit from the terminal, the already started job will also be terminated. In tmux, if you start a job, and leave the tmux instead of quit (it is called “detach”), the job will still be running in the background (exactly on the tmux server). Later on, you can join again (this is called “attach”) to this session, and you will find the previously started job running.
For session handling, tmux has to be started with the following commands:
$ tmux new-session -s Temp
or
$ tmux new -s Temp
This will create a new session called Temp, and it will automatically attach to it. Almost everything will be the same as in a normal starting of tmux without session handling, except that the name of the session can be read on the first place of the status bar. The picture shows the previously mentioned digital clock in a named session. If -d is used at the end of the new session command, then the session will be created but it will not attach to it automatically.
First of all, run again the clock command <Prefix b>. Now we have an opened session inside tmux. If we were to type the command “exit”, then tmux would terminate this session and we would not be able to attach again. Therefore we have to use the detach command: <Prefix d>. We can see the terminal output:
[detached (from session Temp)]
This message means that we have an opened session. In order to list all of the available sessions, just type the following command:
$ tmux list-session
Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23]
The shortened version can also be used (ls). The result indicates that we have one opened session, named “Temp”, with one window (some additional information can be read as well). Now let’s try to attach to this session again:
$ tmux attach -t Temp
Now we get the same session “Temp” with the already running clock. We can use more sessions as well. Detach from the currently running “Temp” session, start a new one with name “Pmet”, detach from it, and finally get the list of the sessions again:
[detached (from session Temp)] $ tmux new -s Pmet [detached (from session Pmet)] $ tmux ls Temp: 1 windows (created Sat Jun 10 21:38:16 2017) [80×23] Pmet: 1 windows (created Sat Jun 10 21:40:42 2017) [80×23]
If a session is not needed any more, there are two ways to terminate it. Method (1) can be performed inside tmux, just attach to the required session and type the exit command. Method (2) can be used outside of tmux:
$ tmux kill-session -t Temp
After killing all of the sessions, we will get the following output of the list-session command:
no server running on /tmp/tmux-1000/default
That was all about the session handling. Let’s jump to the window handling.
Window handling
Windows inside a session are very similar to the tabs of a text editor or a web browser. Each and every windows are able to run different (or the same) commands. After detaching from a running session, all of the opened windows will keep running the started application in the background.
When we start a new tmux session, the name of the window (the word after the number of the window) will be the currently running application by default, and it is changed every time when we start a new command. Right after starting tmux, it will be the name of our bash. If we start the top application, the window name will be top.
In order to avoid the continuous name change, the window name has to be defined explicitly. This can be done in two ways: (a) at the same time when we start the tmux, (b) inside tmux by a command.
The method (a) is:
$ tmux new -s temp_w -n first
This terminal command starts tmux with a session (-s) named temp_w, and with a window (-n) named first. Now, if we run the top command, the name of the window will remain temp_w.
The method (b) is to run the following command inside tmux: <Prefix ,> (comma). By performing the command, the status bar will be active and the name of the window can be modified.
A brand new window can be created inside tmux with the command <Prefix c>. It has no defined name, but this can be changed by the command mentioned previously.
There is no limitation on the amount of windows in tmux. In the picture, three windows have been created inside session temp_w:
There are several ways to change between the windows. The active window is always marked with * character after the name of the window in the status bar. The – character signs the previously used window. Method (1) is to use the <Prefix n> command to go to the next window. The command will go through the open windows as a circle, so, after the last one, the first one will be the next. Obviously the <Prefix p> command will go to the previous window. All of the windows have a unique identifier - starting with 0. Method (2) is to use the <Prefix w_number> command, where the w_number is the number of the window. Zero-based indexing is familiar to programmers - like array numbering, but later on we will learn how to change it to a one-based index for better usage.
Finally, method (3) is to use the <Prefix w> command. This will list all of the available windows as a visual menu, and the desired one can be chosen with the arrow keys.
In order to close one window, the exit command can be used. This will terminate the active window without any questions. The safer method is to use the <Prefix &> command. This will request a confirmation in the status bar before killing the active window. Terminating the last window will also close the session.
Pane handling
The usage of multiple windows is quite useful, but still not the best way to manage different tasks at the same time. If parallel monitoring of the tasks is important, then panes will be our best friend. Let’s start a new session named temp_p. Any window can be split vertically and/or horizontally. For vertical split, use the <Prefix %> command. For horizontal split, use the <Prefix “> (double quote) command. The window looks like in the picture:
Now, it is easy to open a vim editor in the left pane to edit a source file of an embedded application, and run the compiler in the right top pane, while flashing of the binary can be monitored in the right bottom pane.
Moving between the panes can be performed with two methods. Method (1) is to use the <Prefix o> command, this will go through the panes continuously. Method (2) is to use the <Prefix arrow_keys> command where the arrow_keys can be the Up, Down, Left or Right key.
In order to close one pane, the exit command can be used. This will terminate the pane without any questions. The safer method is to use the <Prefix x> command. This will request a confirmation in the status bar before killing the active pane. Terminating the last pane will also close the active window, and, if it is the last window, then the entire session will be closed as well.
Conclusion
This article covers the most important topics regarding tmux. Now we can use it as our main development environment as programmers, or as the main monitoring environment as server administrators. In the next article, we will learn how to configure tmux via its configuration file. We will change some commands for better usage, and also the visual styling can be modified for pleasant usage. Get Productive! Get tmux!
Command Reference
tmux new-session -s <name> Start a new session with a defined name
tmux new -s <name> (Shorter) Start a new session with a defined name
tmux new -s <name> -n <name> (Shorter) Start a new session and window with a defined name
tmux attach -t <name> Join to an already opened session with a defined name
tmux list-session List all of the opened sessions
tmux ls (Shorter) List all of the opened sessions
tmux kill-session -t <name> Terminate (destroy) an already opened session
Ctrl-B & Command Prefix
Prefix t Show the digital clock in the middle of the screen
Prefix c Create a new window
Prefix , Rename a window in the status bar
Prefix n Go to the next window
Prefix p Go to the previous window
Prefix w_number Go to the w_number window
Prefix w List the opened windows as a visual menu
Prefix & Close the active window with confirmation
Prefix % Split the active window vertically
Prefix “ Split the active window horizontally
Prefix o Go to the next pane
Prefix arrow_key Go to the desired pane
Prefix x Close the active pane with confirmation