tmux 101

Vignesh Muthukumaran - Jul 17 - - Dev Community

tmux is a terminal multiplexer. We can have multiple terminals inside a single terminal, which is especially useful when we ssh into a remote machine.

Image description

tmux has 3 levels of hierarchy,

  • Sessions - To have completely different work environments for different concerns
  • Windows - To have multiple tabs for what that window represents, basically one for monitoring logs and performance and another to execute the commands required.
  • Panes - To have different panes inside a window, like one running top, and another running a couple of tail commands on log files.

To start using tmux, we just need to run the command tmux.

One key concept in tmux is the concept of prefix key. After pressing the prefix key, tmux enters command mode, similar to vim's insert, command, and view modes. The prefix key for tmux by default is Ctrl + B.

Basic commands

  • Start a new session
tmux
Enter fullscreen mode Exit fullscreen mode
  • Detach from a session
Ctrl+B d
Enter fullscreen mode Exit fullscreen mode
  • Attach to the last accessed session
tmux attach 
Enter fullscreen mode Exit fullscreen mode
  • Exit and quit tmux
Ctrl+B &
Enter fullscreen mode Exit fullscreen mode

Session management

  • Create a named session
tmux new -s <session_name>
Enter fullscreen mode Exit fullscreen mode
  • Attach to a session
tmux attach -t <session_name>
Enter fullscreen mode Exit fullscreen mode

or

tmux a -t <session_name>
Enter fullscreen mode Exit fullscreen mode
  • Switch sessions
tmux switch -t <session_name>
Enter fullscreen mode Exit fullscreen mode
  • List sessions
tmux ls
Enter fullscreen mode Exit fullscreen mode
  • Kill sessions
tmux kill-session -t <session_name>
Enter fullscreen mode Exit fullscreen mode

Window management

Key Operation
Ctrl+B C Create new window
Ctrl+B N Move to next window
Ctrl+B P Move to previous window
Ctrl+B L Move to last window
Ctrl+B 0-9 Move to window by index number

Pane management

Key Operation
Ctrl+B % Vertical split (panes side by side)
Ctrl+B " Horizontal split (one pane below the other)
Ctrl+B O Move to other pane
Ctrl+B ! Remove all panes but the current one from the window
Ctrl+B Q Display window index numbers
Ctrl+B Ctrl-Up/Down Resize current pane (due north/south)
Ctrl+B Ctrl-Left/Right Resize current pane (due west/east)
Ctrl+B W Open a panel to navigate across windows in multiple sessions

Command mode

Some other key points are we can enter command mode by

Ctrl + B :

We can allow access to a mouse by

set -g mouse
Enter fullscreen mode Exit fullscreen mode

Tmux config

We have a tmux config file that is a dot config file. We can create it using the following command.

vi $HOME/.tmux.conf
Enter fullscreen mode Exit fullscreen mode

We can add the above command to allow mouse usage into the tmux conf file.

Tmux References

There is a lot more to tmux than this, which we can check out from the tmux manual.


Originally posted on vignesh.page.

Hope tmux improves your daily workflow as it did mine. Comment out any cool features or tmux plugins that you find useful.

. . . . . . . . . . . . . . . . . . .