How to use tmux

Keywords: Linux

How to use tmux

1. The role of tmux

(1)It allows simultaneous access to multiple sessions in a single window. This is useful for running multiple command-line programs at the same time.

(2) It allows new windows"Access"Session already exists.

(3)It allows multiple connection windows per session, so multiple people can share the session in real time.

(4)It also supports arbitrary vertical and horizontal splitting of windows.

2. Basic use

2.1 installation

# Ubuntu or Debian
$ sudo apt-get install tmux

# CentOS or Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

2.2 start and exit

After the installation, type the Tmux command to enter the Tmux window

$ tmux
#The above command will start the Tmux window with a status bar at the bottom. The left side of the status bar is the window information (number and name) and the right side is the system information.
$ exit
#Press Ctrl+d or explicitly enter the exit command to exit the Tmux window.

2.3 prefix key

The Tmux window has a large number of shortcut keys. All shortcut keys should be recalled through prefix keys. The default prefix key is Ctrl+b, that is, press Ctrl+b first before the shortcut key takes effect.

For example, the shortcut key for the help command is Ctrl+b?. In the Tmux window, press Ctrl+b and then?, The help message is displayed.

Then, press ESC or q to exit help.

3. Session management

3.1 new session

The first started Tmux window is numbered 0, the second window is numbered 1, and so on. The sessions corresponding to these windows are session 0 and session 1.

Using numbers to distinguish sessions is not intuitive. A better way is to name the session.

$ tmux new -s <session-name>

The above command creates a new session with the specified name.

3.2 detach session

In the Tmux window, press Ctrl+b d or enter the tmux detach command to separate the current session from the window.

$ tmux detach

After the above command is executed, the current Tmux window will exit, but the session and the processes inside are still running in the background.

The tmux ls command can view all current Tmux sessions.

$ tmux ls
# or
$ tmux list-session

3.3 access session

The tmux attach command is used to reconnect to an existing session.

# Use session number
$ tmux attach -t 0

# Use session name
$ tmux attach -t <session-name>

3.4 kill session

The TMUX kill session command is used to kill a session.

# Use session number
$ tmux kill-session -t 0

# Use session name
$ tmux kill-session -t <session-name>

3.5 switching sessions

The tmux switch command is used to switch sessions.

# Use session number
$ tmux switch -t 0

# Use session name
$ tmux switch -t <session-name>

3.6 rename session

The TMUX rename session command renames a session.

$ tmux rename-session -t 0 <new-name>

The above command renames session 0.

3.7 session shortcuts

Here are some session related shortcuts.

  • Ctrl+b d: detach the current session.
  • Ctrl+b s: list all sessions.
  • Ctrl+b $: renames the current session.

4. Pane action

Tmux can divide a window into multiple pane s, each of which runs different commands. The following commands are executed in the Tmux window.

4.1 dividing panes

The TMUX split window command is used to divide the panes.

# Divide the upper and lower panes
$ tmux split-window

# Divide the left and right panes
$ tmux split-window -h

Move Cursor

The TMUX select pane command is used to move the cursor position.

# The cursor switches to the upper pane
$ tmux select-pane -U

# The cursor switches to the lower pane
$ tmux select-pane -D

# The cursor switches to the left pane
$ tmux select-pane -L

# The cursor switches to the right pane
$ tmux select-pane -R

Pane shortcuts

Here are some shortcut keys for pane operations.

  • Ctrl+b%: divide the left and right panes.
  • Ctrl+b ": divide the upper and lower panes.
  • CTRL + B < arrow key >: switch the cursor to other panes. < arrow key > is the direction key to the pane to switch to. For example, to switch to the lower pane, press the direction key ↓.
  • Ctrl+b;: the cursor switches to the previous pane.
  • Ctrl+b o: the cursor switches to the next pane.
  • Ctrl+b {: the current pane is swapped with the previous pane.
  • Ctrl+b}: the current pane is swapped with the next pane.
  • Ctrl+b Ctrl+o: all panes move forward one position, and the first pane becomes the last pane.
  • Ctrl+b Alt+o: all panes move back one position, and the last pane becomes the first pane.
  • Ctrl+b x: close the current pane.
  • Ctrl+b!: split the current pane into a separate window.
  • Ctrl+b z: the current pane is displayed in full screen, and it will change back to its original size when used again.
  • CTRL + B Ctrl + < arrow key >: resize the pane in the direction of the arrow.
  • Ctrl+b q: displays the pane number.

Posted by lewisstevens1 on Fri, 26 Nov 2021 05:06:11 -0800