跳至內容
出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯,請訪問簡體中文翻譯團隊

附註: 已同步最新文本,正在翻譯。(在 Talk:Tmux# 中討論)

tmux 是終端多路復用器,可在一個屏幕中創建、訪問並控制多個終端(或窗口),每個終端或窗口內都可以運行獨立的程序。將 tmux 從當前窗口分離(tmux detach)後,tmux 依然可以在後台運行,直到恢復會話(tmux attach)。 使用 ISC 協議的 tmux 是 GNU Screen 的替代品。儘管兩者相似,但依然有許多不同之處(參考 tmux FAQ page)。

安裝

安裝 tmux 軟體包。 可選擇安裝 tmux-bash-completion-gitAUR 軟體包用於提供 bash 補全功能。

配置

3.2 版本起,tmux 默認從 $XDG_CONFIG_HOME/tmux/tmux.conf,然後是 ~/.config/tmux/tmux.conf 尋找用戶配置文件。全局配置文件位於 /etc/tmux.conf,但 Archlinux 並無此文件。

快捷鍵綁定

命令快捷鍵的前綴默認是 Ctrl+b

提示:可以使用默認的幫助快捷鍵 Ctrl+b ? 查詢所有可用的快捷鍵綁定。

要垂直分割一個窗口,請按下 Ctrl+b %

將窗口分割成多個面板(panes)後,通過按下命令前綴(例如 Ctrl+b),然後按住 Ctrl 不放的同時,按下方向鍵 LeftRightUpDown 調整面板大小。交換不同的面板與此類似,只需將調整大小環節中所按的方向鍵改為 o 鍵即可。

按鍵綁定可以在 tmux.conf 配置文件中通過 bind 與 unbind 命令修改。例如,將下面的示例配置添加到用戶配置文件中可將默認的前綴綁定 Ctrl+b 更改為 Ctrl+a

unbind C-b
set -g prefix C-a
bind C-a send-prefix
提示:使用特殊字符作為前綴時,請用 Alt(也被稱為 Meta)替換 Ctrl,例如:set -g prefix m-'\'

使用 Ctrl+b c 創建一個新的窗口(window),通過快捷鍵 Ctrl+b nCtrl+b p 切換到下一個或上一個窗口。

下面還有一些用於在窗口間移動的其他方法:

Ctrl+b l(移动到上一个使用的窗口)
Ctrl+b w(列出所有窗口与窗口编号)
Ctrl+b <窗口编号>(移动到指定编号的窗口,默认编号是 0 到 9)
Ctrl+b q(显示面板编号,在面板编号显示的同时按下对应编号可以切换到该面板)

tmux 有窗口搜索選項與快捷鍵綁定,便於在多個窗口之間切換:

Ctrl+b f <窗口名称>(按名称搜索窗口)
Ctrl+b w(从交互式窗口列表中选择窗口)

複製模式

tmux 中的窗口可能會處於幾種模式中的一種。默認的模式下可以直接訪問窗口中的終端。另一種模式是「複製模式」。在複製模式下用戶可以瀏覽緩衝區、滾動瀏覽歷史。複製模式下所使用的快捷鍵是 vi 或是 emacs 風格的。除非用戶在 VISUAL 或是 EDITOR 變量中設置了 vi,否則複製模式中默認的快捷鍵綁定是 emacs 風格。

按如下快捷鍵進入複製模式:

Ctrl+b [

然後就可以像在自己的編輯器裡一樣瀏覽緩衝區內容了。

使用如下之一的快捷鍵退出複製模式:

vi 模式下請使用:

q

emacs 模式下則是:

Esc

打開 URL

安裝並配置好 urlviewAUR 後才能在 tmux 中打開 URL。以下是配置示例:

在一個新的終端中:

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; run-shell "$TERMINAL -e urlview /tmp/tmux-buffer"

在一個新的 tmux 窗口中(無需打開新的終端):

bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'

設置正確的終端

256 色

如果用戶使用的是 256 色終端,應在 tmux 中設置正確的終端如:tmuxtmux-256color

tmux.conf
set -g default-terminal "tmux-256color"

也可以使用 tmux -2 強制讓 tmux 認定終端支持 256 色防止顯示混亂。

24 位深顏色

tmux 支持 24 位深顏色。如果用戶使用的終端支持這一特性(請參考這個連結),請將它添加至 terminal-features

例如,對於 Alacritty 終端,請添加以下內容:

set -as terminal-features ",alacritty*:RGB"

對於其他終端,請使用 $TERM 的值替換上述的 alacritty

關於 RGB 的詳細信息,請查閱 tmux(1)

xterm-keys

將下面這一行內容添加至配置文件以啟用 xterm-keys:

tmux.conf
set-option -g xterm-keys on

注意,如果在 tmux.conf 中啟用了 xterm-keys,用戶需要自行構建一個自定義的 terminfo 用於聲明新的轉解碼,否則應用程式將識別不到。使用 tic 編譯以下內容之後,就可以將「xterm-screen-256color」作為 TERM 使用了:

# 一个以 screen- 前缀命名的 TERMINFO,
# 用于声明由 tmux 配置“set-window-option -g xterm-keys”所启用的转译序列。
#
# 以 xterm- 前缀命名,因为一些应用程序除了检查终端功能,
# 还会检查 TERM 名称。
xterm-screen-256color|GNU Screen with 256 colors bce and tmux xterm-keys,

# As of Nov'11, the below keys are picked up by
# .../tmux/blob/master/trunk/xterm-keys.c:
	kDC=\E[3;2~, kEND=\E[1;2F, kHOM=\E[1;2H,
	kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~, kPRV=\E[5;2~,
	kRIT=\E[1;2C,

# 如果所用的终端不支持 bce,
# 请将下面的内容改为 screen-256color:
	use=screen-256color-bce,

使用 tic -c 檢查所使用的終端是否支持 bce:

$ tic -c xterm-screen-256color 
"xterm-screen-256color", line 16, terminal 'xterm-screen-256color': resolution of use=screen-256color-bce failed

將上述內容保存後使用 tic 編譯:

$ tic xterm-screen-256color 

文件編譯完成後會被保存到 $HOME/.terminfo(若以 root 運行,則會保存到 /usr/share/terminfo/ 中,可全局使用)。

定製主題

可以為 tmux 定製主題,但首先需要知道顏色代碼。在終端中執行以下命令,將輸出顏色代碼與其對應的顏色示例:

$ for i in {0..255}; do printf "\x1b[38;5;${i}mcolor${i} - ██████████\n"; done

上述命令所輸出的顏色代碼可用於修改 tmux 的顏色主題。下面是一個修改狀態欄顏色的示例:

# 状态栏颜色
set -g status-bg "color4"         # 蓝色背景
set -g status-fg "color7"         # 灰色文字
set -g status-right "%l:%M %p"    # 时间格式
set-window-option -g window-status-current-style "bg=color75,fg=color231 bold"    # 当前窗口的背景和前景色

面板邊框也可以通過以下的方式設置主題顏色:

# 边框颜色
set -g pane-border-style        fg="colour255"
set -g pane-active-border-style fg="colour33"

其他設置

限制滾動緩衝區上限至最多 10000 行:

set -g history-limit 10000

切換滑鼠支持:

bind-key m set-option -g mouse \; display "Mouse: #{?mouse,ON,OFF}"

隨 systemd 啟動

開機啟動 tmux 有如下的好處。例如,創建一個新的 tmux 會話時,開機啟動的 tmux 服務能縮減啟動延遲。

除此以外,如有特殊需求(例如需要加載大量的 tmux 配置項或是有共享的用戶會話),開機啟動 tmux 能夠在用戶不登錄的情況下恢復並持續運行這些會話。

下面這個服務為特定用戶(例如,啟動或啟用 tmux@username.service)啟動 tmux:

/etc/systemd/system/tmux@.service
[Unit]
Description=tmux session for user %I

[Service]
Type=forking
User=%I
# 警告:這裡請使用 %I 而不要使用 %u,在系統服務中 %u=root。
ExecStart=/usr/bin/tmux new-session -s %I -d
ExecStop=/usr/bin/tmux kill-session -t %I

[Install]
WantedBy=multi-user.target
提示:
  • 可以添加 WorkingDirectory=自定義路徑 以自定義工作目錄。若設置為 ~,將使用 User= 中指定的用戶家目錄。
  • 添加 -v 參數可在工作目錄中生成 tmux 客戶端和服務端的日誌。
  • 若希望引入圖形會話的相關環境變量,例如 DISPLAYWAYLAND_DISPLAYSESSION_MANAGER,請將 WantedBy 選項修改為 graphical-session.target,並在 [Unit] 下面添加 After=graphical-session.target
  • 也可以創建一個在桌面環境(也可以是窗口管理器或者 Wayland 混成器)啟動後自動執行的腳本,並使用 tmux setenv -g name [value] 加載並引入相關的環境變量。

也可以將此文件存放至 systemd/用戶 目錄中(此時不需要 User=%I,同時還需要將 WantedBy 中的 multi-user.target 修改為 default.target),例如 ~/.config/systemd/user/tmux.service。此時 tmux 服務會在用戶登錄時啟動,並且由於這個原因,用戶服務會保持運行狀態,登出並不會停止服務。除此以外還可以設置隨系統自動啟動_systemd_用戶實例

會話初始化

將下面的內容添加到 ~/.tmux.conf 中可在打開一個新的 tmux 會話時含有預加載的窗口:

new  -n WindowName Command
neww -n WindowName Command
neww -n WindowName Command

要讓打開的新會話包含多個分割窗口,請將 splitw 命令添加至需要分割的 neww 下方,像這樣:

new  -s SessionName -n WindowName Command
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1 
selectp -t 0

將會打開兩個窗口,其中第二個窗口的名稱是 foo/bar,且會被垂直平均分割成 foo 和 bar 上下兩個面板,foo 在 bar 的上方。窗口焦點會在窗口 2(foo/bar)內上方的面板(foo)中。

注意:會話、窗口和面板的序號都是從 0 開始,除非用戶在 .conf 中指定序號從 1 開始。

在配置文件中 source 不同的會話文件可以實現多個會話的管理:

# 初始化多个会话
bind F source-file ~/.tmux/foo
bind B source-file ~/.tmux/bar

剪貼板集成

提示:tmux 插件 tmux-yank 提供了類似的功能。

可以將 tmux 中選擇的內容複製到顯示服務的剪貼板(both primary/secondary selections),再粘貼回 tmux。下面的配置文件片段提供了將 X11 和 Wayland 剪貼板與當前的 tmux 集成的方法:

注意:下面的 Vim 風格示例中,需要使用 unbind p 解除「切換至上一個窗口」的快捷鍵的綁定,才能正常使用粘貼功能。

與 Xorg 集成

首先可以考慮使用 xsel

Emacs 風格:

bind-key -T copy-mode y send-keys -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
bind-key C-y run "xsel -o | tmux load-buffer - ; tmux paste-buffer"

Vim 風格:

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xsel -i -p && xsel -o -p | xsel -i -b"
bind-key p run "xsel -o | tmux load-buffer - ; tmux paste-buffer"

也可以使用 xclip 達到相同的目的。與 xsel 不同,xclip 在處理與目前區域設置(locale)不兼容的原始比特流時更好一些。儘管如此,使用 xsel 比 xclip 更乾淨整潔些,這是因為 xclip 在讀取 tmux 緩衝區內容後並不會關閉 STDOUT。這樣一來,tmux 就無法知道複製是否完成,並一直等待 xclip 終止,看起來就像是 tmux 不響應了一樣。解決方法是將 STDOUT 重定向到 /dev/null

Emacs 風格:

bind-key -T copy-mode y send-keys -X copy-pipe-and-cancel "xclip -i -sel clip > /dev/null"
bind-key C-y run "xclip -o -sel clip | tmux load-buffer - ; tmux paste-buffer"

Vim 風格:

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -sel clip > /dev/null"
bind-key p run "xclip -o -sel clip | tmux load-buffer - ; tmux paste-buffer"

與 Wayland 集成

首先請確保安裝了 wl-clipboard

Emacs 風格:

bind-key -T copy-mode y send-keys -X copy-pipe-and-cancel "wl-copy && wl-paste -n | wl-copy -p"
bind-key C-y run "wl-paste -n | tmux load-buffer - ; tmux paste-buffer"

Vim 風格:

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy && wl-paste -n | wl-copy -p"
bind-key p run "wl-paste -n | tmux load-buffer - ; tmux paste-buffer"

在 Urxvt 中使用中鍵粘貼

注意:啟用滑鼠支持後才能使用此功能。

tmux 有一個非官方的 perl 擴展(在 tmux 的官方 FAQ 中有提到),可在 urxvt 中通過滑鼠中鍵粘貼剪貼板內容。

首先,下載 perl 腳本並將其保存到 urxvt 的 perl 庫中:

wget http://anti.teamidiot.de/static/nei/*/Code/urxvt/osc-xterm-clipboard
mv osc-xterm-clipboard /usr/lib/urxvt/perl/

同時還需要在 .Xdefaults 中啟用 perl 腳本:

~/.Xdefaults
...
*URxvt.perl-ext-common:		osc-xterm-clipboard
...

接下來,需要讓 tmux 知道這個新功能,並啟用滑鼠支持(如果還未啟用):

~/.tmux.conf
...
set-option -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'
set -g mouse on
...

這樣就完成了。請結束所有 tmux 進程後再嘗試新添加的中鍵功能。

在 tmux 中,Shift+MiddleMouseClick 會粘貼剪貼板內容,而僅用滑鼠中鍵會粘貼 tmux 緩衝區中的剪貼板內容。

在 tmux 外部,僅用滑鼠中鍵會粘貼 tmux 緩衝區內容,而標準 Ctrl+c用於複製。

提示與技巧

以默認的會話布局啟動 tmux

tmuxinatortmuxp 這樣的會話管理器能簡化通常的會話配置。

要使用 tmuxinator,請安裝 tmuxinatorAUR。安裝完成後嘗試運行以下命令測試安裝是否成功:

$ tmuxinator doctor

獲取默認布局參數

首先,根據個人的喜好啟動並配置 tmux 窗口和面板。完成後,在當前會話內,執行下面的命令獲取當前的布局參數值:

tmux list-windows

命令的輸出可能會像下面這樣(比如兩個窗口,分別有 3 個和 2 個面板的布局):

0: default* (3 panes) [274x83] [layout 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}] @2 (active)
1: remote- (2 panes) [274x83] [layout e3d3,274x83,0,0[274x41,0,0,4,274x41,0,42,7]] @3                                         

這其中需要複製並在這之後使用到的部分,是從 [layout... 之後開始到 ... ] @2 (active) 之前的部分。對於上面的例子,第一個窗口的布局對應的內容是 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}

定義默認的 tmux 布局

在上一節中獲取到需要的布局信息後,就可以退出當前的 tmux 會話了。然後編輯 tmuxinator 的配置文件來創建自定義的默認 tmux 會話布局(不要複製示例,用上一節獲取到的布局信息):

~/.tmuxinator/default.yml
name: default
root: ~/
windows:
  - default:
      layout: 20a0,274x83,0,0{137x83,0,0,3,136x83,138,0[136x41,138,0,5,136x41,138,42,6]}
      panes:
        - clear
        - vim
        - clear && emacs -nw
  - remote:
      layout: 24ab,274x83,0,0{137x83,0,0,3,136x83,138,0,4}
      panes:
        - 
        - 

上面的示例定義了兩個窗口(名稱分別是「default」和「remote」)和用戶定義的布局參數值。每個面板設置都需要至少一行 -。tmux 啟動時,第一個窗口中的第一個面板中執行了「clear」,第二個面板中是「vim」,第三個面板中執行了「clear && emacs -nw」。第二個窗口中的兩個面板在啟動時都沒有執行任何命令。

使用如下命令測試新的默認布局:

tmuxinator default

令 tmux 自啟動時使用默認布局

如果希望自啟動 tmux 會話時使用自定義的默認布局,請按下面的示例編輯:

~/.bashrc
 if [ -z "$TMUX" ]; then
   tmuxinator default          
 fi                     

使用默認會話的其他方法

除了以上的方法,也可以直接編寫一個在執行時創建並附加默認會話的 bash 腳本。

然後就能在終端執行並獲得一個預先配置好的 tmux 會話了:

#!/bin/bash
tmux new-session -d -n WindowName Command
tmux new-window -n NewWindowName
tmux split-window -v
tmux selectp -t 1
tmux split-window -h
tmux selectw -t 1
tmux -2 attach-session -d

Start tmux in urxvt

Use this command to start urxvt with a started tmux session. I use this with the exec command from my .ratpoisonrc file.

urxvt -e bash -c "tmux -q has-session && exec tmux attach-session -d || exec tmux new-session -n$USER -s$USER@$HOSTNAME"

Start tmux on every shell login

if [ -x "$(command -v tmux)" ] && [ -n "${DISPLAY}" ] && [ -z "${TMUX}" ]; then
    exec tmux new-session -A -s ${USER} >/dev/null 2>&1
fi

What the above snippet does is the following:

  1. test if tmux is executable,
  2. and if a graphical session is running (remove this condition if you want tmux to start in any login shell, but it might interfere with autostarting X at login),
  3. and if we are not already inside a tmux session,
  4. then try to attach, if the attachment fails, start a new session.

If you are using systemd as a user to keep a session alive, you can replace the command inside the if-block with the following commands to attach to that session and detach all the other connected clients:

if ! systemctl --user is-active --quiet tmux.service; then
    systemctl --user start tmux.service
fi
exec tmux attach-session -d -t "${USER}" >/dev/null 2>&1

Start a non-login shell

tmux starts a login shell by default, which may result in multiple negative side effects:

  • Users of fortune may notice that quotes are printed when creating a new panel.
  • The configuration files for login shells such as ~/.profile are interpreted each time a new panel is created, so commands intended to be run on session initialization (e.g. setting audio level) are executed.

To disable this behaviour, add to ~/.tmux.conf:

set -g default-command "${SHELL}"

Use tmux windows like tabs

The following settings added to ~/.tmux.conf allow to use tmux windows like tabs, such as those provided by the reference of these hotkeys — urxvt's tabbing extensions. An advantage thereof is that these virtual 「tabs」 are independent of the terminal emulator.

#urxvt tab like window switching (-n: no prior escape seq)
bind -n S-down new-window
bind -n S-left prev
bind -n S-right next
bind -n C-left swap-window -t -1
bind -n C-right swap-window -t +1

Of course, those should not overlap with other applications' hotkeys, such as the terminal's. Given that they substitute terminal tabbing that might as well be deactivated, though.

It can also come handy to supplement the EOT hotkey Ctrl+d with one for tmux's detach:

bind-key -n C-j detach

Clients simultaneously interacting with various windows of a session

In Practical Tmux, Brandur Leach writes:

Screen and tmux's behaviour for when multiple clients are attached to one session differs slightly. In Screen, each client can be connected to the session but view different windows within it, but in tmux, all clients connected to one session must view the same window.
This problem can be solved in tmux by spawning two separate sessions and synchronizing the second one to the windows of the first, then pointing a second new session to the first.

The tmx script below implements this — the version here is slightly modified to execute tmux new-window if 1 is its second parameter. Invoked as tmx base_session_name [1], it launches the base session if necessary. Otherwise a new "client" session linked to the base, optionally add a new window and attach, setting it to kill itself once it turns "zombie". Do not forget to make it executable.

~/bin/tmx
#!/bin/bash
# Modified TMUX start script from:
#     http://forums.gentoo.org/viewtopic-t-836006-start-0.html

# Works because bash automatically trims by assigning to variables and by passing arguments
trim() { echo $1; }

if [[ -z "$1" ]]; then
    echo "Specify session name as the first argument"
    exit
fi

# Only because I often issue `ls` to this script by accident
if [[ "$1" == "ls" ]]; then
    tmux ls
    exit
fi

base_session="$1"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)
if [[ "$tmux_nb" == "0" ]]; then
    echo "Launching tmux base session $base_session ..."
    tmux new-session -s $base_session
else
    # Make sure we are not already in a tmux session
    if [[ -z "$TMUX" ]]; then
        echo "Launching copy of base session $base_session ..."
        # Session id is date and time to prevent conflict
        session_id=`date +%Y%m%d%H%M%S`
        # Create a new session (without attaching it) and link to base session 
        # to share windows
        tmux new-session -d -t $base_session -s $session_id
        if [[ "$2" == "1" ]]; then
		# Create a new window in that session
		tmux new-window
	fi
        # Attach to the new session & kill it once orphaned
	tmux attach-session -t $session_id \; set-option destroy-unattached
    fi
fi

A useful setting for this is

setw -g aggressive-resize on

added to ~/.tmux.conf. It causes tmux to resize a window based on the smallest client actually viewing it, not on the smallest one attached to the entire session.

An alternative is to put the following ~/.bashrc:

~/.bashrc
function rsc() {
  CLIENTID=$1.`date +%S`
  tmux new-session -d -t $1 -s $CLIENTID \; set-option destroy-unattached \; attach-session -t $CLIENTID
}

function mksc() {
  tmux new-session -d -s $1
  rsc $1
}

Citing the author:

"mksc foo" creates a always detached permanent client named "foo". It also calls "rsc foo" to create a client to newly created session. "rsc foo" creates a new client grouped by "foo" name. It has destroy-unattached turned on so when I leave it, it kills client.
Therefore, when my computer looses network connectivity, all "foo.something" clients are killed while "foo" remains. I can then call "rsc foo" to continue work from where I stopped.

Correct the TERM variable according to terminal type

Instead of setting a fixed TERM variable in tmux, it is possible to set the proper TERM (either screen or screen-256color) according to the type of your terminal emulator:

~/.tmux.conf
## set the default TERM
set -g default-terminal screen

## update the TERM variable of terminal emulator when creating a new session or attaching a existing session
set -g update-environment 'DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TERM'
## determine if we should enable 256-colour support
if "[[ ${TERM} =~ 256color || ${TERM} == fbterm ]]" 'set -g default-terminal screen-256color'
~/.zshrc
## workaround for handling TERM variable in multiple tmux sessions properly (by Nicholas Marriott)
if [[ -n ${TMUX} && -n ${commands[tmux]} ]];then
        case $(tmux showenv TERM 2>/dev/null) in
                *256color) ;&
                TERM=fbterm)
                        TERM=screen-256color ;;
                *)
                        TERM=screen
        esac
fi

Reload an updated configuration without restarting tmux

By default tmux reads ~/.tmux.conf only if it was not already running. To have tmux load a configuration file afterwards, execute:

tmux source-file path

This can be added to ~/.tmux.conf as e. g.:

~/.tmux.conf
 bind r source-file path

You can also do ^: and type :

source .tmux.conf

Template script to run program in new session or attach to existing one

This script checks for a program presumed to have been started by a previous run of itself. Unless found it creates a new tmux session and attaches to a window named after and running the program. If however the program was found it merely attaches to the session and selects the window.

#!/bin/bash

PID=$(pidof $1)

if [ -z "$PID" ]; then
    tmux new-session -d -s main ;
    tmux new-window -t main -n $1 "$*" ;
fi
    tmux attach-session -d -t main ;
    tmux select-window -t $1 ;
exit 0

A derived version to run irssi with the nicklist plugin can be found on its ArchWiki page.

Terminal emulator window titles

If you SSH into a host in a tmux window, you will notice the window title of your terminal emulator remains to be user@localhost rather than user@server. To allow the title bar to adapt to whatever host you connect to, set the following in ~/.tmux.conf

set -g set-titles on
set -g set-titles-string "#T"

For set-titles-string, #T will display user@host:~ and change accordingly as you connect to different hosts.

Automatic layouting

When creating new splits or destroying older ones the currently selected layout is not applied. To fix that, add following binds which will apply the currently selected layout to new or remaining panes:

bind-key -n M-c kill-pane \; select-layout
bind-key -n M-n split-window \; select-layout
提示:You may be interested in tmux-xpanesAUR which makes managing window layouts and SSH connections easy.

Vim colorscheme not loading

See the following if your vim colorscheme is not loading in tmux: [1] [2]

Vim friendly configuration

See [3] for a configuration friendly to vim users.

Friendly pane splitting

The default key-binding for splitting a pane vertically is Ctrl+b % and for splitting a pane horizontally is Ctrl+b ". That can be difficult to type depending of your keyboard layout and it is also hard to remember.

A more friendly key-binding is to use Ctrl+b h for splitting horizontally and Ctrl+b v for splitting a pane vertically, it is also very convenient to remember.

To make this change, add these lines in ~/.tmux.conf:

# More friendly split pane
bind-key h split-window -h
bind-key v split-window -v

Inhibit system suspension

If tmux hangs when connected from another device because the host goes to sleep, run session's shell command with an inhibition lock:

tmux new-session -A "systemd-inhibit --what=idle $SHELL"

Troubleshooting

Scrolling issues

In case of trouble scrolling in the terminal with Shift-Page Up/Down, the following will disable the smcup and rmcup capabilities for any term that reports itself as anything beginning with xterm:

set -ga terminal-overrides ',xterm*:smcup@:rmcup@'

This tricks the terminal emulator into thinking tmux is a full screen application like pico or mutt[4], which will make the scrollback be recorded properly. Beware however, it will get a bit messed up when switching between windows/panes. Consider using tmux's native scrollback instead.

Mouse scrolling

注意:This interferes with selection buffer copying and pasting. To copy/paste to/from the selection buffer hold the shift key.

If you want to scroll with your mouse wheel, ensure mode-mouse is on in .tmux.conf

set -g mouse on

You can set scroll History with:

set -g history-limit 30000

For mouse wheel scrolling as from tmux 2.1 try adding one or both of these to ~/.tmux.conf

   bind -T root WheelUpPane   if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; copy-mode -e; send-keys -M"
   bind -T root WheelDownPane if-shell -F -t = "#{alternate_on}" "send-keys -M" "select-pane -t =; send-keys -M"

Though the above will only scroll one line at a time, add this solution to scroll an entire page instead

   bind -t vi-copy    WheelUpPane   page-up
   bind -t vi-copy    WheelDownPane page-down
   bind -t emacs-copy WheelUpPane   page-up
   bind -t emacs-copy WheelDownPane page-down

Terminal emulator does not support UTF-8 mouse events

When the terminal emulator does not support the UTF-8 mouse events and the mouse on tmux option is set, left-clicking inside the terminal window might paste strings like [M# or [Ma into the promt.

To solve this issue set:

set -g mouse-utf8 off

Shift+F6 not working in Midnight Commander

See Midnight Commander#Broken shortcuts.

See also

Tutorials