TMUX primer and cheat-sheet

TMUX primer and cheat-sheet

Tmux is a terminal multiplexer that can greatly improve your productivity on the command line. You can use tmux instead of opening multiple terminal windows on your local machine. With tmux you can ssh into multiple systems from a single terminal session without wasting time touching your mouse to change tabs on your local terminal program. Even better you can disconnect and reconnect to a previous tmux session so long as the server hasn’t been rebooted. This means if the network connection is broken all your work is not lost.

TMUX Cheat Sheet

Tmux is a very large tool with an ocean of possible commands that can be overwhelming. This is a minimum list of commands to get started.

To do this …enter this command outside tmux
Start new tmux session
tmux
Start new with session name
tmux new -s somename
Attach to existing session
tmux a
Attach to named session
tmux a -t myname
List sessions
tmux ls
Kill session
tmux kill-session -t myname

 
 

Best practices

  • At the end of the day use detach (PREFIX + d) instead of killing your tmux session on connect.
  • Attach (tmux a) to your existing tmux session instead of starting a new one.
  • Ignoring this can lead to stacking up orphaned tmux sessions on connect and dead puppies.

 

Meta-key aka Prefix functions

In tmux, hit the prefix ctrl+b and then press one of the following keys to send a command to tmux.

Sessions

  • :new == new session
  • s list sessions
  • $ name session
  • d detach from session

 

Windows (tabs)

  • c new window
  • w list windows
  • n next window
  • p previous window
  • f find window
  • , name window
  • & kill window

 

Panes (splits)

  • % vertical split
  • “ horizontal split
  • o swap panes
  • q show pane numbers
  • x kill pane
  • + break pane into window (e.g. to select text by mouse to copy)
  • – restore pane from window
  • ⍽ + space – toggle between layouts
  • PREFIX + q (Show pane numbers, when the numbers show up type the key to goto that pane)
  • PREFIX + { (Move the current pane left)
  • PREFIX + } (Move the current pane right)

 

Resizing Panes

You can also resize panes if you don’t like the layout defaults. These commands are entered into the tmux command line. This is different than using the prefix + keystroke pattern described above. Instead prefix + : will open a command prompt inside tmux. From here enter the following commands to resize panes.

Note all of these commands are at the colon prompt which can be revealed by issuing the prefix key combo.

  • : resize-pane (By default it 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 20 (Resizes the current pane down by 20 cells)PREFIX : 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)

 

Copy mode

Tmux provides a useful function to copy text from one pane into another without having to touch your mouse at all.

Press “prefix key”, [ to enter copy mode for the currently selected pane. A small yellow box should appear in the top right corner of the pane. This box tells shows which line of text your cursor is on and how many lines of text are available for scrollback. Now you use the arrow keys to move around the pane, select and then copy some text into the tmux clipboard. Normally tmux is not configured to make this copied text available in your local system clipboard. So you can’t do a cmd + p in another window.

To exit copy mode press Esc or q depending on your personal mode-keys setting in ~/.tmux.conf. By default tmux will use copy mode commands from the “emacs” column shown below.

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

 

If you found this article helpful and wish to learn more about Tmux, we recommend starting with the official GitHub page, Josh Clayton’s Crash Course, and Ham Vocke’s Quick and Easy Guide to tmux. Happy computing!

Questions? Contact our sales team for help!