Files
oam/knowledge base/tmux.md
2024-06-15 14:08:41 +02:00

6.1 KiB

Tmux

  1. TL;DR
  2. Further readings
    1. Sources

TL;DR

There are 3 main objects: sessions, windows, and panes.

Sessions are collections of one or more windows managed as a single unit.
One can have any number of sessions active at one time, but one is typically only attached to one of them.
Each session has a single active window.

Windows contain and are split into one or more panes.
Windows can be thought of as tabs in browsers.
Each window has a single currently active pane and allows to switch to any pane it manages.
Windows are shown in the bar and the current one is marked with a * sign by default.
Windows can be split into panes vertically or horizontally in a tiling fashion.

Each pane is a split in one window and has its own active terminal session.
Only one pane is active and can be interacted with at any time.

Enter commands to Tmux by using the prefix key (ctrl + b by default), followed by the command.
See usage for details.

Installation and configuration
# Install.
brew install 'tmux'

# Get the default settings.
# Might need to run from inside a sessions.
# Specify a null configuration file so that tmux ends up printing whatever is hard-coded in its source.
tmux -f '/dev/null' show-options -s
tmux -f '/dev/null' show-options -g
tmux -f '/dev/null' list-keys

The configuration file is $HOME/.tmux.conf or $XDG_CONFIG_HOME/tmux/tmux.conf.

set -g default-terminal "screen-256color"   # set the default terminal mode to 256 colors
set -g history-limit 100000                 # set the scrollback size
set -g mouse on                             # enable mouse control (clickable windows, panes, resizable panes)
set -g xterm-keys on                        # pass xterm keys through

setw -g automatic-rename on   # rename the window to reflect the current program
set -g renumber-windows on    # renumber all windows when a window is closed
Usage
# Reload settings.
tmux source '/path/to/config.file'

# Start sessions if none is attached.
tmux

# Create new named sessions.
tmux new -s 'session-name'

# List active sessions.
tmux list-session
tmux ls

# Attach to the most recent session.
tmux attach
tmux a

# Attach to specific sessions.
tmux attach -t named-session
Key combination Effect
ctrl + b actuate
ctrl + b, then d detach from the current session
ctrl + b, then s list active sessions
ctrl + b, then w list active sessions with preview window
ctrl + b, then c create a new window
ctrl + b, then n pass to the next window
ctrl + b, then p pass to the previous window
ctrl + b, then 0 to 9 pass to the window identified by that number
ctrl + b, then " split the current window with a horizontal line
ctrl + b, then % split the current window with a vertical line
ctrl + b, then arrow key switch to the pane pointed to by the arrow key's direction
ctrl + b, then { switch to the pane left to the current one
ctrl + b, then } switch to the pane right to the current one
ctrl + b, then q, then number switch to the pane identified by the number
ctrl + b, then z toggle full screen for the current pane
ctrl + b, then ! turn the current pane into a window
ctrl + b, then x close the current pane
Real world use cases
# Dedicate sessions to commands.
# Attaches to the session if it already exists, or creates it otherwise.
# Closes the session once the command finishes.
tmux new-session -As 'gitlab-upgrade' "dnf update 'gitlab-ee'"

# Operate on a background session from another one.
tmux new-session -d -S 'session-name'
tmux send-keys -t 'session-name': "command" "Enter"
tmux capture-pane -t 'session-name': -S - -E - -p | cat -n
tmux kill-session -t 'session-name'

Further readings

Sources