Tmux usage

From wikinotes

Components

pane:    window-splits. there is always at least one pane per window.
window:  a full terminal-screen. A session may have many windows. A Window may have many panes.
session: Contains one or more windows. Each terminal has one or more sessions, and can switch focus to other sessions.
server:  a tmux server runs all sessions/windows/panes for a login
   +------( window )------+
  /                        \

+-----------------------------+
|             |               | <-+--- panes
|             |               |   |
|             +---------------+   |
|             |               | <-+
+-----------------------------+
| 1 |*2*| 3 |            12:00|  <-- window list
+-----------------------------+

Controlling Tmux

Keybindings

Set these in your tmux.conf.

$prefix + x  # kill-pane

Command-Mode

By default, sending $prefix + : enters command mode.
You can enter tmux commands here directly.

kill-pane

CLI

Pass commands as arguments to tmux directly on the command line.

tmux kill-pane

Scripting Tmux

Simple Example

+-------------------+
|     myproject     |
+---------+---------+
|   docs  |  build  |
+---------+---------+
tmux respawn-pane -k -c ~/src/myproject
tmux split-window    -c ~/src/myproject/docs
tmux split-window -h -c ~/src/myproject/build

# also useful:
tmux select-pane -${DIRECTION}  # U/D/L/R up/down/left/right

Overriding RPS1

I configured my ~/.zshrc to let ${OVERRIDE_RPS1} if set to replace ${RPS1}.
In some tailored work-environments, it might be useful to label each tmux pane with it's purpose.

tmux respawn-pane \
  -k \
  -c /mnt/usb/workthings \
  -e OVERRIDE_RPS1='[%F{green}workthings%f]'

Commands

Sessions

tmux new-session -d -s ${session_name} -c ${cwd}
tmux switch -t ${session_name}
tmux list-sessions

Windows

window == tab

tmux new-window \
  -t ${session}:${window_num}      `# target session` \
  -n ${window_name}                `# tab-name` \
  -e FOO=abc -e BAR=def            `# environment variables` \
  -c ${cwd}                        `# starting directory`

tmux split-window \
  -t ${session}:${window_num} \
  -c ${cwd}

tmux respawn-pane -k -c ${cwd}      # change directory of current pane
tmux rename-window ${window_name}   # rename window
tmux display-message -p '#I'        # print current window number

Panes

tmux split-window             # create vertical split, select bottom-most
tmux split-window -h          # create horizontal split, select right-most

tmux select-pane ${direction} # -U/-D/-L/-R Up/Down/Left/Right

tmux resize-pane -Z           # maximize/monocle-view pane

Notifications

tmux show notifications through the command
tmux display-message -c /dev/pts/0 'message'.