Tmux: Difference between revisions

From Leo's Notes
This page was last edited on 30 July 2020, at 16:40.
Line 1: Line 1:
tmux is a terminal multiplexer similar to [[Screen]] but with some cool features such as split panes.
tmux is a terminal multiplexer similar to [[Screen]] but with some cool features such as split panes.


== Quick Usage ==
==Quick Usage==
When starting tmux, you create a new session. Each session can have multiple windows (numbered 0..N) and acts like tabs in a web browser. Each window can split into multiple panes and organized in a grid.
When starting tmux, you create a new session. Each session can have multiple windows (numbered 0..N) and acts like tabs in a web browser. Each window can split into multiple panes and organized in a grid.


Tmux Command Usage:
Tmux Command Usage:
{| class="wikitable"
{| class="wikitable"
! width="30%" | Command
! width="30%" |Command
! Description
!Description
|-
|-
| tmux ls
|tmux ls
| Lists all tmux sessions
|Lists all tmux sessions
|-
|-
| tmux new -s session-name
|tmux new -s session-name
| Creates a new session named session-name
|Creates a new session named session-name
|-
|-
| <nowiki>tmux a|attach N</nowiki>
|<nowiki>tmux a|attach N</nowiki>
| Attaches to an existing session
|Attaches to an existing session
|-
|-
| <nowiki>tmux a|attach -t session-name</nowiki>
|<nowiki>tmux a|attach -t session-name</nowiki>
| Attaches to an existing session by name
|Attaches to an existing session by name
|-
|-
| tmux kill-session -t session-name
|<nowiki>tmux a|attach -dt session-name</nowiki>
| Kills a session by name
|The <code>-d</code> option detaches any other attached sessions.
|-
|-
| tmux kill N
|tmux kill-session -t session-name
| Kills a session by ID
|Kills a session by name
|-
|tmux kill N
|Kills a session by ID
|}
|}


=== Tmux Commands ===
===Tmux Commands===
All commands to tmux are prefixed with a key {{code|Ctrl-b}} (as opposed to {{code|Ctrl-a}} in screen).  The default key bindings are quite unintuitive but can be changed to something a bit more usable. The tables below will show both the default and modified key bindings.
All commands to tmux are prefixed with a key {{code|Ctrl-b}} (as opposed to {{code|Ctrl-a}} in screen).  The default key bindings are quite unintuitive but can be changed to something a bit more usable. The tables below will show both the default and modified key bindings.


