Useful tmux command keys

Thought I’d make an entry with useful tmux keys as a reference to myself.

Ctrl-b (also referred to as C-b) is the prefix for tmux commands. You can change this via .tmux.conf; I left mine at the defaults so I’ll stick with that.

After pressing C-b you press a key for the actual command you want tmux to do:

  • C-b again if you want to pass C-b as is to an application in tmux
  • c to create a new window
    • n to switch to the next window
    • p to switch to the previous window
    • ' prompt to enter a window index number to switch to
    • 09 switch to the numbered window
    • w interactively choose a window to switch to
  • : enter the tmux command prompt where you can enter commands
  • [ enter copy mode – this lets you scroll around the history with your arrow keys. This will scroll out of the application window if you go too much up.
    • when in this mode press space to start selecting text and enter to copy the selection into tmux’s buffers
    • press q to quit copy mode
  • ] paste the most recently copied text (from tmux’s buffer)
  • D (shift-d) interactively select a client to detach
    • This is useful when you connect to a session from a larger screen and tmux doesn’t resize the window as the remote client of smaller dimensions is still connected. You simply do C-b S-d and find the connection with smaller dimensions and press Enter to disconnect it.

Others that I haven’t used much but worth a mention

  • ( and ) to switch to the previous and next sessions
  • s interactively choose a session to connect to (a session contains windows & panes)
  • % split the current window into left & right panes
    • o switch to the previous pane (I couldn’t find one to switch to the next pane; good I guess, one less key to remember!)
    • { and } swap the current pane with the previous or next panes

A note about copy-paste

  • When using iTerm2 on the Mac, I can copy a selection of text in a tmux session and it copies it to the Mac clipboard. This is because I told iTerm2 it is ok for terminal applications to access the clipboard.
  • I couldn’t find a similar option in the GNOME Terminal or Xfce Terminal. So here I press SHIFT and make a selection with the mouse. Then use the usual terminal copy shortcut (Ctrl+Shift+c in my case to copy the selection).