Linux 实用工具

Linux 的终端上有很多实用小工具,能提高工作效率,比如 tmux。

在此介绍他们的用法、配置和快捷键。

TMUX

参考资料:一文助你打通 tmuxTmux 使用教程

命令行的典型使用方式是,打开一个终端窗口(terminal window,以下简称 "窗口"),窗口可以被不断切割,切割成一个个小块,这一个个小块我们叫做窗格(pane)。在里面输入命令。用户与计算机的这种临时的交互,称为一次 "会话"(session) 。

会话的一个重要特点是,窗口与其中启动的进程是连在一起的。打开窗口,会话开始;关闭窗口,会话结束,会话内部的进程也会随之终止,不管有没有运行完。

Tmux 就是会话与窗口的 "解绑" 工具,将它们彻底分离:窗口关闭时,会话并不终止,而是继续运行,等到以后需要的时候,再让会话 "绑定" 其他窗口。

  1. 它允许在单个窗口中,同时访问多个会话。这对于同时运行多个命令行程序很有用。

  2. 它可以让新窗口 "接入" 已经存在的会话。

  3. 它允许每个会话有多个连接窗口,因此可以多人实时共享会话。

  4. 它还支持窗口任意的垂直和水平拆分。

tmux 的安装

Linux 系统中通常使用 yum 来安装 tmux : yum install tmux

Mac OS 系统中通常使用 brew 来安装 tmux :brew install tmux

tmux 的前缀键

tmux 中的很多操作都是通过快捷键来实现的,通过快捷键我们可以更加高效的完成任务。如果想使用 tmux 中的快捷键,我们必须使用 tmux 的前缀按键 ctrl + b , 在 tmux 中所有的快捷键都需要通过前缀按键去唤起的。

tmux 的会话常用操作

操作 命令 快捷键
新建会话 tmux / tmux new -s <session-name>
离开会话 tmux detach (ctrl + b) d
查看会话列表 tmux ls (ctrl + b) s
进入会话 ta/tmux attach -t <session-name>
关闭会话 tmux kill-session -t <session-name> ctrl + d
切换会话 tmux switch -t <session-name>
重命名会话 tmux rename-session -t <old-session-name> <new-session-name> (ctrl +b) $

tmux 的窗格常用操作

操作 命令 快捷键
水平分割窗格 tmux split-window -h(左右) (ctrl + b) %(左右)
垂直分割窗格 tmux split-window (上下) (ctrl + b) “(上下)
光标移动到上方窗格 tmux select-pane -U (ctrl +b) ↑
光标移动到下方窗格 tmux select-pane -D (ctrl +b) ↓
光标移动到左边窗格 tmux select-pane -L (ctrl +b) ←
光标移动到右边窗格 tmux select-pane -R (ctrl +b) →
光标切换到上一个窗格 (ctrl +b) ;
光标切换到下一个窗格 (ctrl +b) o
当前窗格向上移动 tmux swap-pane -U
当前窗格向下移动 tmux swap-pane -D
关闭当前的窗格 (ctrl +b) x
最大化窗格 (ctrl +b) z 触发两次还原当前的窗格大小
显示时间 (ctrl +b) t 点击 Enter 将会复原

tmux 的窗口常用操作

操作 命令 快捷键
创建窗口 tmux new-window -n <window-name> (ctrl + b) c
切换窗口 tmux select-window -t <window-name> (ctrl +b) w 显示窗口列表可以通过 j,k 上下进行选择窗口,然后回车进入指定的窗口。
(ctrl +b) n 快速切换到下一个窗口。
(ctrl +b) p 快速切换到上一个窗口。
重命名窗口 tmux rename-window <new-window-name> (ctrl +b) ,
关闭窗口 tmux kill-window -t <window-name> (ctrl +b) &

tmux 的配置

.tmux.conf 默认放在 ~(home 目录) 目录下面,以下是本人配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
set -g mode-keys vi
set -g default-shell /bin/zsh

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'fcsonline/tmux-thumbs'
set -g @plugin 'schasse/tmux-jump'
set -g @plugin 'dracula/tmux'

if '[ -f ~/dotfiles/tmux/bindings.tmux ]' 'source ~/dotfiles/tmux/bindings.tmux'

# 主题配置
set -g @dracula-show-battery false
set -g @dracula-show-network false
set -g @dracula-show-weather false
set -g @dracula-show-timezone false
set -g @dracula-military-time true
set -g @dracula-border-contrast true
set -g @dracula-cpu-usage true
set -g @dracula-ram-usage tr

