Vim & tmux & zsh ——打造Python IDE之tmux

关键词:tmux

我在我的github也维护了一份关于tmux的文档

Tmux 简单来说就是终端里的『窗口管理器』,如果我使用终端登录到远程主机并运行前台程序,那么这个窗口等于就被占用了,想要看一下 CPU 的使用率,就得再连接一次。但是如果在远程主机上运行 tmux,那么就可以开启多个控制台(类似于窗口),相当高效,如果在你的远程主机上也装上tmux, 那么就可以完成screen的所有功能了。

安装

前面提到的窗口管理只是 tmux 功能的一小部分,另一个很有用的功能就是,连接到远程主机之后,一旦断开,那么当前账户登录的任务就被取消了,但是使用 tmux 可以在断开之后继续工作,下次登录可以查看。其他的功能还有:

  1. 窗口切换,每个窗口里还可以分割面板
  2. 配置方便,可以使用脚本
  3. 类似 vim 的双层操作逻辑,也就是说有分层的不同模式
  4. 复制粘贴缓冲区

安装的话也很简单,在 mac 下直接 brew install tmux(前提需要安装 homebrew),ubuntu 下则直接 sudo apt install tmux

在终端中输入 tmux 就可以打开一个新的 tmux session,tmux 的所有操作必须先使用一个前缀键(默认是 ctrl + b, 不过我一般喜欢修改为ctrl+a仿照screen的快捷键)进入命令模式,或者说进入控制台,就像 vim 中的 esc(话说我的键盘LOCK磨的快的原因就是我的esc映射到了LOCK上)。

基本操作(源生)

信息查询

  • tmux list-keys 列出所有可以的快捷键和其运行的 tmux 命令
  • tmux list-commands 列出所有的 tmux 命令及其参数
  • tmux info 流出所有的 session, window, pane, 运行的进程号,等。

窗口控制

先来看看在 tmux 之外如何进行控制

  • session 会话:session是一个特定的终端组合。输入tmux就可以打开一个新的session
    • tmux new -s session_name 创建一个叫做 session_name 的 tmux session
    • tmux attach -t session_name 重新开启叫做 session_name 的 tmux session
    • tmux switch -t session_name 转换到叫做 session_name 的 tmux session
    • tmux list-sessions / tmux ls 列出现有的所有 session
    • tmux detach 离开当前开启的 session
    • tmux kill-server 关闭所有 session
  • window 窗口:session 中可以有不同的 window(但是同时只能看到一个 window)
    • tmux new-window 创建一个新的 window
    • tmux list-windows
    • tmux select-window -t :0-9 根据索引转到该 window
    • tmux rename-window 重命名当前 window
  • pane 面板:window 中可以有不同的 pane(可以把 window 分成不同的部分)
    • tmux split-window 将 window 垂直划分为两个 pane
    • tmux split-window -h 将 window 水平划分为两个 pane
    • tmux swap-pane -[UDLR] 在指定的方向交换 pane
    • tmux select-pane -[UDLR] 在指定的方向选择下一个 pane

更常用的是在 tmux 中直接通过默认前缀 ctrl + b 之后输入对应命令来操作,具体如下(这里只列出输入默认前缀之后需要输入的操作):

基本操作

  • ? 列出所有快捷键;按q返回
  • d 脱离当前会话,可暂时返回Shell界面
  • s 选择并切换会话;在同时开启了多个会话时使用
  • D 选择要脱离的会话;在同时开启了多个会话时使用
  • : 进入命令行模式;此时可输入支持的命令,例如 kill-server 关闭所有tmux会话
  • [ 复制模式,光标移动到复制内容位置,空格键开始,方向键选择复制,回车确认,q/Esc退出
  • ] 进入粘贴模式,粘贴之前复制的内容,按q/Esc退出
  • ~ 列出提示信息缓存;其中包含了之前tmux返回的各种提示信息
  • t 显示当前的时间
  • ctrl + z 挂起当前会话

窗口操作

  • c 创建新窗口
  • & 关闭当前窗口
  • [0-9] 数字键切换到指定窗口
  • p 切换至上一窗口
  • n 切换至下一窗口
  • l 前后窗口间互相切换
  • w 通过窗口列表切换窗口
  • , 重命名当前窗口,便于识别
  • . 修改当前窗口编号,相当于重新排序
  • f 在所有窗口中查找关键词,便于窗口多了切换

