November 21, 2018 · Don't Forget mac linux

tmux shortcuts & cheatsheet

Tmux is a terminal multiplexer. It creates a host server on your server and connects to it with a client window. If the client is disconnected, the server keeps running. One can reattach to the tmux session and the files, processes and general activity that you were working with will still be active.

Command mode is enable via a prefix ctrl+b.

Startup and Sessions:

tmux ls # lists sessions

tmux attach -t 0 # attaches to session 0

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

prefix + :new # creates new session

prefix + s # lists sessions

prefix + $ # renames session

prefix + ( # switch to previous session

prefix + ) # switch to next sesssion

prefix + d # detach

prefix + ? # list shortcuts

prefix + D # gives you a list of connected clients, and which ever you select is disconnected

prefix + q # briefly flashes pane numbers in each pane

Panes (splits):

prefix + % # vertical split

prefix + " # horizontal split

prefix + :splitw -hb # horizontal split, placed before current pane (ie: left side)

prefix + :splitw -bf # full width pane, on top of window with a vertical split

prefix + :splitw -fh # full width pane, on right side of window with a horizontal split

prefix + :splitw -t 2 # splits pane number 2 vertically

prefix + o # swap panes

prefix + x # kill pane

prefix + :respawn-pane -k # respawn the pane (kill/restart shell)

prefix + + # break pane into window (e.g. to select text by mouse to copy)

prefix + - # restore pane from window

prefix + space # toggle between layouts

prefix + q # Shows pane numbers, when the numbers show up type the key to goto that pane

prefix + arrows # Navigates between panes

prefix + ALT + arrows # resizes active pane

prefix + z # toggle pane zoom (repeat to unzoom)

prefix + 'setw synchronize-panes' # sync panes

Windows (tabs):

prefix + c  # create window

prefix + w  # list windows

prefix + n  # next window

prefix + p  # previous window

prefix + 0-9 # Switch to a window using index number

prefix + f  # find window

prefix + ,  # name window

prefix + &  # kill window

Copy mode:

prefix + [ # enables copy mode

prefix + ] # pastes copied buffer

(prefix + 'setw -g mode-keys vi' # vi mode)

With this option set, we can use h, j, k, and l to move around buffer. To get out of Copy mode, just press the ENTER key. Use w to jump to the next word and b to jump back one word, and f, followed by any character, to jump to that character on the same line, and F to jump backwards on the line.

   Function                vi             emacs
----------------------    --------       -------
   Back to indentation     ^              M-m
   Clear selection         Escape         C-g
   Copy selection          Enter          M-w
   Cursor down             j              Down
   Cursor left             h              Left
   Cursor right            l              Right
   Cursor to bottom line   L
   Cursor to middle line   M              M-r
   Cursor to top line      H              M-R
   Cursor up               k              Up
   Delete entire line      d              C-u
   Delete to end of line   D              C-k
   End of line             $              C-e
   Goto line               :              g
   Half page down          C-d            M-Down
   Half page up            C-u            M-Up
   Next page               C-f            Page down
   Next word               w              M-f
   Paste buffer            p              C-y
   Previous page           C-b            Page up
   Previous word           b              M-b
   Quit mode               q              Escape
   Scroll down             C-Down or J    C-Down
   Scroll up               C-Up or K      C-Up
   Search again            n              n
   Search backward         ?              C-r
   Search forward          /              C-s
   Start of line           0              C-a
   Start selection         Space          C-Space
   Transpose chars                        C-t

Other useful hints:

export TERM="xterm-256color" # Vim doesn't detect that the terminal supports 256 colors because tmux's default TERM is screen
 
:source-file ~/.tmux.conf # If you have made changes to your tmux configuration file in the ~/.tmux.conf file, it shouldn’t be necessary to start the server up again from scratch with kill-server. Instead, you can prompt the current tmux session to reload the configuration with the source-file command

For more references, check this out.