Skip to content

Instantly share code, notes, and snippets.

@atkvishnu
Last active April 12, 2023 07:39
Show Gist options
  • Save atkvishnu/fe962b8ed1f684644c4dd2dd0fc01047 to your computer and use it in GitHub Desktop.
Save atkvishnu/fe962b8ed1f684644c4dd2dd0fc01047 to your computer and use it in GitHub Desktop.
TMUX cheatsheet #tmux #cheatsheets #shortcuts

TMUX CheatSheet tmux

Installation:

  • Ubuntu and Debian: sudo apt-get install mux
  • CentOS and Fedora: sudo yum install tmux
  • macOS: brew install tmux
  • Windows: a good workaround for windows is using Ubuntu WSL which can be installed from the Microsoft store.

3 Major things in MUX:

  • Sessions define the general task at hand. Example: If you are testing something, stick to a single session for all activities related to your test.

  • Windows are for specific activities or projects within a session.

  • Panes help you create multiple views within one window. Example: You might be working something in one pane, and using the other to track error logs.

tmux-session-window-panes

tmux-screengrab

TMUX is controlled by pressing CRTL+b, followed by a hotkey.

Start TMUX:

tmux

Start a new named TMUX session

tmux new -s <session-name>

Pane Splitting

Horizontally: CTRL+b "
Vertical: CTRL+b %

Exit TMUX Pane

exit ⏎ / CTRL+d

Moving between panes

Current pane is highlighted in green. Toggle between panes: CTRL+b o TMUX assigns a number to each pane. We can quickly press the number of a pane to switch to it.

Example: CTRL+b q -> gives the numbers -> quickly press -> 1/2/3/.....

Resize panes

You can change the size of each pane. To do so, press CTRL+b : bottom bar changes from green to yellow. Now use these commands to resize the panes!

resize-pane -D: moves the boundary line for the pane downward. resize-pane -U: Up. resize-pane -R: Right. resize-pane -L: Left.

We can also specify number of cells to move the boundary line. Ex., resize-pane -U 10: Moves the boundary line up 10 cells.

We can specify a different cell than the ones you are working in. Ex., To resize cell 2 (lower right): resize-pane -t 2 --R 5 -> Moves the boundary line 5 cells to the right.

Resizing has a couple of considerations:

  • Resizing only works on the boundary line between cells. If the cell doesn't have a boundary line, the command won't work. Example: Trying to resize the upper cell right won't work, because it's already the full width of the screen.

  • Resizing a shared boundary line can change the size of another cell. Example: moving the upper boundary line of cell 1 will also change the size of cell 2.

Command Description
CTRL+b+ Resize current pane height(holding second key is optional)
CTRL+b CTRL + ↑ -do-
CTRL+b -do-
CTRL+b CTRL + ↓ -do-
CTRL+b Resize current pane width(holding second key is optional)
CTRL+b CTRL + → -do-
CTRL+b -do-
CTRL+b CTRL + ← -do-
resize-pane -D Resizes the current pane down
resize-pane -U Resizes the current pane up
resize-pane -L Resizes the current pane left
resize-pane -R Resizes the current pane right
resize-pane -D 20 Resizes the current pane down by 20 cells
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

Zoom into Pane

CTRL+b : -> resize-pane -Z This will expand the current pane. Use the same command to set it back to normal.

Detaching and Reattaching from TMUX Session

Detaching: tmux detach OR CTRL+b d TMUX can be used to keep a process working in the background. You can detach from a current session and return to your normal shell by typing.

tmux detach OR CTRL+b d

There will be a output which will read [detached(from session X)].

Re-attaching: tmux attach -t <session-name> OR tmux attach-session -t <session-name> OR tmux a -t <session-name> OR tmux at -t <session-name> To attach to a session first, you need to find the name of the session. To get a list of the currently running sessions type: $ tmux ls The name of the session is the first column of the output.

0: 1 windows (created XXX XXX XX XX:XX:XX XXXX)
my_named_session: 1 windows (created XXX XXX XX XX:XX:XX XXXX)

As we can see from the output there are 2 running tmux sessions. The first one is name 0 and the second one my_named_session.

To re-attach to session 0: $ tmux attach -t 0 OR tmux attach-session -t 0

Moving between sessions

Command Description
CTRL+b w Sessions and window preview
CTRL+b ( Move to previous session
CTRL+b ) Move to next session
CTRL+b $ Rename session
CTRL+b d Detach from session

Most basic commands

Command Description
tmux Start tmux
tmux new -s <session-name> Start a new named TMUX session
tmux ls Shows the list of sessions
tmux detach OR CTRL+b d Detaching a session
tmux attach -t <session-name> OR tmux attach-session -t <session-name> Re-attaching a detached session
tmux a <session-name> OR tmux a -t <session-name> Re-attaching a detached session <session-name>
tmux kill-session –t <session-name> kill the session named <session-name>
tmux kill-server kill the tmux server
tmux info Show every session, window, pane, etc...