General commands:
General commands:
{| class="wikitable"
{| class="wikitable"
! width="10%" | Default Command
! width="10%" |Default Command
! width="10%" | Modified Command
! width="10%" |Modified Command
! Description
!Description
|-
|-
| colspan="2" | d
| colspan="2" |d
| Detach from this session
|Detach from this session
|-
|-
| colspan="2" | $
| colspan="2" |$
| Rename current session
|Rename current session
|-
|-
| colspan="2" | s
| colspan="2" |s
| Lists all sessions
|Lists all sessions
|-
|-
| colspan="2" | t
| colspan="2" |t
| Shows a clock in the current pane
|Shows a clock in the current pane
|-
|-
| colspan="2" | ?  
| colspan="2" |?
| Shows key bindings
|Shows key bindings
|-
|-
| colspan="2" | :
| colspan="2" | :
| Enters the tmux command prompt, where you can enter commands
|Enters the tmux command prompt, where you can enter commands
|-
|-
| [
|[
| /
|/
| Enter copy mode. You can use {{code|Vim}} key bindings if you set the {{code|mode-keys}} in the settings (see below). Exit copy mode by pressing {{code|Enter}}.
|Enter copy mode. You can use {{code|Vim}} key bindings if you set the {{code|mode-keys}} in the settings (see below). Exit copy mode by pressing {{code|Enter}}.
|-
|-
|  
|
| S
|S
| Synchronize the input to all panes in the current window. Manually done with {{code|:setw synchronize-panes}}.
|Synchronize the input to all panes in the current window. Manually done with {{code|:setw synchronize-panes}}.
|-
|-
| colspan="2" | w  
| colspan="2" |w
| Lists windows (it's also displayed at bottom)
|Lists windows (it's also displayed at bottom)
|-
|-
|
|
| `
|`
| Sends '~.' to stop remote SSH in current pane.
|Sends '~.' to stop remote SSH in current pane.
|}
|}


Windows (tab) commands:
Windows (tab) commands:
{| class="wikitable"
{| class="wikitable"
! width="10%" | Default Command
! width="10%" |Default Command
! width="10%" | Modified Command
! width="10%" |Modified Command
! Description
!Description
|-
|-
| c
|c
| n
|n
| Creates a new window
|Creates a new window
|-
|-
| n
|n
| ,
|,
| Go to next window
|Go to next window
|-
|-
| p
|p
| .
|.
| Go to previous window
|Go to previous window
|-
|-
| &
|&
| X
|X
| Kills the current window
|Kills the current window
|-
|-
| ,
|,
| r
|r
| Rename the current window
|Rename the current window
|-
|-
|  
|
| < >
|< >
| Reorder windows
|Reorder windows
|}
|}


Panes (split) commands. Enable mouse (see below) to make switching and resizing panes easy.
Panes (split) commands. Enable mouse (see below) to make switching and resizing panes easy.
{| class="wikitable"
{| class="wikitable"
! width="10%" | Default Command
! width="10%" |Default Command
! width="10%" | Modified Command
! width="10%" |Modified Command
! Description
!Description
|-
|-
| %
|%
| b (bisect)
|b (bisect)
| Horizontal split (% is like 0/0, splits left/right)
|Horizontal split (% is like 0/0, splits left/right)
|-
|-
| "
|"
| v
|v
| Vertical split
|Vertical split
|-
|-
| colspan="2" | x
| colspan="2" |x
| Close pane
|Close pane
|-
|-
| colspan="2" | !
| colspan="2" |!
| Close all other panes
|Close all other panes
|-
|-
| o
|o
| JKHL
|JKHL
| Swap panes (modified bindings swap in the desired direction)
|Swap panes (modified bindings swap in the desired direction)
|-
|-
| colspan="2" | q
| colspan="2" |q
| Show pane numbers
|Show pane numbers
|-
|-
| colspan="2" | z
| colspan="2" |z
| 'Zooms' or maximizes the selected pane
|'Zooms' or maximizes the selected pane
|-
|-
|  
|
| jkhl
|jkhl
| Selects pane in direction. (Or use mouse)
|Selects pane in direction. (Or use mouse)
|}
|}


== Configuration ==
==Configuration==
Configuration can be set in {{code|~/.tmux.conf}} file. Some useful settings are given below.
Configuration can be set in {{code|~/.tmux.conf}} file. Some useful settings are given below.


Line 217: Line 220:
}}
}}


== See Also ==
==See Also==
* https://gist.github.com/MohamedAlaa/2961058
 
