I haven’t been using tmux since I’m a happy user of iTerm2. Eventhough tmux had been used in servers I used to work with, I preferred avoiding it because of the fallacious assumption that it would be hard to remember all those scary key-shortcuts. I gave it a try today, and totally regret for not learning it earlier.
Why tmux?
tmux is a terminal multiplexer. It enables to run multiple processes in one terminal.
With tmux you can create multiple sessions in a terminal, each session can have multiple windows which can be split into panes. Sessions can be attached/detached to the working terminal. This is heavily useful for multiuser concurrent system-terminals.
Another advantage is that processes running inside tmux sessions can continue even after closing the terminal.
Installation
In OSX, tmux can be installed with homebrew.
$ brew install tmux
console
Typing tmux
will start a new session, you can see the session status displayed on the bottom bottom of the window in a green bar.
$ tmux
When you are inside tmux session, every tmux command need to have a prefix. By default it is CTRL+B
. For example, commands below does splitting window into vertical panes.
CTRL+B %
Here is a list of useful commands,
Prefix | Command | Use |
---|---|---|
CTRL+B |
Sessions | |
s | list sessions | |
:new<ENTER> |
create new session | |
$ | rename session | |
d | detach session | |
Windows | ||
w | list windows | |
f | find window | |
c | create window | |
& | kill window | |
, | rename window | |
n | next window | |
p | previous window | |
Panes | ||
% | vertical split | |
" | horizontal split | |
q | show pane numbers, if pane number is typed immediately, control is switched to that pane | |
x | kill pane | |
z | toggle zoom pane | |
<ARROW KEY> |
switch to pane pointed by arrow key | |
o | swap panes | |
{ | move current pane to left | |
} | move current pane to right | |
<SPACE BAR> |
toggle between different pane layouts | |
Extras | ||
t | show time | |
? | list shortcuts | |
: | prompt |
As mentioned in the last entry of table above, CTRL+B :
will give tmux prompt at the bottom of the window, you can enter commands there.
: <command>
Command | Use | |
---|---|---|
:setw synchronize-panes | synchronize cursor on all panes (repeat same for toggle) | |
:resize-pane | -L | resize current pane to left (Available flags: L, R, T, D for left, right, top, down respectively) |
-D 50 | resize current pane down by 50 cells | |
-t 2 -R 30 | resize pane number 2 right by 30 cells |
Managing sessions
Sessions are by default numbered as 0, 1, 2, .. You can name/rename sessions with strings.
Action | Command |
---|---|
create session |
$ tmux $ tmux new $ tmux new -s <session_name>
|
list sessions | $ tmux ls |
detach session | CTRL+B d |
attach session |
$ tmux a -t <session_name>
|
kill session | $ tmux kill-session -t <session_name> |
Configuration file
If you want to save commands entered in tmux prompt to for all of your sessions, it can be saved to configuration file ~/.tmux.conf
Here is an example config file:
# enable mouse actions
set -g mouse on
# change default PREFIX from CTRL+B to CTRL+A
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
# reload config file inside session with PREFIX+r
bind r source-file ~/.tmux.conf
# Change theme of pane border
set -g pane-border-fg red
set -g pane-active-border-fg yellow
For more options: man tmux