Want some programming help? [close]

Tmux Basics


Tmux is a terminal multiplexer that allows you to manage multiple terminals at once and even split the screen in to multiple terminals. It is similar to the GNU screen tool but in my opinion a better alternative.


# Ubuntu/Debian
sudo apt install tmux
# Fedora/CentOS/RHEL
sudo yum install tmux
# Arch Linux
sudo pacman -S tmux

Basic usage

The basic idea with tmux is that you create sessions which you can connect and disconnect to. Sessions will continue running in the background even if you disconnect (detach). Sessions can have multiple windows but we will first look at the basics of creating sessions, detaching, and reattaching.

Start a new session with tmux. Detach from a session using tmux detach. Then reconnect to an existing session with tmux attach.

# Start a new tmux session
# Disconnect from a tmux session
tmux detach
# Reconnect to the session
tmux attach

You can have multiple active sessions at once. To list all active sessions use tmux ls:

# List active sessions
tmux ls

You can reconnect to a session using it's numeric ID.

# Connect to a specific session by numeric ID
tmux attach -t 0

If you want to create a named session you can do that like this:

# Create a new named session
tmux new -s "mysession"

Then to reconnect to a named session later:

# Connect to a session by name
tmux attach -t "mysession"

Get help

Since there are so many commands and keybinds, it can be hard to remember them all. There are a few commands worth memorizing in order to get help in the future. If you remember these, you will be able to figure out anything you need.

man tmux
tmux --help
tmux list-keys
tmux list-commands

Using commands

tmux attach
tmux detach
tmux ls # List sessions

tmux new-window
tmux list-windows
tmux select-window -t 1
tmux rename-window # Renames current window

tmux split-window
tmux split-window -h

tmux swap-pane -\[UDLR\]

Common Keyboard shortcuts

Every command is assumed to begin with the chord, which by default is CTRL-B. Press CTRL-B and then release before pressing the next action key.

Here are some shortcuts that are particularly worth knowing.

  • List shortcuts - ?

  • Detach from a session - d

  • Create a new window - c

  • List window - w
  • Select a window - (the number of the window)
  • Rename windows - ,
  • Kill window - &

  • Next window - n

  • Previous window - p
  • Last window - l

  • Split window horizontally in two panes - "

  • Split window vertically in to two panes - %

  • Switch active pane - up/down/left/right arrow

  • Next pane - o
  • Previous pane - ;
  • Kill pane - x

  • Break out pane in to window - !

  • Resize pane 1 unit - CTRL- plus left/right/up/down arrow
  • Resize pane 5 units - ALT- plus left/right/up/down arrow
  • Swap pane up/down - { or }
  • Rotate pane up/down - CTRL-o or ALT-o

  • Switch to next pre-defined layout: Space bar

  • Set even split horizontally - ALT-1
  • Set even split vertically - ALT-2

Send keys to a session

# Run a new screen named `myapp` in detached mode
tmux new-s -s mysession -d "vim"

# Send some vim commands to the session
tmux send-keys -t mysession "i" "hello" Enter Escape ":w! test.txt" Enter

All keys including functions, alt, home, page up, arrow keys, and even mouse actions can all be emulated. Here is a full list of keys from the source code of tmux: https://github.com/tmux/tmux/blob/ec7f5305b1a6e5548f0769f988e76b01ec293dcc/key-string.c#L33-L100

# Shut down the tmux terminal when done
tmux kill-session -t myapp


After reading this you should understand the basics of tmux and how to perform common actions like:

  • creating new sessions
  • creating windows and panes
  • switching between windows
  • splitting the terminal
  • detaching and reattaching to a session