Tmux 学习笔记

Table of Contents


1 tmux 基础知识

By default, tmux uses Ctrl-b as the prefix key.

<prefix> c: Create a new window (appears in status bar)
<prefix> 0: Switch to window 0
<prefix> 1: Switch to window 1
<prefix> 2: Switch to window 2 (etc.)
<prefix> x: Kill current window
<prefix> d: Detach tmux (exit back to normal terminal)

To see all tmux sessions, you can enter:

tmux ls

To attach to the last used session, you can enter:

tmux a

To attach to a specific session, you can enter:

tmux a -t <session-name>

You can also customize tmux by making a .tmux.conf file in your home directory.

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

#Start window numbering at 1
set -g base-index 1

A few recommendations:

  1. 如果你还没有把 ctrl 键映射到 caps-lock 键,我建议你那么做。

    unbind C-b
    set -g prefix C-a
  2. ~/.tmux.conf 文件只有在每次打开时才会读取更新,所以我建议:

    # bind a reload key
    bind R source-file ~/.tmux.conf \; display-message "Config reloaded."

    这样的话,你每次添加更新后,只需按 Ctrl-b R 重新加载配置即可。

Sessions A tmux session can contain multiple windows, Sessions are a neat feature. To create a new session just run:

tmux new -s <name-of-my-session>

To create a new session press Ctrl-b : and the enter the following command:

new -s <name-of-my-new-session>

1.1 Navigation through Tmux sessions

To get a list of the existing sessions, press Ctrl-b s

In the case that you are not in Tmux but you have one or more sessions running just use:

tmux attach

1.2 Fast text navigation and copying

1.2.1 Navigate text

Tmux allows for text navigation in a way that is very similar to Vim.

# Use vim keybindings in copy mode
setw -g mode-keys vi

1.2.2 Send copied text to System's clipboard

By default, when you copy text from Tmux, the text is only available to be inside that same Tmux session. In order to make that text available to be pasted anywhere, you have to tell Tmux to copy to system's clipboard.

  1. Install retach-to-user-namespace, this is very easy with brew, just run the following command:

    brew install reattach-to-user-namespace
  2. Update ~/.tmux.conf file

    # invoke reattach-to-user-namespace every time a new window/pane opens
    set-option -g default-command "reattach-to-user-namespace -l bash"

1.2.3 Select and copy text

Now, follow these steps to copy that text:

  1. Enter copy mode: ctrl-b [.
  2. Start moving across the text as you would do in Vim: with j, k, l, h, etc..
  3. Once you get to the text you want to copy press the spacebar and start selecting text(exactly as you would do it in Vim).
  4. Once text is selected press enter key

1.2.4 Make text copying even more Vim-like

You can use v key to select text and v to copy it, just add and following to your ~/.tmux.conf file:

# start selecting text typing 'v' key (once you are in copy mode)
bind-key  -t vi-copy v begin-selection
# copy selected text to the system's clipboard
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

1.3 Effective pair programming

You can share the address of a Tmux session with someone else, and that person can connect to the session via SSH. Since the connection runs over SSH, it will be very lightweight. For the user connecting to the remote Tmux session, it will fell as if the session is running locally, providing the internet connection is fast enough.

1.3.1 Tmux with Tmate

Tmate is a tool that makes it very easy to create a Tmux session and share it with someone else over hte internet.

  1. Install Homebrew:

    ruby -e "$(curl -fsSL"
  2. Install Tmate

    brew update   && \
    brew tap naviennot/tmate && \
    brew install tmate
  3. Launch a new session with Tmate:

  4. Copy the SSH URL given by Tmate on the Tmux session.
  5. Ask the other person to access via SSH using the URL you just copied.

1.4 Table Notes

命令 属性
ctrl-b s 列出当前tmux中的会话
ctrl-b % 垂直分割
ctrl-b " 水平分割
ctrl-b ➡ 进入目标窗格
ctrl-b c 创建窗口
ctrl-b 0..! 进入对应数字窗口
tmux new -s name start new with session name
tmux a # (or at, or attach) attach
tmux a -t name attach to nameed
tmux ls list sessions
tmux kill-session -t name kill session

In tmux, hit the prefix ctrl+b (my modified prefix is ctrl+a) and then:


:new<CR> new session
s list sessions
$ name session


c create window
w list windows
n nest window
p previous window
f find window
, name window
& kill window


% vertical split
" horizontal split

o swap panes
q show pane numbers
x kill pane
+ break pane into window (e.g. to select text by mouse to copy)
- restore pane from window

<prefix> q (show pane numbers, when the numbers show up type the key to goto that pane)
<prefix> { (move the current pane left)
<prefix> } (move the current pane right)
<prefix> z toggle pane zoom


d detach
t big clock
? list shortcuts
: prompt

Last Updated 2018-05-26 Sat 15:55. Created by Siqing Mu