tmux is a terminal multiplexer. Its 3-level architecture "session-window-pane" is very flexible and powerful. Sessions are independent. Windows are different "tabs" in a session. And panes are different parts in a window.
The multiplexer means if different hosts attach to the same session, they share the same screen, the same input and output, which like they sit together, use the same computer, so making pair-programming a breeze.
You can detach from a session while it's still kept by tmux server in background. This is very useful on servers. You use laptop attaching to a tmux server running a server machine, work for a while, then detach from this server, shutdown your laptop. Next time when you attach to it, all the works are kept from last detach, as if you didn't leave them.
You can move window between sessions, which is convinient for collaboration. For example, Bob works on session "bob" and Tom works on "tom", Bob write some codes and want Tom to complete it. Then he can move his window to Tom's session. If he want pair-programming with Tom, he can tell Tom to detach from his own session "tom" and attach to "bob". When finished, Tom detach from "bob" and attach to "tom" to continue his work.
Ref: "tmux - Productive Mouse-Free Development" by Brian P. Hogan.
Install
With internet connected, install tmux with sudo yum install tmux
or sudo apt-get install tmux
.
Without internet connection,
download tmux package on RepoForge,
then install it with sudo rpm -ivh tmux-1.6-1.el6.rf.x86_64.rpm
.
Getting help
List all binding keys
tmux list-keys
or use shortcuts: "bind-key ?".
So if you know a shortcut, say [prefix]!, and want know the corresponding command, you can run
tmux list-keys|grep !
then you get
bind-key ! break-pane
So the corresponding command is "break-pane".
List all commands
tmux list-commands
Define tmux startup shortcuts in Xfce, Mint
Application Finder(Alt-F3) -> Keyboard -> Application shortcuts: Add Command: exo-open --launch TerminalEmulator xfce4-terminal --fullscreen -x tmux new -s basic Shortcut: Alt-[
Frequently used shortcuts
According to the defualt shortcuts and above modifications:
-
List all shortcuts: prefix ?
-
List existing sessions: tmux ls
-
Attach to a session: tmux attach -t
, if there's only one window, you can use tmux a
for short; -
Switch to next window: prefix n
-
Jump to last window: prefix l (easy for jump between 2 windows)
-
Detach from a session: prefix d
-
Jump to previous/next session: prefix (/)
-
Jump to last session: prefix L (easy for jump between 2 sessions)
-
Create new window: prefix c
-
Rename current window: prefix
-
Jump to a window according to it's number: prefix
(see tmux's status line to get its number) -
Split current window horizontally/vertically: prefix -/|
-
Jump to another pane(sub-window): prefix h/j/k/l
-
Jump to next pane: prefix o
-
Maximum/Restore a pane: prefix z (tmux version >= 1.8, use
tmux -V
to get the version) -
Move window to another session: prefix .(period)
-
Switch to next layout: prefix
Window-Pane Management
You can run tmux ...
directly within tmux-managed sessions and windows,
no need to detach from tmux environment beforehand.
The only exception is tmux attach
,
which will produce a recursive attached tmux session, and messed up things.
All command of tmux has auto-completion, so it's very convenient to run tmux command within tmux window.
The equivalent way of running tmux command without command-line(like that in vi) is using shortcut "prefix :".
Turning a window into a pane
-
In the destination window:
tmux join-pane -s [source-window-number]
; -
In the source window:
tmux join-pane -t [destination-window-number]
; -
Specify source and destination windows both:
tmux join-pane -s [src-window-number] -t [dst-window-number]
;
Turning a pane into a window
Use shortcuts "prefix, !" in the pane.
Keep the window name fixed
Add "set-option -g allow-rename off" into ~/.tmux.conf.
Kill zombie window
tmux kill-window -t [n]
, where [n] is the window number.
Clipboard Management
Select/copy/paste text with shortcuts
Add the following lines into config file (updated for tmux version above 2.4):
# Copy mode
unbind [
unbind ]
bind -n M-c copy-mode
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection
bind-key -T copy-mode-vi r send-keys -X rectangle-toggle
bind p paste-buffer
Here I modify default key of opening copy mode to M-c,
move cursor with vi keys (h/j/k/l), select text with v
,
copy with y
and paste with prefix+p.
Here -T
means "key table". Search bind-key
in man tmux
for details.
Ref: Everything you need to know about Tmux copy paste
The following is DEPRECATED for old version tmux:
To enable copy/paste between tmux buffer and system clipboard, install xsel, and add codes below into ~/.tmux.conf:
bind -t vi-copy y copy-pipe "xsel -i -p -b"
bind p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"
Here "bind" is the alias of "bind-key". "-t" specifies the type is "vi-copy" instead of "emacs-copy". In copy mode of tmux, use "y" to copy selected text into system clipboard, then paste the text with Ctrl-V in GUI application, or Shift-Ctrl-V in terminal, or "p" in vim.
Ref:
-
X clipboard integration on ArchLinux Wiki.
Path share between windows
I want to copy all files in window #1 to directory in window #2. The workflow is:
prefix 2
tmux setb $PWD
prefix 1
cp * [prefix p]/
The trick is using tmux setb
to copy working directory to buffer of tmux,
then use [prefix p] to paste the path into the copy command.
Don't forget the trailing slash at the end of cp command, it's necessary.
If parcellite is installed, there is a little simpler way to achieve this:
prefix 2
pwd|parcellite
prefix 1
cp * [Ctrl-Alt-v]/
You can use ls -l|parcellite
to add command output to parcellite buffer.
Multiple Buffers Copy & Paste
tmux maintains a stack of copied buffers. So you can copy multiple times, and paste selected buffers in arbitrary order, with the help of following commands: list-buffer, show-buffer, choose-buffer, save-buffer.
Copy all the texts in the current pane with "capture-pane"(not including scrolled out texts).
Customize status bar
Add battery status in tmux status bar
Add the following line into ~/.tmux.conf:
set -g status-right "%R #[fg=black bg=white]%Y %b %d#[fg=white bg=black] #(/home/chad/apps/getBatteryStuatus)"
And create a shell script named "getBatteryStuatus" under /home/chad/apps:
upower -i /org/freedesktop/UPower/devices/battery_BAT0|awk '/^\s*percentage:/ {print $2}'
Disable Mouse Scrolling
Enable mouse scrolling will copy selected text into tmux clipboard. So it's impossible to copy text in tmux and paste it outside tmux environment.
To disable it, add "set -g mode-mouse off" into ~/.tmux.conf.
To scrolling window in tmux, first go into copy mode, then use Ctrl-K/J to move up/down.
To make this smooth, add the following into .tmux.conf:
# Copy mode
unbind [
bind -n C-j copy-mode
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
Deal with Nested Tmux Session
If you run tmux a
inside a tmux window, you attach to a nested tmux session.
If the prefix definition of both tmux are the same, you can't send keys to inner session directly.
For example, if you press "prefix d", if will detach from the outer session.
How to detach from inner session?
First press "prefix :", you get the tmux command prompt of outer session.
Then input "send-prefix", press
Or you can use "send-keys" command.
First press "prefix :", you get the tmux command prompt of outer session, like above.
Then input "send-keys M-q d", press
The format of keys after "send-keys" is defined in section "KEY BINDINGS" in man tmux
.
Make tmux more convenient
Add the follow aliases into ~/.bash_aliases:
alias tl='tmux ls'
alias ta='tmux attach -t'
You can use ta
.
Get tmux version: tmux -V
.
My configuration file
$ cat ~/.tmux.conf
# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"
# run zsh
# set-option -g default-command "reattach-to-user-namespace -l $SHELL -l"
# disable mouse scroll
set -g mode-mouse off
# fix the window name
set-option -g allow-rename off
# Setting the prefix from C-b to Alt-f
set -g prefix M-q
# Free the original Ctrl-b prefix keybinding
unbind C-b
# quick window switching, -n means no need to use prefix
bind -n C-h select-window -t :-
bind -n C-l select-window -t :+
# quick window creation
bind -n C-k new-window
# quick pane expansion
# bind -n M-u break-pane
# quick window joining
bind -n M-i command-prompt "join-pane -s %1"
# enable vi keys.
setw -g mode-keys vi
# Set the base index for windows to 1 instead of 0
set -g base-index 1
# Set the base index for panes to 1 instead of 0
setw -g pane-base-index 1
# setting the delay between prefix and command
set -sg escape-time 1
#set -sg repeat-time 1000
# Reload the file with Prefix r
bind r source-file ~/.tmux.conf \; display "Reloaded!"
# Split window
bind | split-window -h
bind - split-window -v
# moving between panes
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Pane resizing, -r means you can repeat this key bindings
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# Copy mode
unbind [
bind -n C-j copy-mode
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Update the status bar every sixty seconds
set -g status-interval 60
# set the status line's colors
set -g status-fg white
set -g status-bg black
# Status line left side
set -g status-left-length 40
set -g status-left "#[fg=green]session: #S #[fg=yellow]#I #[fg=cyan]#P"
set -g status-utf8 on
# enable activity alerts
setw -g monitor-activity on
set -g visual-activity off
# Center the window list
set -g status-justify centre
# set the color of the window list
setw -g window-status-fg cyan
setw -g window-status-bg default
setw -g window-status-attr dim
# set colors for the active window tag in window list
setw -g window-status-current-fg white
setw -g window-status-current-bg blue
setw -g window-status-current-attr bright
# Status line right side
set -g status-right "%R #[fg=black bg=white]%F#[fg=white bg=black] #(/home/chad/apps/myscripts/getBatteryStuatus)"
#set -g status-right "%R #[fg=black bg=white]%Y %b %d"
# setw -g status-right-attr bright
# Command / message line
set -g message-fg white
set -g message-bg black
set -g message-attr bright
# Execute a command in a new window
bind C command-prompt "new-window -n %1 '%1'"
bind S command-prompt "new-window -n %1 'ssh %1'"
The last line shows that you can use parameters in key bindings. When you press prefix S, tmux propmt you to input a parameter, I input "47" and press
If your .tmux.conf doesn't take effect, run the following command to load config file explicitly (or find errors in file):
tmux new
tmux source-file ~/.tmux.conf
The format strings in status line are interpreted as the followings:
-
%X: date-time sign, such as %R is time, %F is date, etc. See full list in "FORMAT controls the output" section in
man date
; -
...: maybe the output of a shell command, or color of foreground/background, or an attribute. See full list in "status-left" of
man tmux
. These meta-data can be used both in status-left and status-right;