100 Useful Command-Line Utilities

by Oliver; 2014

66. tmux

Note: tmux is not a default shell program. You may have to download and install it.

As discussed in An Introduction to the Command-Line (on Unix-like systems) - tmux, tmux is a more modern version of screen, which all the cool kids are using. In tmux there are:
  • sessions
  • windows
  • panes
Sessions are groupings of tmux windows. For most purposes, you only need one session. Within each session, we can have multiple "windows" which you see as tabs on the bottom of your screen (i.e., they're virtual windows, not the kind you'd create on a Mac by typing ⌘N). You can further subdivide windows into panes, although this can get hairy fast. Here's an example of a tmux session containing four windows (the tabs on the bottom), where the active window contains 3 panes:

image

I'm showing off by logging into three different computers—home, work, and Amazon.

Start a new session:
$ tmux
Detach from your session: Leader d, where the leader sequence is Cntrl-b by default.

List your sessions:
$ tmux ls
0: 4 windows (created Fri Sep 12 16:52:30 2014) [177x37]
Attach to a session:
$ tmux attach			# attach to the first session
$ tmux attach -t 0		# attach to a session by id
Kill a session:
$ tmux kill-session -t 0	# kill a session by id
Here's a more systematic list of commands:

<Leader> cCreate a new window
<Leader> xKill the current window
<Leader> nGo to the next window
<Leader> pGo to the previous window
<Leader> lGo to the last-seen window
<Leader> 0-9Go to a window numbered 0-9
<Leader> wList windows
<Leader> sToggle between sessions
<Leader> dDetach the session
<Leader> ,Set a window's title
<Leader> [Start copy mode (which will obey vim conventions per order of the config file)
<Leader> ]Paste what you grabbed in copy mode
<Leader> %Spawn new horizontal pane (within the window)
<Leader> "Spawn new vertical pane (within the window)
<Leader> oGo to the next pane (within the window)
<Leader> o (pressed simultaneously*)Swap position of panes (within the window)
<Leader> qNumber the panes (within the window) - whereupon you can jump to a specific pane by pressing its numerical index
<Leader> zToggle the expansion of one of the panes (within the window)
<Leader> tDisplay the time
<Leader> mTurn mouse mode on (allows you to resize panes with the mouse)
<Leader> MTurn mouse mode off
<Leader> :Enter command mode

* With tmux, in contrast to screen, you give a slightly longer pause after pressing the Leader sequence. In this particular case, however, they are pressed simultaneously.

As before, I like to use Cntrl-f as my Leader sequence. Here's a sample .tmux.conf file, which accomplishes this, among other useful things:
### Default Behavior
# Use screen key binding
set-option -g prefix C-f

# 1-based numbering
set -g base-index 1

# Fast command sequence - cause some error
# set -s escape-time 0

# Set scrollback to 10000 lines
set -g history-limit 10000

# Agressive window
setw -g aggressive-resize on

### Views
# Highlight Active Window
set-window-option -g window-status-current-bg red

# Status Bar
set -g status-bg black
set -g status-fg white
set -g status-left ""
set -g status-right "#[fg=green]#H"

### Shortcut
# Last Active Window
bind-key C-a last-window

# set -g mode-mouse on

# vi bindings in copy mode
set-window-option -g mode-keys vi

# Tmux window size ( http://superuser.com/questions/238702/maximizing-a-pane-in-tmux ) 
unbind +
bind + new-window -d -n tmux-zoom 'clear && echo TMUX ZOOM && read' \; swap-pane -s tmux-zoom.0 \; select-window -t tmux-zoom
unbind -
bind - last-window \; swap-pane -s tmux-zoom.0 \; kill-window -t tmux-zoom
To reload .tmux.conf after making changes: <Leader> :source-file ~/.tmux.conf

To copy and paste text, assuming vi bindings:
  • first enter copy mode with <Leader> [
  • copy text with space to enter visual mode, then select text, then use y to yank, then hit Enter
  • paste text with <Leader> ]
Occasionally, you may find yourself using tmux within tmux. Suppose you're running a tmux session on your current computer and in one of your windows you ssh into another computer, which is also running tmux. In this window, you run:
$ tmux attach
and now you're running tmux within tmux. To execute tmux commands on the inner session, use Cntrl-b after the <Leader> sequence. For example, to list your windows on the inner session, you'd type <Leader> Cntrl-b w.

tmux is amazing—I've gone from newbie to cheerleader in about 30 seconds!

Hat tip: Albert

<PREV   NEXT>