* [https://zserge.com/posts/tmux/ Tmux for mere mortals] with logical key bindings
*https://gist.github.com/MohamedAlaa/2961058
*[https://zserge.com/posts/tmux/ Tmux for mere mortals] with logical key bindings


{{Navbox Linux}}[[Category:Linux]]
{{Navbox Linux}}
[[Category:Linux]]
[[Category:LinuxUtilities]]
[[Category:LinuxUtilities]]

Revision as of 16:40, 30 July 2020

tmux is a terminal multiplexer similar to Screen but with some cool features such as split panes.

Quick Usage

When starting tmux, you create a new session. Each session can have multiple windows (numbered 0..N) and acts like tabs in a web browser. Each window can split into multiple panes and organized in a grid.

Tmux Command Usage:

Command Description
tmux ls Lists all tmux sessions
tmux new -s session-name Creates a new session named session-name
tmux a|attach N Attaches to an existing session
tmux a|attach -t session-name Attaches to an existing session by name
tmux a|attach -dt session-name The -d option detaches any other attached sessions.
tmux kill-session -t session-name Kills a session by name
tmux kill N Kills a session by ID

Tmux Commands

All commands to tmux are prefixed with a key Ctrl-b (as opposed to Ctrl-a in screen). The default key bindings are quite unintuitive but can be changed to something a bit more usable. The tables below will show both the default and modified key bindings.

General commands:

Default Command Modified Command Description
d Detach from this session
$ Rename current session
s Lists all sessions
t Shows a clock in the current pane
? Shows key bindings
: Enters the tmux command prompt, where you can enter commands
[ / Enter copy mode. You can use Vim key bindings if you set the mode-keys in the settings (see below). Exit copy mode by pressing Enter.
S Synchronize the input to all panes in the current window. Manually done with :setw synchronize-panes.
w Lists windows (it's also displayed at bottom)
` Sends '~.' to stop remote SSH in current pane.

Windows (tab) commands:

Default Command Modified Command Description
c n Creates a new window
n , Go to next window
p . Go to previous window
& X Kills the current window
, r Rename the current window
< > Reorder windows

Panes (split) commands. Enable mouse (see below) to make switching and resizing panes easy.

Default Command Modified Command Description
% b (bisect) Horizontal split (% is like 0/0, splits left/right)
" v Vertical split
x Close pane
! Close all other panes
o JKHL Swap panes (modified bindings swap in the desired direction)
q Show pane numbers
z 'Zooms' or maximizes the selected pane
jkhl Selects pane in direction. (Or use mouse)

Configuration

Configuration can be set in ~/.tmux.conf file. Some useful settings are given below.

# Enable mouse, useful for selecting or resizing panes
set-option -g mouse on

# Enable Vim key bindings in copy-mode
setw -g mode-keys vi

set -g default-terminal screen-256color
set -g history-limit 20000
set -g base-index 1
set-option -g renumber-windows on
set -s escape-time 0
set -ga terminal-overrides ',xterm*:smcup@:rmcup@'

# Pane names on the borders
set -g pane-border-status top
set -g pane-border-format "#P- #{pane_current_command}: #{pane_title}"

# Close ssh connection with .~ as Mod-`
bind ` send-keys "C-m" "~."

# make scrolling with wheels work
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M

# Reload configuration with Mod-R
bind R source-file ~/.tmux.conf \; display-message "  Config reloaded..".

# Synchronize inputs to all panes with Mod-S
bind S setw synchronize-panes \; display-message " Synchronize panes".

# Better key bindings for common actions
# These are listed as the modified key bindings in the cheat sheet at https://leo.leung.xyz/wiki/Tmux
bind n new-window -c "#{pane_current_path}"
bind . select-window -n
bind , select-window -p
bind < swap-window -t -1
bind > swap-window -t +1
bind Left swap-window -t -1
bind Right swap-window -t +1
bind X confirm-before "kill-window"
bind x confirm-before "kill-pane"
bind v split-window -h -c "#{pane_current_path}"
bind b split-window -v -c "#{pane_current_path}"
bind r command-prompt -I "" "rename-window '%%'"
bind / copy-mode
bind h select-pane -L
bind l select-pane -R
bind k select-pane -U
bind j select-pane -D
bind H run-shell 'old=`tmux display -p "#{pane_index}"`; tmux select-pane -L; tmux swap-pane -t $old'
bind J run-shell 'old=`tmux display -p "#{pane_index}"`; tmux select-pane -D; tmux swap-pane -t $old'
bind K run-shell 'old=`tmux display -p "#{pane_index}"`; tmux select-pane -U; tmux swap-pane -t $old'
bind L run-shell 'old=`tmux display -p "#{pane_index}"`; tmux select-pane -R; tmux swap-pane -t $old'

# status bar colors
set -g status-bg black
set -g status-fg white

# Left status session name
set-option -g status-left 'Session: #[bg=black,fg=cyan]#S#[fg=white]  Windows: '
set-option -g status-left-length 35

# window list options
# alignment settings of window list in status line
set-option -g status-justify left
setw -g automatic-rename on
set-window-option -g window-status-format '#[fg=cyan,dim]#I#[fg=blue]:#[default]#W#[fg=grey,dim]#F'
set-window-option -g window-status-current-format '#[bg=blue,fg=cyan,bold]#I#[bg=blue,fg=cyan]:#[fg=colour230]#W#[fg=dim]#F'
set -g base-index 1

# Redraw every second so clock is correct
set -g status-interval 1
set -g status-right '#[fg=yellow]#(whoami)#[fg=default]@#[fg=green]#h#[fg=white]

See Also