Following commands can be used after typing CTRL+b

Reload

Command Description
r reload .tmux.conf file

Sessions

Command Description
s list sessions
$ rename sessions
d detach sessions

Windows

Command Description
w list windows and select one
, rename window
c or N create new window
n go to next window
p go to previous window
f find window
& kill/close current window
0-9 go to window 0-9

Panes (split windows)

Command Description
% vertical split
" horizontal split
; toggle last active pane
{ Move to the current pane left
} Move to the current pane right
x kill pane
o go to next pane
(space) toggle between pane layouts
h, j, k, l go to next pane in vim-style
← ↓ ↑ → (arrow keys) switching with arrow keys
z toggle full-screen mode for current pane
q show pane numbers
! convert pane into a window
+ break pane into window (ex., to select text by mouse to copy)
- restore pane from window

Commands

Command Description
: go to command mode

Next the commands mentioned below can be used:

Command Description
list-keys shows all the commands
list-panes shows the names of all panes
resize-pane -D 20 resize down
resize-pane -U 20 resize up
resize-pane -L 20 resize left
resize-pane -R 20 resize right
swap-pane -s 3 -t 1 swap pane 1 with pane 3

Copy mode

'Vim' mode is set in the .tmux.conf file and Esc is used to enter into the copy mode. OR :setw -g mode-keys vi: use vi keys in buffer

Command Description
Esc OR CTRL+b [ go to copy mode

Following commands can be used WITHOUT using CTRL+b

Command Description
q quit mode
h, j, k, l left, down, up, right
J or K scroll down or up
F or B go to next or previous page
$ go to end of line
0 go to beginning of line
w or b go to next or previous word (one word at a time)
/ search forward
? search backward
n search next (use after above commands)
n Next keyword occurrence
N Previous keyword occurrence
space start selection
Esc clear selection
y OR Enter copy section
g Go to Top line
G Go to bottom line
L Cursor to bottom line
M Cursor to middle line
H Cursor to top line
k Cursor up
d Delete entire line
D Delete to the end of line
p paste buffer
CTRL+Down OR J Scroll down
CTRL+Up OR K Scroll up

Following command can be used WITH CTRL+b for paste operation.

Command Description
p OR ] paste selection

Interaction with Buffers

Command Description
: go to command mode
:show-buffer display buffer_0 contents
:capture-pane copy entire visible content of pane to a buffer
:list buffers Show all buffers
:choose-buffer Show all buffers and paste selected
:save-buffer buf.txt Save buffer contents to buf.txt
:delete-buffer -b 1 delete buffer_1
CTRL+b ] Paste contents of buffer_0

Misc. configuration commands

Command Description
:set -g <OPTION> Set <OPTION> for all sessions
:setw -g <OPTION> Set <OPTION for all windows
:set -g mouse on Enable mouse mode, if you wanna use the mouse
CTRL+b + :setw synchronize-panes Sync panes

CONFIGURATION FILE

.tmux.conf file

When Tmux is started, it reads its configuration parameters from ~/.tmux.conf if the file is present.

NOTE: First, create a dot file .tmux.conf in the ‘home’ folder by doing touch .tmux.config.

Here is a sample ~/.tmux.conf configuration with customized status line and few additional options:

# ~/.tmux.conf

# make delay shorter
set -sg escape-time 0


# key bindings

# reload config file
bind r source-file ~/.tmux.conf \; display ".tmux.conf reloaded!"

# quickly open a new window
bind N new-window

# synchronize all panes in a window
bind y setw synchronize-panes

# pane movement shortcuts (same as vim)
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# enable mouse support for switching panes/windows
set -g mouse-utf8 on
set -g mouse on


# copy mode : vim

# set vi mode for copy mode
setw -g mode-keys vi

# copy mode using 'Esc'
unbind [
bind Escape copy-mode

# start selection with 'space' and copy using 'y'
bind -t vi-copy 'y' copy-selection

# paste using 'p'
unbind p
bind p paste-buffer

# Improve color
set -g default-terminal 'screen-256color'

# Customize the status line
set -g status-fg  green
set -g status-bg  black

More about TMUX

Learn more about TMUX on the manual pages. man tmux in your terminal to visit the manual pages .

HELP

Command Description
CTRL+b ? show the list of commands
:list-keys OR tmux list-keys listing keys
tmux info Show every session, window, pane, etc...
@atkvishnu
Copy link
Author

atkvishnu commented Nov 10, 2021

Future work: Need to group commands into similar categories!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment