Tmux

A part of my job involves connecting to a couple of Amazon EC2 instances(Linux servers) regularly to run commands, build, debug and deploy code. All along I’ve been using tabs terminal tabs to keep track of each server connection and the processes running in it. Using the terminal emulator’s tabs feature works okay for simple use cases but once you start running long running commands over SSH on multiple machines, you need something more powerful. Enter tmux.

Tmux is a terminal multiplexer, it allows you to create windows and panes in your terminal and it offers you the ability to run processes in named sessions.

Installation

Installing tmux is pretty straightforward on Linux and Mac. On Ubuntu and similar distros, do sudo apt-get install tmux or brew install tmux in Mac.

 

Running tmux

tmux follows a server/client model. When you run it, this starts the tmux server that keeps track of processes. To start a new tmux session type tmux into the terminal. You’ll know you’re in tmux when you see a green status bar at the bottom. This status bar shows important information about your system and your tmux session.

tmux terminal window

 

Commands in tmux are triggered by a prefix key followed by a command key. The default prefix key is C-b(Ctrl-b on Linux or Cmd-b on Mac). Pressing these two keys at the same time lets tmux know that you’re about to pass it a command.

Split screen

To split your screen vertically, the command to run is

Ctrl-B + %

This means press and hold down the Ctrl key, then press b at the same time. Release and press the percent symbol(SHIFT – 5). This will split your screen in half allowing you to run two sets of commands in the same window:

tmux split screen

To create a horizontal split, run:

Cntrl + b " That’s Ctrl and b at the same time then SHIFT and the quote symbol(“).

Sessions

I think the true power of tmux lies in its ability to create sessions. With sessions you can run a long running task, detach from the session and go about your business. tmux makes it easy to reconnect to the session.

To create a new tmux session, do
tmux new -s <session-name>. This creates and starts a session named “session-name”. In the example below, I create a session named “test”. Note that the name of the session appears in the status bar on the bottom left.
tmux session

To detach from a session, do Ctrl-b d To re-attach to the named session do
tmux attach -t <session-name> The t flag stands for target, but I like to read the command as “attach to <session-name>”.

Other commands

There is much more you can do with tmux and I barely scratched the surface in this blog post, but the commands above cover the most basic tasks. Below is a quick reference of common tmux commands:

  • Ctrl + b + c     Create a new window
  • Ctrl + b + w    List windows
  • Ctrl + b + 0-9    Switch to window number
  • Ctrl + b + ,    Rename a window
  • Ctrl + b + %     Split window vertically
  • Ctrl + b + "     Split window horizontally
  • Ctrl + b + O    Go to next pane
  • Ctrl + b + ;     Toggle between current and previous pane
  • Ctrl + b + x     Close current pane
  • Ctrl + b + ARROW KEY Move between windows using arrow keys
  • Ctrl + b + [ Scroll mode