run '~/.tmux/plugins/tpm/tpm'
  1. tmux-plugins/tpm 是 Tmux Plugin Manager,即 Tmux 的插件管理工具。
  2. tmux-plugins/tmux-sensible,一组人人都能接受的选项。
  3. tmux-plugins/tmux-yank,允许拷贝至系统剪切板。
  4. schasse/tmux-jump,按 tmux-prefix+j 键进入 jump 模式,输入需要跳转到的位置的字符,屏幕会高亮显示符合条件的位置并分配一个 key,输入 key 后光标就会跳转至选中位置。
  5. dracula 主题

在 Tmux 中,安装插件的一些快捷命令如下:

1
2
3
prefix shift-i      # install
prefix shift-u # update
prefix alt-u # uninstall plugins not on the plugin list

bindings.tmux 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -- general -------------------------------------------------------------------
set -g default-terminal "screen-256color" # colors!
setw -g xterm-keys on
set -s escape-time 10 # faster command sequences
set -sg repeat-time 300 # increase repeat timeout
set -s focus-events on
set -g prefix2 C-a # GNU-Screen compatible prefix
bind C-a send-prefix -2
set -q -g status-utf8 on # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on
set -g history-limit 5000 # boost history

# edit configuration
# bind e new-window -n "~/.tmux.conf.local" "sh -c '\${EDITOR:-vim} ~/.tmux.conf.local && tmux source ~/.tmux.conf && tmux display \"~/.tmux.conf sourced\"'"
bind E command-prompt -p "Command:" \
"run \"tmux list-panes -a -F '##{session_name}:##{window_index}.##{pane_index}' \
| xargs -I PANE tmux send-keys -t PANE '%1' Enter\""

# reload configuration
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf sourced'

# send the prefix to client inside window
bind-key -n C-q send-prefix

# -- display -------------------------------------------------------------------

set -g base-index 1 # start windows numbering at 1
setw -g pane-base-index 1 # make pane numbering consistent with windows

setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on # renumber windows when a window is closed

set -g set-titles on # set terminal title

# fix the window name
set-option -g allow-rename off

set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000 # slightly longer status messages display time
set -g status-interval 10 # redraw status line every 10 seconds

# clear both screen and history
bind -n C-l send-keys C-l \; run 'sleep 0.1' \; clear-history

# activity
set -g monitor-activity on
set -g visual-activity off

# -- navigation ----------------------------------------------------------------

# create session
bind C-c new-session

# find session
bind C-f command-prompt -p find-session 'switch-client -t %%'

# split current window horizontally
bind - split-window -v -c "#{pane_current_path}"
# split current window vertically
bind _ split-window -h -c "#{pane_current_path}"
bind | split-window -h -c "#{pane_current_path}"

bind c new-window -c "#{pane_current_path}"

# pane navigation
bind -r h select-pane -L # move left
bind -r j select-pane -D # move down
bind -r k select-pane -U # move up
bind -r l select-pane -R # move right
bind -r > swap-pane -D # swap current pane with the next one
bind -r < swap-pane -U # swap current pane with the previous one

# pane resizing
bind -r H resize-pane -L 2
bind -r J resize-pane -D 2
bind -r K resize-pane -U 2
bind -r L resize-pane -R 2

# window navigation
unbind n
unbind p
bind -r C-h previous-window # select previous window
bind -r C-l next-window # select next window
bind Tab last-window # move to last active window

# kill pane
bind -r W kill-pane
bind C-w kill-pane

# -- copy mode -----------------------------------------------------------------

bind Enter copy-mode # enter copy mode

run -b 'tmux bind -t vi-copy v begin-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true'
run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi H send -X start-of-line 2> /dev/null || true'
run -b 'tmux bind -t vi-copy L end-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi L send -X end-of-line 2> /dev/null || true'

# copy to macOS clipboard
if -b 'command -v pbcopy > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | pbcopy"'
if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
# copy to X11 clipboard
if -b 'command -v xsel > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xsel -i -b"'
if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1"'
# copy to Windows clipboard
if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'

# -- buffers -------------------------------------------------------------------

bind b list-buffers # list paste buffers
bind p paste-buffer # paste from the top paste buffer
bind P choose-buffer # choose which buffer to paste from

# -- mouse mode ----------------------------------------------------------------
set -g mouse on

# Toggle mouse on
bind-key M \
set-option -g mouse on \;\
display-message 'Mouse: ON'

# Toggle mouse off
bind-key m \
set-option -g mouse off \;\
display-message 'Mouse: OFF'