IT Services

How to use multiple terminal windows and shared sessions with tmux

On this page:

tmux allows you to use terminal windows in a session that you can enter (attach) and leave (detach) without interrupting running programs. This is useful, for instance, when your are connected via SSH, a large file is being copied and an interruption of the SSH connection would abort a not yet completed copy.

 

How to manage tmux sessions?

# check active tmux sessions
tmux ls
tmux list-sessions

# create and enter a new tmux session
tmux new -s session-name

# leave an tmux session you entered (detach)
CTRL+b d

# enter an existing tmux session previously identified using tmux ls
tmux attach -t [session name]

# ungracefully end an active tmux session without entering it
tmux kill-session -t session-name

# rename an existing tmux session
tmux rename-session -t oldname newname
 

How to list all keybindings in tmux?

CTRL+b ?

# bonus: create your own key bindings by editing ~/.tmux.conf
bind | split-window -h # this enables you to split a window horizontally using CTRL-b | 
bind - split-window -v # vertical split: CTRL-b -

# apply your customisations
tmux source-file ~/.tmux.conf

# bonus bonus: activate mouse interaction by editing ~/.tmux.conf
# this enables you to resize tmux panes with your mouse
set -g mouse on

# bonus bonus bonus: change tmux colours by editing ~/.tmux.conf
set -g default-terminal "screen-256color" # this is the default
set -g default-terminal "xterm-256color"
 

How to navigate in an active tmux session?

You can work with a single window or split a window into panes.

Working with a single tmux window

# how to scroll in tmux - type q to leave tmux scroll mode
CTRL+b [

# how to show tmux history - go back by typing q:
CTRL+b ~

# how to create a new window in tmux
CTRL+b c

# how to rename the current window in tmux
CTRL+b ,

# how to show the list of active tmux windows
# the list includes a preview of the window and the window number
CTRL+b w

# how to move to the next window in tmux
CTRL+b n

# how get into the previous window in tmux
CTRL+b p

# how to get into a specific window number in tmux
CTRL+b [0-9]

# how to get into the previously active tmux window
CTRL+b l # that's a lowercase L

# how to ungracefully close the current tmux window
CTRL+b & # you get prompted if you really want to do that

Working with panes by splitting a tmux window

# how to split a tmux window into panes
CTRL+b % # vertical split
CTRL-b " # horizontal split

# how to move the current tmux pane left or right
CTRL+b { # left
CTRL+b } # right

# how to switch to the next tmux pane
CTRL+b o

# how to show tmux pane numbers and switch to a numbered pane
CTRL+b q # when the numbers show up type the key to go to that pane

# how to resize tmux panes
# see bindings section to resize panes using your mouse
CTRL+b CTRL-arrowkey # keep CTRL pressed to use an arrow key multiple times

# how to make a tmux pane its own window
CTRL+b : # now enter "break-pane" without the double quotes
 

How to use tmux for shared sessions with multiple users?

# WARNING, only do this with trustworthy people
# the following is a malicious way to use a shared tmux session
tmux -S /tmp/pair detach -t /dev/victim_tty -E '(malicious shell code) & exec tmux -S /tmp/pair attach'

# anyone in the same POSIX group can join the following tmux session
# NOTE: the session name is a path
tmux -S /tmp/pair
chmod 770 /tmp/pair

# additional tmux particpants in the right POSIX group join like this
tmux -S /tmp/pair attach

Make a payment