Tmux is a terminal multiplexer, or in other words, you can start several terminal sessions in one terminal and switch between them or even place them in one window, and also disconnect from the session leaving it to work in the background, and then return and see the result, it can be very convenient when working on SSH. If you used the screen utility before, then tmux is its analogue, which has many more functions.
This article is a small Tmux cheat sheet in which we will analyze the installation of the program, its main features and commands, as well as some usage tricks. After reading the article, you will be able to fully use the utility.
The content of the article:
Install Tmux
Everything is simple here. To install the program on Ubuntu, run the following command:
sudo apt install tmux
And for installation on CentOS:
sudo yum install tmux
Tmux Syntax and Options
The command syntax is extremely simple; to create a new session, just run the utility without parameters. To connect to an existing one, you will have to use additional options. Here is the basic syntax:
tmux team options
As for options, in most cases you will have to use only two – this is -t (target) – which indicates the purpose to which the command will be applied, for example, the identifier of the session or connected client, as well as -s (session) – which is usually used for designation of the source over which the action will be performed.
Tmux commands for managing sessions that you can use:
- attach-session (attach) – connect to an existing session. In the parameter, you must pass the -t option and the session identifier;
- detach-session (detach) – disconnect all clients (or transferred using the -t option) from the session passed to the -s option;
- has-session – check if the session exists, similarly, you must pass the session identifier;
- kill-server – stop all running sessions;
- kill-session – end the session passed in the -t parameter;
- list-clients – see clients connected to the -t session;
- list-sessions (ls) – list all running sessions;
- new-session – create a new session, you can transfer the session name to the -s options and the start directory to the -c option;
- rename-session – rename the session, you need to transfer the session identifier and a new name;
Of course, tmux has a lot more commands, but we will not discuss them in this article. Basically, the commands will be used to connect and disconnect from sessions, all other actions are performed using hot keys and they are also enough here.
By default, to activate the keyboard shortcut, press Ctrl + B, release, and then press the desired key. Here are the basic tmux keyboard shortcuts you'll need:
- Ctrl + b c – create a new window;
- Ctrl + b w – select a window from the list;
- Ctrl + b 0-9 – open the window by its number;
- Ctrl + b – rename the current window;
- Ctrl + b% – split the current panel horizontally;
- Ctrl + b " – split the current panel vertically;
- Ctrl + b arrow – go to the panel located on the side where the arrow points;
- Ctrl + b Ctrl + arrow – change the size of the current panel;
- Ctrl + b o – go to the next panel;
- Ctrl + b; – switch between the current and previous panel;
- Ctrl + b x – close the current panel;
- Ctrl + b ( – enter the copy mode (more details below);
- Ctrl + b) – paste from the internal clipboard tmux;
- Ctrl + b d – disconnect from the current session;
- Ctrl + b: – open the command line.
These hot keys will be enough to get started. The tmux cheat sheet is finished, now let's move on to examples of working with the program.
How to use tmux
1. Creating a session
To create a new session, just run the command without parameters:
tmux
You can also create a session with the new-session command and give it a convenient name:
tmux new-session -s losst
2. Splitting a window on a panel
The session window can be divided into several panels. To split horizontally, click Ctrl + b release and then press Shift + ":
Similarly, you can divide the panel vertically by clicking Ctrl + b, and then Shift +%:
The size of the active panel can be changed by clicking Ctrl + b, and then Ctrl and arrow:
And to switch between panels use the keys Ctrl + b and arrow. At first not familiar, but then convenient.
3. Mouse support
Mouse support is disabled by default, but if you work in a graphical environment, you can enable it. This will give several advantages: you can select the active panel with the mouse, copy text to the internal clipboard, change the size of the panels, use scroll to view the output history, and so on. To do this, open the ~ / .tmux.conf file and add the following lines:
vi ~/.tmux.conf
Save changes, click Ctrl + b:, and then run this command to restart tmux:
Configuring tmux is complete, the mouse will now work.
4. Copy and paste
One of the rather important operations when working with the terminal is the ability to copy something and transfer it somewhere. After activating the mouse support, you can simply select a section of text with the mouse and it will automatically be copied to the tmux internal buffer.
If you want to be able to copy to the system clipboard, before you perform actions with the mouse, hold Shift:
In addition, tmux has copy and paste control using keys. To switch to copy mode, press Ctrl + B (, then move the cursor to the desired location using the arrows. By the way, this mode can also be used for scrolling. Then click Ctrl + Space to start the selection, select the desired fragment with the arrows and press Ctrl + w for copying.
To paste text from the internal clipboard, press the key combination Ctrl + b). To exit copy mode, use q or Esc.
5. Windows tmux
In addition to panels in tmux, you can open windows. This is an analogue of desktops in Linux. Each window is tied to a specific digital button and has its own set of panels. To create a new window, click Ctrl + b c:
A list of windows is displayed at the bottom of the terminal window. To switch to the desired window, press Ctrl + b and window number.
6. Disconnect from a session
To disconnect from the current session, just press the key combination Ctrl + b d.
After disconnection, all the programs launched in the session will continue to work and you can always reconnect and see their output. This is convenient when connecting via SSH, which can always break.
7. Connection to the session
You can view the list of sessions with the ls command:
tmux ls
And to connect, use attach:
tmux attach -t losst
You can also see a list of clients connected to the session:
tmux lc
findings
In this article, we have covered the basic tmux commands, as well as how to use this utility. I discovered tmux not long ago. I heard about the program for a long time, but my hands did not reach in any way, and then I tried and can no longer do without it.
Comments