logo

Cheatsheets - tmux

# start new
$ tmux

# start new with session name:
$ tmux new -s foo

# list sessions
$ tmux ls

# rename:
$ tmux rename-session -t 0 foo

# attach
$ tmux a  #  (or at, or attach)
$ tmux attach -t 0
$ tmux a -t foo
$ tmux attach-session -t 0

# detach
$ tmux detach
# or `Ctrl-b d`

# kill
$ tmux kill-session -t myname
# kill all
$ tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill

C-b = Ctrl-b

Sessions

  • :new<CR> new session
  • C-b s list sessions
  • C-b $ name session

Windows (tabs)

  • C-b c: create a window
  • C-b n: next window
  • C-b p: previous window
  • C-b 1: jump to window 1
  • C-b ,: name window
  • C-b w: list windows
  • C-b f: find window
  • C-b &: kill window
  • C-b .: move window - prompted for a new number
  • :movew<CR>: move window to the next unused number

Panes (splits)

  • C-b %: vertical split
  • C-b ": horizontal split
  • C-b o: swap panes
  • C-b q: show pane numbers
  • C-b x: kill pane
  • C-b +: break pane into window (e.g. to select text by mouse to copy)
  • C-b -: restore pane from window
  • C-b ⍽: space - toggle between layouts
  • C-b {: Move the current pane left
  • C-b }: Move the current pane right
  • C-b z: toggle pane zoom

Resize Panes

:resize-pane -D (Resizes the current pane down)
:resize-pane -U (Resizes the current pane upward)
:resize-pane -L (Resizes the current pane left)
:resize-pane -R (Resizes the current pane right)
:resize-pane -D 20 (Resizes the current pane down by 20 cells)
:resize-pane -U 20 (Resizes the current pane upward by 20 cells)
:resize-pane -L 20 (Resizes the current pane left by 20 cells)
:resize-pane -R 20 (Resizes the current pane right by 20 cells)
:resize-pane -t 2 20 (Resizes the pane with the id of 2 down by 20 cells)
:resize-pane -t -L 20 (Resizes the pane with the id of 2 left by 20 cells)

Window/pane surgery

:joinp -s :2<CR>  move window 2 into a new pane in the current window
:joinp -t :1<CR>  move the current pane into a new pane in window 1

Misc

  • C-b d: detach
  • C-b t: big clock
  • C-b ?: list shortcuts
  • C-b :: prompt

More Details

https://github.com/tmux/tmux

2 big features:

  • Window management: could be achieved by using modern terminals like iTerm2
  • Session management: detaching from and attaching to sessions; very useful for remote working (running jobs on remote / cloud dev servers).

Why does using a terminal multiplexer help?

  • Upon laptop crash/reboot, you lose no state from the multiplexed terminal sessions on your (dev) server
  • you can search scrolled screen content quickly and mechanically
  • you can easily disconnect/reconnect a "session" -- no more HUPs
  • platform independence: use tmux on Macbook, Linux notebook / desktop, servers, Raspberry Pis, etc.
  • customizable: customize the look and behavior of the tmux environment. And sync this across different platforms using a simple dotfile
  • share screens: it can "mirror" its session on multiple screens. If two users log into the same Tmux session, then they both see the same output on their screens in real time. (e.g. for training someone remotely, debugging a command that isn't working for them, reviewing text, monitoring services or processes)

e.g., you're in the middle of a 20-minute run that you did not protect via nohup, when you need to go to a meeting or catch a bus. With neither mosh nor screen/tmux, you'd lose the ssh connection.

tmux is based on a client-server architecture. The tmux server keeps track of all the running sessions. You will only ever work with the tmux client and create new sessions or connect to existing ones.

Concepts:

  • Sessions are collections of windows that can be detached and reattached later.
  • Windows may contain one or more panes.

Configuration

Enable scrollback: Create or edit ~/.tmux.conf on the server and add the following commands:

new-session
set-option -g mouse on
set -g history-limit 30000

Session

#S: name of the session.

If not named, the name is a number, shown at the bottom left in status bar like [0].