Linux Tmux Tutorial With Command Examples
Tmux is terminal multiplexer popular in Linux world. Tmux provides multiple windows in a single session. Tmux also have the ability to detach and run after disconnect and attach after connection. This is very useful for system administrators.
Man page of tmux provides a lot of detailed features. To get more information man page of tmux can be used like below.
$ man tmux
Tmux is a terminal emulator acts like a shell. Tmux can be started like a shell too. Just run
tmux command we will get tmux screen by clearing existing shell screen.
Tmux uses CTRL+b for operation key which means to use shortcuts of tmux CTRL+b should be prefixed with related keyboard shortcut
List Current Key Bindings
Tmux mainly used with keys with bind keys. These key bindings can be listed with following command. As we can see from following screenshot there are a lot of key bindings provided by tmux.
Start New Window
One of the most useful functionality of the tmux is operating multiple windows. Existing windows are listed in the green bar below. Creating new window will create new shell like a new tab in terminal emulators.
Move Next Window
While working with multiple windows navigation between windows can be done in different ways. One of the most common usage is switching next window with following key short cut.
After switching the asterisk which shows the current desktop will put new window afterward.
Move Previous Window
This is similar to to moving next window. Just type following keyboard shortcut
Move Window With Number
Another useful navigation feature is using window number to navigate. Window numbers can be seen below of the terminal before the window name.
CTRL+b <number of window>
Normally tmux sets current running application name as window name. If we are using a lot of windows for multiple operations and we want to rename the window name.
Pane are used to multiplex single window. Using multiple commands in a single window is very useful and popular usage. Below we can see two horizontal pane. We will look how to create panes below.
Create New Vertical Pane
Current window can be divided in to two vertical pane with
After creating pane current working pane will change to new pane and active pane can be seen the line between panes. The lower part of the line is yellow which means left pane is active.
New Horizontal Pane
Horizontal pane can be created with
Move and Navigate Between Panes
To move between panes direction keys can be used like below.
Move Left Pane
CTRL+b <left arrow>
Move Right Pane
CTRL+b <right arrow>
Move Below Pane
CTRL+b <down arrow>
Move Upper Pane
CTRL+b <up arrow>
Show Pane Numbers
While working multiple pane it can be useful to show pane numbers. Pane numbers can be shown according to their location with command below. While showing pane number in the upper left corner of the pane the size is also shown.
If we have finished our job with a pane we generally close it to make things clean. We can close current pane with the following shortcut.
One of the most important feature is copying and pasting text from terminal. Tmux provides a bit different method to copy paste. Normally console provided copy paste can be used but if source is multiline text it can be impossible to copy properly. Below is the steps to copy text.
Set Start Point
We can move cursor to the start of the copy range in the copy mode and after that to set starting point following command is used.
Set End Point
Move cursor and we see can see the current selection is in orange color. To set end point and copy the contents to the buffer use following command.
Copied text will be put into the buffer. We can use these text wherever we want. We can also access this text in tmux from different windows.
Creating a tmux session is just starting tmux. But we can also provide some parameters like name.
$ tmux new -s mysession
There may be more than one tmux sessions. This sessions can be listed with the following command
Existing session can be detached like below. This will not end the session thus this will only detach session from current shell and it will continue running in the background.
Attaching session means reconnecting currently running session which is detached before.
$ tmux a
There is to path where configuration file resides.