Tmux for keyboard driven development

Keyboard driven development means using your keyboard as much as possible while programming, avoiding the use of mouse or touchpad. The advantages include being much faster and showing off your unix foo, and being more ergonomic, since repeatedly moving your hand from keyboard to mouse is a waste of time that will eventually give you a repetitive strain injury.

This post serves as a cheatsheet for myself, in case to need to review one of the less used keyboard shortcuts. A lot of people like to bind their prefix to the caps lock key, but I already use that to activate my Karabiner layer (to be explained in another post).

Tmux uses a prefix before accepting commands, let’s change it

# Set the prefix from Ctrl-b to Ctrl-a
set -g prefix C-a

# Free original Ctrl-b prefix keybinding
unbind C-b

# Open config file with Prefix-e
bind e new-window -n "~/.config/tmux/tmux.conf" "nvim ~/.config/tmux/tmux.conf && tmux source ~/.config/tmux/tmux.conf && tmux display \"Reloaded tmux.conf\""

# Reload the config file with Prefix-r
bind r source-file ~/.config/tmux/tmux.conf \; display "Reloaded tmux.conf"

Splitting panes

# Prefix-c new window
bind c new-window -c "#{pane_current_path}"
# Prefix-| split vertically
bind | split-window -h -c "#{pane_current_path}"
# Prefix-- split horizontally
bind - split-window -v -c "#{pane_current_path}"

Moving between panes using vim keys

# Prefix-h left
bind h select-pane -L
# Prefix-j down
bind j select-pane -D
# Prefix-k up
bind k select-pane -U
# Prefix-l right
bind l select-pane -R

Pane resizing

# Prefix-H left
bind -r H resize-pane -L 5
# Prefix-J down
bind -r J resize-pane -D 5
# Prefix-K up
bind -r K resize-pane -U 5
# Prefix-L right
bind -r L resize-pane -R 5

Window navigation

# We use hjkl for panes and Ctrl-hjkl for windows, so let's remove the default
unbind n
unbind p
# Prefix then Ctrl-h switches to previous window
bind -r C-h previous-window
# Prefix then Ctrl-l switches to next window
bind -r C-l next-window
# Prefix then Tab switches to last active window
bind Tab last-window

Copying & session management

# Prefix-y copies the tmux buffer into the macOS clipboard
bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"

# Prefix-b list paste buffers
bind b list-buffers
# Prefix-p paste from the top paste buffer
bind p paste-buffer
# Prefix-Ctrl-P choose which buffer to paste from
bind P choose-buffer

# Find session (only really handy if you name your sessions)
bind C-f command-prompt -p find-session 'switch-client -t %%'

Default keybindings

C-o         Rotate the panes in the current window forwards.
C-z         Suspend the tmux client.
!           Break the current pane out of the window.
"           Split the current pane into two, top and bottom.
#           List all paste buffers.
$           Rename the current session.
%           Split the current pane into two, left and right.
&           Kill the current window.
'           Prompt for a window index to select.
(           Switch the attached client to the previous
            session.
)           Switch the attached client to the next session.
,           Rename the current window.
-           Delete the most recently copied buffer of text.
.           Prompt for an index to move the current window.
0 to 9      Select windows 0 to 9.
:           Enter the tmux command prompt.
;           Move to the previously active pane.
=           Choose which buffer to paste interactively from a
            list.
?           List all key bindings.
D           Choose a client to detach.
L           Switch the attached client back to the last
            session.
c           Create a new window.
d           Detach the current client.
f           Prompt to search for text in open windows.
i           Display some information about the current
            window.
l           Move to the previously selected window.
m           Mark the current pane (see select-pane -m).
M           Clear the marked pane.
n           Change to the next window.
o           Select the next pane in the current window.
p           Change to the previous window.
q           Briefly display pane indexes.
r           Force redraw of the attached client.
s           Select a new session for the attached client
            interactively.
t           Show the time.
w           Choose the current window interactively.
x           Kill the current pane.
z           Toggle zoom state of the current pane.
{           Swap the current pane with the previous pane.
}           Swap the current pane with the next pane.
~           Show previous messages from tmux, if any.
M-1 to M-5  Arrange panes in one of the five preset layouts:
            even-horizontal, even-vertical, main-horizontal,
            main-vertical, or tiled.
Space       Arrange the current window in the next preset
            layout.
M-n         Move to the next window with a bell or activity
            marker.
M-o         Rotate the panes in the current window backwards.
M-p         Move to the previous window with a bell or
            activity marker.
C-Up, C-Down
C-Left, C-Right
            Resize the current pane in steps of one cell.
M-Up, M-Down
M-Left, M-Right
            Resize the current pane in steps of five cells.
Last modified 2021.08.02