0%

用到的tmux

终端复用软件,跟screen一样,但是要强大的多。

1. 典型应用场景

自己电脑是客户端,平常ssh到linux服务器上编译运行调试软件,网络断开或者自己电脑挂掉之后,不想服务器上运行的程序也挂掉,这时候就可以用tmux了。还有就是tmux能方便的多开好几个窗口。tmux的作用是代理终端,以前你直接通过ssh通道来操作服务器的终端,现在通过ssh通道启动了1个tmux程序,由tmux程序代替你控制多个终端,这样你就拥有了一组终端,所以是个服务器开发神器。这个神器差不多长这样:
image.png

2. 配置

跟vim差不多,也是要在服务器本用户主目录下边放一个配置文件.tmux.conf,直接用这个吧,有些东西自己想调整可以看着改:

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
# (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
unbind C-b
set -g prefix C-a

#up
bind-key k select-pane -U
#down
bind-key j select-pane -D
#left
bind-key h select-pane -L
#right
bind-key l select-pane -R

#在当前路径打开新窗口
bind-key c new-window -c "#{pane_current_path}"
bind-key '"' split-window -c "#{pane_current_path}"
bind-key % split-window -h -c "#{pane_current_path}"

#select last window
bind-key C-l select-window -l

#copy-mode 将快捷键设置为vi 模式
setw -g mode-keys vi
# use the powerline patched font variant of the theme, possible values are:
# - default
# - powerline
tmux_conf_theme=default
#tmux_conf_theme=powerline

# display an indicator in the status line when the prefix key has been pressed,
# possible values are:
# - enabled
# - disabled
tmux_conf_theme_prefix=disabled
#tmux_conf_theme_prefix=enabled

# display the battery information in the status line, possible values are:
# - enabled
# - disabled
tmux_conf_theme_battery=enabled
#tmux_conf_theme_battery=disabled

# style to use for the battery status, possible values are:
# - bar
# - percentage
tmux_conf_battery_style=bar
#tmux_conf_battery_style=percentage

# symbols to use for battery status, possible values are:
# - block
# - heart
tmux_conf_battery_symbol=block
#tmux_conf_battery_symbol=heart

# number of symbols to use for battery status
tmux_conf_battery_symbol_count=auto
#tmux_conf_battery_symbol_count=5

# palette used for the battery status, possible values are:
# - 'colour_full_fg,colour_empty_fg,colour_bg'
# - heat
# - gradient
tmux_conf_battery_palette='#d70000,#e4e4e4,#000000'
#tmux_conf_battery_palette=colour160,colour254,colour16
#tmux_conf_battery_palette=heat
#tmux_conf_battery_palette=gradient

# display the battery status: charging (U+26A1) / discharging (U+1F50B)
tmux_conf_battery_status=enabled
#tmux_conf_battery_status=disabled

# or alternatively use an external tool, e.g. [https://github.com/Goles/Battery](https://github.com/Goles/Battery)
#tmux_conf_battery='#(battery -t) '

# display the time in the status line, possible values are:
# - enabled
# - disabled
tmux_conf_theme_time=enabled
#tmux_conf_theme_time=disabled

# display the date in the status line, possible values are:
# - enabled
# - disabled
tmux_conf_theme_date=enabled
#tmux_conf_theme_date=disabled

# display the username in the status line, possible values are:
# - enabled
# - disabled
# - ssh
tmux_conf_theme_username=enabled
#tmux_conf_theme_username=disabled
#tmux_conf_theme_username=ssh

# display the hostname in the status line, possible values are:
# - enabled
# - disabled
# - ssh
tmux_conf_theme_hostname=enabled
#tmux_conf_theme_hostname=disabled
#tmux_conf_theme_hostname=ssh

# highlight focuse pane, possible values are:
# - enabled
# - disabled
tmux_conf_theme_highlight_focused_pane=disabled
#tmux_conf_theme_highlight_focused_pane=enabled

# should new windows retain current path, possible values are:
# - true
# - false
tmux_conf_new_windows_retain_current_path=false
#tmux_conf_new_windows_retain_current_path=true

# should new panes reatin current path, possible values are:
# - true
# - false
tmux_conf_new_panes_retain_current_path=true
#tmux_conf_new_panes_retain_current_path=false

# prompt for session name when creating a new session, possible values are:
# - true
# - false
tmux_conf_new_session_prompt=false
#tmux_conf_new_session_prompt=true

# if you're running tmux within iTerm2
# - and tmux is 1.9 or 1.9a
# - and iTerm2 is configured to let option key act as +Esc
# - and iTerm2 is configured to send [1;9A -> [1;9D for option + arrow keys
# then uncomment the following line to make Meta + arrow keys mapping work
#set -g terminal-overrides "*:kUP3=\e[1;9A,*:kDN3=\e[1;9B,*:kRIT3=\e[1;9C,*:kLFT3=\e[1;9D"

# ------------------------------------------------------------------------------
# this is the place to further customize tmux's configuration

#set -g history-limit 10000

3. 几个概念

  1. session,会话
  2. window,窗口
  3. panel,面板

关系是“1个会话会包含几个窗口,1个窗口会包含几个面板”
当然,1个ssh通道可以启动好几个会话,每个会话都可以detach和attach,从而一下子恢复一组工作空间。网络断开之后也会恢复哦,不过服务器重启就没办法了,毕竟tmux是跑在服务器上的软件。

4. 常用命令

4.1 在tmux之外的命令

1
2
3
4
5
6
7
8
# 新建1个名叫hello的session
tmux new -s hello

# 列出当前有几个session
tmux ls

# 挂接到名叫hello的session,恢复先前的工作空间
tmux at -t hello

4.2 进入tmux后的命令。一般来说在tmux内部的常用命令都是组合键,先按下前缀键,再按下后边跟着的那个键。默认的前缀键是ctrl+b,上边的配置修改成了ctrl+a,因为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
# 退出当前panel;如果当前panel是该window的最后一个,就退出该window;如果该window是该session的最后1个,就退出该session。这个不带前缀键。
ctrl+d

# 安全退出当前session(detach),这样你以后还能重新挂接(attach)上来恢复工作空间。
ctrl+a, d

# 浏览并切换session
ctrl+a, s

# 浏览当前配置使用的快捷键列表,按q退出
ctrl+a, ?

# 新建一个window
ctrl+a, c

# 在前后两个window之间跳转
ctrl+a, ctrl+l

# 跳转到相应编号的window
ctrl+a, 1/2/3...

# 修改当前window的名称,默认都是个'bash'。是个逗号哈。
ctrl+a, ,

# 新建垂直切分的panel
ctrl+a, %

# 新建水平切分的panel
ctrl+a, "

# 在panel之间切换啊,跟vim的方向一样,不过都需要加个前缀
# 向左
ctrl+a, h
# 向右
ctrl+a, l
# 向下
ctrl+a, j
# 向上
ctrl+a, k

# 暂时将当前panel占据整个window,再按一次就切换回去,在复制的时候很有用。
ctrl+a, z

# 浏览历史,进入后就可以按vim的进行操作了,常用的是j,k, ctrl+b(向上翻页),ctrl+f(向下翻页)
ctrl+a, [

# 强制关闭当前panel
ctrl+a, x

# 强制关闭当前window
ctrl+a, &

# 切换panel布局,比如横向分屏变竖向
ctrl+a, space

# 旋转panel布局,比如左1右2,变为左2右1
ctrl+a, ctlr+o

# 微调panel大小,这个试一下就知道了
ctrl+a, ctrl+方向键

好了,常用的就是这些了。


参考:

  1. https://github.com/gpakosz/.tmux