面板操作

  • " 将当前面板上下分屏(我自己改成了 |
  • % 将当前面板左右分屏(我自己改成了 -
  • x 关闭当前分屏
  • ! 将当前面板置于新窗口,即新建一个窗口,其中仅包含当前面板
  • ctrl+方向键 以1个单元格为单位移动边缘以调整当前面板大小
  • alt+方向键 以5个单元格为单位移动边缘以调整当前面板大小
  • q 显示面板编号
  • o 选择当前窗口中下一个面板
  • 方向键 移动光标选择对应面板
  • { 向前置换当前面板
  • } 向后置换当前面板
  • alt+o 逆时针旋转当前窗口的面板
  • ctrl+o 顺时针旋转当前窗口的面板
  • z 最大化当前所在面板
  • page up 向上滚动屏幕,q 退出
  • page down 向下滚动屏幕,q 退出

因为 iTerm2 的支持,很多切换的操作可以直接用鼠标进行,非常方便。具体大家可以自己尝试一下。

配置(个人配置,非源生)

我们可以先进行一些简单的配置,修改 ~/.tmux.conf 即可,让整个使用更方便。

注意我个人喜欢使用ctrl_a因此特意进行了匹配修饰。

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#set -g default-terminal "xterm-256color" # Use 256 colors
set -g default-terminal "screen-256color" # Use 256 colors
# fix utf character confused with mosh + tmux
# see issue: https://github.com/tmux/tmux/issues/1310
set -as terminal-overrides ',*:indn@'
set -g display-time 5000
# tmux 2.2 has remove below option
# set -g status-utf8 on # Enable utf-8
set -g history-limit 100000 # Scrollback buffer n lines
set -g mouse on # Enable mouse (>=2.1)
setw -g mode-keys vi
set-option -g status-position top # Status bar, default bottom
set-option -g allow-rename off
# Start window indexing at one instead of zero
set -g base-index 1
# Start pane indexing at one instead of zero
setw -g pane-base-index 1
# Disable default 500ms repeat time
set-option -g repeat-time 0
# Set the prefix key and some key bindings to match GNU Screen
set -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
# start selecting text typing 'v' key (once you are in copy mode)
bind-key -T copy-mode-vi 'v' send -X begin-selection
# copy selected text to the system's clipboard
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"
# Key bindings for horizontal and vertical panes
unbind %
bind | split-window -h
unbind '"'
bind - split-window -v
# Key bindings for join/split panes
# via: http://unix.stackexchange.com/questions/14300/moving-tmux-window-to-pane
bind-key j command-prompt -p "join pane from:" "join-pane -s '%%'"
bind-key s command-prompt -p "send pane to:" "join-pane -t '%%'"
# Shift-movement keys will resize panes
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
# Window title string (uses statusbar variables)
set -g set-titles-string '#T'
# Disable auto rename window title
set -g allow-rename off
setw -g automatic-rename off
# Bind to reload config
bind r source-file ~/.tmux.conf
# Bind to create a specify layout in current window
bind T source-file ~/.dotfiles/fupeng
# # Pane settings
# set -g status-left-length 90
# set -g status-right-length 90
# set -g status-left '[#(whoami)]'
# set -g status-right '#[fg=white][#(date +" %m-%d %H:%M ")]'
# set -g status-justify "centre"
# set -g window-status-format '#I #W'
# set -g window-status-current-format ' #I #W '
#
# # https://github.com/seebi/tmux-colors-solarized
# #### COLOUR (Solarized 256)
#
# # default statusbar colors
# set-option -g status-bg blue
# set-option -g status-fg white
# set-option -g status-attr default
#
# # default window title colors
# set-window-option -g window-status-fg colour253
# set-window-option -g window-status-bg default
# set-window-option -g window-status-attr dim
#
# # active window title colors
# set-window-option -g window-status-current-fg white
# set-window-option -g window-status-current-bg cyan
# set-window-option -g window-status-current-attr bright
#
# # pane border
# set-option -g pane-border-fg green
# set-option -g pane-border-bg black
# set-option -g pane-active-border-fg red
# set-option -g pane-active-border-bg black
#
# # message text
# set-option -g message-bg white
# set-option -g message-fg black
# set-option -g message-attr bright
#
# # pane number display
# set-option -g display-panes-active-colour colour33 #blue
# set-option -g display-panes-colour colour166 #orange
## Status bar design
## from: http://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/
# status line
set -g status-justify centre
set -g status-bg default
set -g status-fg colour12
set -g status-interval 2
# messaging
set -g message-fg black
set -g message-bg yellow
set -g message-command-fg blue
set -g message-command-bg black
#window mode
setw -g mode-bg colour6
setw -g mode-fg colour0
# window status
setw -g window-status-format " #F#I:#W#F "
setw -g window-status-current-format " #F#I:#W#F "
setw -g window-status-format "#[fg=magenta]#[bg=black] #I #[bg=cyan]#[fg=colour8] #W "
setw -g window-status-current-format "#[bg=brightmagenta]#[fg=colour8] #I #[fg=colour8]#[bg=colour14] #W "
setw -g window-status-current-bg colour0
setw -g window-status-current-fg colour11
setw -g window-status-current-attr dim
setw -g window-status-bg green
setw -g window-status-fg black
setw -g window-status-attr reverse
# loud or quiet?
set-option -g visual-activity off
set-option -g visual-bell off
set-option -g visual-silence off
set-window-option -g monitor-activity off
set-option -g bell-action none
set -g default-terminal "screen-256color"
# The modes
setw -g clock-mode-colour colour135
#setw -g mode-attr normal
setw -g mode-fg colour196
setw -g mode-bg colour238
# The panes
set -g pane-border-bg colour235
set -g pane-border-fg colour238
set -g pane-active-border-bg colour236
set -g pane-active-border-fg blue
# The statusbar
set -g status-position bottom
set -g status-bg colour034
set -g status-fg colour137
set -g status-attr dim
set -g status-left '#[fg=colour233,bg=colour241,bold] @#(whoami) #[fg=colour233,bg=colour245,bold] [#h] '
set -g status-right '#{?pane_synchronized, #[fg=white]#[bg=blue] sync ,}#[fg=colour233,bg=colour241,bold] %Y-%m-%d #[fg=colour233,bg=colour245,bold] %H:%M '
set -g status-right-length 50
set -g status-left-length 50
setw -g window-status-current-fg blue
setw -g window-status-current-bg colour238
setw -g window-status-current-attr bold
setw -g window-status-current-format ' #I#[fg=colour250]:#[fg=colour255]#W#[fg=red]#F '
setw -g window-status-fg colour138
setw -g window-status-bg colour235
setw -g window-status-attr none
setw -g window-status-format ' #I#[fg=colour237]:#[fg=colour250]#W#[fg=colour244]#F '
setw -g window-status-bell-attr bold
setw -g window-status-bell-fg colour255
setw -g window-status-bell-bg colour1

参考链接