Screen & Tmux
Published:
Screen
Screen is a very simple and easy-to-use terminal multiplexer that can separate sessions from windows, preventing server programs from being interrupted due to disconnections. For more powerful features, you can refer to the tmux tutorial, tmux is a more powerful terminal multiplexer.
Installation:
apt-get install screen
Common Commands:
screen -S python
Create a session named python
screen -ls
View already created sessions
screen -r
Resume a session
exit
Exit
Shortcuts: Ctrl + a, d # Detach from the current session
Ctrl + a, c # Create a sub-session in the current screen session
Ctrl + a, w # List sub-sessions
Ctrl + a, p # Previous sub-session
Ctrl + a, n # Next sub-session
Ctrl + a, 0-9 # Switch between window 0 to window 9
References: Screen command detailed explanation
Tmux
Tmux is a more powerful terminal multiplexer than screen.
Sessions & Windows & Panes
Session: The typical way to use the command line is to open a terminal window and type commands inside it. This temporary interaction between the user and the computer is called a “session”.
Window: A window is the interactive interface provided by the terminal for a session to execute commands. An important feature of sessions is that windows are linked to the processes started within them. When a window is opened, the session begins; when the window is closed, the session ends, and the processes within the session are terminated, regardless of whether they have finished running. To solve this problem, tmux “unbinds” sessions from windows: when a window is closed, the session does not terminate but continues to run, and can be “bound” to another window when needed in the future.
Pane: In development, it may be necessary to use vim to edit files while simultaneously running programs and observing resource utilization status, etc. Tmux can split a window into multiple small panes for simultaneously executing various tasks in a visual manner.
Session Operations
Tmux windows have a lot of shortcuts. All shortcuts are activated by a prefix key. The default prefix key is Ctrl+b, meaning that you must first press Ctrl+b for the shortcut to take effect. For example, the shortcut for the help command is Ctrl+b ?. Its usage is to press Ctrl+b first, and then press ? in the Tmux window to display the help information. Then, pressing the ESC key or the q key will exit the help.
Some common commands are as follows:
# Create a new session
tmux new -s <session-name>
# Detach from a session
tmux attach
# List existing sessions
tmux ls
tmux list-session
# Re-attach to a session
tmux attach -t <session-name>
# Kill a session
tmux kill-session -t <session-name>
# Switch to a session
tmux switch -t <session-name>
# Rename a session
tmux rename-session -t <old-name> <new-name>
Shortcuts: Ctrl+b d: Detach from the current session. Ctrl+b s: List all sessions. Ctrl+b $: Rename the current session.
Window Operations
## Create a new window
tmux new-window
# Create a new window with a specified name
tmux new-window -n <window-name>
## Switch to a window
# Switch to a window by its number
tmux select-window -t <window-number>
# Switch to a window by its name
tmux select-window -t <window-name>
## Rename a window
tmux rename-window <new-name>
Shortcuts:
Ctrl+b c: Create a new window, the status bar will display information for multiple windows.
Ctrl+b p: Switch to the previous window (in the order shown on the status bar).
Ctrl+b n: Switch to the next window.
Ctrl+b
Ctrl+b w: Select a window from a list.
Ctrl+b ,: Rename the window.
Pane Operations
## Split panes
# Split the window into two vertical panes
tmux split-window
# Split the window into two horizontal panes
tmux split-window -h
## Move the cursor
# Move the cursor to the upper pane
tmux select-pane -U
# Move the cursor to the lower pane
tmux select-pane -D
# Move the cursor to the left pane
tmux select-pane -L
# Move the cursor to the right pane
tmux select-pane -R
## Move panes
# Move the current pane up
tmux swap-pane -U
# Move the current pane down
tmux swap-pane -D
Shortcuts:
Ctrl+b %: Split the window into two vertical panes.
Ctrl+b “: Split the window into two horizontal panes.
Ctrl+b
Ctrl+b ;: Move the cursor to the previous pane.
Ctrl+b o: Move the cursor to the next pane.
Ctrl+b {: Swap the current pane with the previous pane.
Ctrl+b }: Swap the current pane with the next pane.
Ctrl+b Ctrl+o: Move all panes one position forward, making the first pane the last pane.
Ctrl+b Alt+o: Move all panes one position backward , making the last pane the first pane.
Ctrl+b x or Ctrl+b d: Close the current pane.
Ctrl+b !: Detach the current pane into a separate window.
Ctrl+b z: Make the current pane full-screen; press again to return to its original size.
Ctrl+b Ctrl+
Ctrl+b q: Display pane numbers.
Suggestions
- Try to use shortcuts instead of commands as much as possible; shortcuts can also be customized by modifying the configuration file
- According to personal habits, open multiple panes and windows for efficient development at the same time
References: