tmux:13 个酷炫的调整技巧,让它更个性化、更强大
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
目标受众: tmux 用户(初学者)或阅读过我的“命令行幸福”文章第一部分的人,或正在寻找最佳技巧和窍门的人。
你为什么要调整默认设置?
- tmux 中的键盘快捷键操作起来有点费劲,无论是身体上还是有时在思维上。
- tmux 的默认设置有很多不尽如人意的地方
- 此外,配置过程也很有趣,尤其是当你根据自己的需求进行个性化设置时;毕竟,这就是它的意义所在!
一般来说,只要默认设置能满足需求,我都会尽量使用默认设置。但是,tmux 是个例外。它的设计初衷就是为了方便用户自定义。除此之外,以下是我认为你应该对其进行一些调整的原因。
跟着步骤来,让我们一起打造一个更友好的 tmux 配置。过程中,别忘了在配置文件中添加更多注释;它们会在以后帮助你回忆起相关内容。把你的 tmux 配置当作一份动态文档来维护;学习、实践、更新。
这里为您提供一些实用且有效的 tmux 使用技巧和优化方法。尝试这些技巧可以提升您在使用 tmux 进行日常开发时的效率。让我们从最重要的开始![前缀]
1. 前缀应简洁
默认情况下,所有快捷键绑定都需要先按下一个“前缀”键才能激活。这类似于 Vim 中的 [leader] 键。默认值为Ctrl-b。
默认设置不太容易触发,因为键盘按键位置比较远。大多数人更喜欢Ctrl-a使用前缀键:
- 它会将你的前缀放在主行。
- CapsLock 键可以通过 Ctrl 键重新映射,A 键就在它旁边。
- 如果您已经使用过 GNU screen,
Ctrl-a那么它已经是您的标准键了。
unbind-key C-b # free the original bind-key key
set-option -g prefix C-a # setting the prefix from C-b to C-a
bind-key C-a send-prefix # ensure that we can send Ctrl-A to other apps or the shell that your interacting
2. 只需重新加载配置即可
考虑到您会经常进行配置调整和测试,因此最好在这里介绍一下快捷方式。
默认情况下,有两种重新加载的方式。
- 关闭所有 tmux 会话并重新启动它们
- 在所有会话上执行“source-file ~/.tmux.conf”
谁愿意一直用上述方法啊!咱们来创造一条捷径吧——Ctr+r
bind-key C-r source-file ~/.tmux.conf \; display "Config Reloaded !"
3. 我希望这样开始
如果您不想使用默认 shell 而更喜欢其他 shell,在 tmux 中很容易设置。
让我把默认 shell 设置为我最喜欢的 zsh。Mac 现在已将 zsh 设置为操作系统默认登录 shell。这并非偶然。如果你还没有将 zsh 设置为默认 shell,不妨试试看。
set-option -g default-shell /usr/bin/zsh # login shell for new windows/pane
4. 我看不清!
- 默认情况下,状态栏中的消息会瞬间消失,面板编号的显示时间也短到难以察觉。您可以根据需要调整显示时间。
- 如果您觉得默认的历史记录限制不够用,可以适当提高这个限制。
- 如果会话长时间未活动,则锁定会话。有时,保护屏幕以防止他人窥视是明智之举。
- 窗口的默认名称取决于窗格中运行的内容。嗨 tmux,让我来给它命名。
set-option -g display-time 2000 # By default, status msg disappears in the blink of an eye (750ms)
set-option -g display-panes-time 2000 # By default, pane number disappears in 1 s
set-option -g history-limit 50000 # maximum number of lines held in window history - crank it up from 2k default
set-option -g lock-after-time 3600 # lock the session after 60 mins of inactivity. Sometimes, it is good to protect your screen to make sure other's can't see enough.
set-option -wg automatic-rename off # default names are too vague to see. Let me name it.
5. 像人类一样计数
-
默认情况下,窗口或窗格的索引从 0 开始(程序员真是太傻了!)。虽然 tmux 是“程序员创建,程序员使用”的工具之一,但这种索引方式使得切换窗口变得很麻烦;窗口 0 会出现在状态栏的最左侧,而键盘上的 0 键却在最右侧,然后 1 键又出现在最左侧……这让人很困惑。
-
假设你有三个窗口。如果我们移除第二个窗口,默认结果会剩下两个窗口,编号分别为 1 和 3。但是,通过正确的设置,tmux 可以自动将窗口重新编号为 1 和 2。
好,我们暂时把 tmux 拟人化一下。
set-option -g base-index 1 # window index will start with 1
set-window-option -g pane-base-index 1 # pane index will start with 1
set-option -g renumber-windows on
6. 用 X 特攻队消灭它!
默认情况下,如果您按下 x 键,tmux 会询问您是否确定要关闭窗格。这固然不错,但如果您只想直接关闭窗格呢?我们来试试。顺便一提,我们还可以创建一个自定义快捷键组合,用于关闭整个会话。
unbind-key x # unbind-key “x” from it’s current job of “ask and then close”
bind-key x kill-pane # rebind-key it to just “close”
bind-key X kill-session # key combo for killing the entire session - <prefix> + shift + x
7. 让分屏操作更直观
目前,窗口分割功能绑定了 % 和 ”> 键,这两个键很难记住。如果分别使用 和 进行|垂直分割和_水平分割,就容易记住多了。目前,由于我暂时没有其他需要用到这些奇怪快捷键的地方,所以暂时保留默认绑定。
此外,您还可以指定拆分时要在新窗格中打开的目录。
bind-key | split-window -h -c "#{pane_current_path}" # let's open pane with current directory with -c option
bind-key _ split-window -v -c "#{pane_current_path}"
8. 动作要快
使用 tmux 的主要原因之一是它以键盘为中心,并且与 Vim(我最喜欢的另一个以键盘为中心的工具)配合良好。如果你使用 Vim,你可能熟悉它使用 h、j、k 和 l 作为移动键。这样,你就不必将手指从主行移开就能移动到其他位置。
让我们让窗格、窗口和命令提示符中的操作更加熟悉快捷。
# Pane: Vim Style Movements
bind-key -r h select-pane -L # go left
bind-key -r j select-pane -D # go down
bind-key -r l select-pane -R # go right
bind-key -r k select-pane -U # go up
# Pane: Arrow Movements
bind-key Up select-pane -U
bind-key Down select-pane -D
bind-key Left select-pane -L
bind-key Right select-pane -R
# Window: Movements
bind-key L last-window
bind-key -r C-h select-window -t :- # cycle through the windows for quick window selection
bind-key -r C-l select-window -t :+
# word separators for automatic word selection
set-window-option -g word-separators ' @"=()[]' # default => ‘ -_@’.
# tmux adds a short, almost imperceptible delay between the commands that can cause funny behavior when running vim inside tmux.
set-option -s escape-time 0
# Command Prompt Movements: within the tmux command prompt and the command prompt is accessed using <P>: (in the status line)
set-option -g status-keys vi
9. 调整窗格大小
默认按键绑定Ctr+ Up/Down/Left/Right用于移动一行,Alt + Up/Down/Left/Right用于移动五行。
让我们再添加一个到集合中(Vim 方式)
# Vim Style
bind-key -r H resize-pane -L 2 # resize a pane two rows at a time.
bind-key -r J resize-pane -D 2
bind-key -r K resize-pane -U 2
bind-key -r L resize-pane -R 2
10. 复制和粘贴文本
我们将在这里进行多项自定义设置。如果您经常需要在窗口之间复制粘贴,这项调整可以显著提高您的工作效率。
我们将这样做;
- 像 vi 一样快速浏览输出。
- Vim 样式在复制模式下
- 设置密钥(如果还没有安装 xclip,请先安装)
- 将当前缓冲区中的内容复制到系统剪贴板
Alt+c - 将系统剪贴板中的文本粘贴到当前缓冲区
Alt+v - 直接从选定内容复制到系统剪贴板
Ctr+c - 将系统剪贴板中的文本粘贴到视图中
Ctr+v
- 将当前缓冲区中的内容复制到系统剪贴板
- 截取窗格的屏幕截图并保存,同时添加时间戳。
Alt+s
# To navigating through output in quick way, enable vim navigation keys
set-window-option -g mode-keys vi
# Vim Style in Copy-Mode "<prefix> ["
# Interacting with Paste Buffer
bind-key Escape copy-mode
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection -N "start visual mode for selection"
bind-key -T copy-mode-vi 'y' send-keys -X copy-selection-and-cancel -N "yank text into the buffer"
bind-key C-b choose-buffer # view the buffer stack
unbind-key p
bind-key p paste-buffer # default "<prefix> ]"
# Alt+C: To copy from the current buffer to the sys clipboard .
bind-key M-c run "tmux save-buffer - | xclip -i -sel clipboard"
# Alt+V: To paste text from sys clipboard into current buffer
bind-key M-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\""
# Ctr+C: Make it even better -just one step to move from sys->buffer->editor vice versa
bind-key -Tcopy-mode-vi C-c send -X copy-pipe "xclip -i -sel p -f | xclip -i -sel c" \; display-message "copied to system clipboard"
# Ctr+V: To paste text from sys clipboard into the view
bind-key C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\";tmux paste-buffer"
# To take ASCII screenshots (tmux-resurrect uses C-s for saving, here binding to Alt-s ) .
# create the dir for storing screenshots
bind-key M-s run "tmux capture-pane; tmux save-buffer ~/.mytmux/pane_screenshots/\"$(date +%FT%T)\".screenshots"
11. 视觉样式:配置颜色
设置好合适的颜色模式后,你会发现从 tmux 中使用 Vim、Emacs 和其他全彩程序要容易得多,尤其是在使用更复杂的颜色方案、shell 或语法高亮时。
在这里,你可以做什么完全取决于你的个人喜好。这不仅仅关乎眼睛的颜色。让我来演示几个我的小技巧;
- 让我们把所有非活动窗格都调暗。这样比在状态栏里找星号(*)要容易得多,也更容易找到活动窗格。
- 自定义窗格分隔线,使其既简洁又独特。
- 请确保信息颜色不会对眼睛造成伤害。
# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"
# Pane divider
set-window-option -g pane-border-style fg=colour11,bg=colour234
set-window-option -g pane-active-border-style fg=colour118,bg=colour234
# Cool trick: Let's dim out any pane that's not active.
set-window-option -g window-style fg=white,bg=colour236
set-window-option -g window-active-style fg=white,bg=colour235
# Command / Message line
set-window-option -g message-style fg=black,bold,bg=colour11
12. 粉饰身份地位
这就是你如何根据你的身份地位来量身定制服装的方法。
- 更新状态栏颜色和窗口指示器颜色
- 更新:您希望在状态栏的左侧和右侧看到什么内容?
- 设置软活动提醒
我没有追求花哨的功能,而是专注于能够帮助我工作且资源占用较低的操作。以下是我的状态栏配置;
# Status Bar
set-option -g status-style fg=white,bg=colour04
set-option -g status-justify centre
set-window-option -g window-status-style fg=colour118,bg=colour04
set-window-option -g window-status-current-style fg=black,bold,bg=colour011
set-window-option -g window-status-last-style fg=black,bold,bg=colour011
set-window-option -g window-status-separator |
# Left Side
# Show my active session, window, pane name or id
set-option -g status-left-length 50 # default 10
set-option -g status-left "[#[fg=white]S: #S, #[fg=colour11]W #I-#W, #[fg=colour3]P: #P #[fg=white]]"
# set-option -g status-left-style
# Right Side
set-option -g status-right-length 50 # default 50
set-option -g status-right "#[fg=grey,dim,bg=default] uptime: #(uptime | cut -f 4-5 -d\" \" | cut -f 1 -d\",\")"
# Enable Activity Alerts
set-option -g status-interval 60 # Update the status line every 60 seconds (15 is default)
set-window-option -g monitor-activity on # highlights the window name in the status line
13. 使用插件扩展 tmux
有很多 tmux插件可供选择。如果非要我选一个,我会选 'tmux-resurrect'。
- 此插件可在系统重启后恢复 tmux 环境。它会尽力保存并恢复 tmux 环境的所有详细信息。请参阅文档。
- 如果你设置了复活功能,那么下一步合乎逻辑的做法就是设置“连续体”,使保存和恢复操作自动进行。
以下是设置 tmux 插件管理的步骤;
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
# Last saved environment is automatically restored when tmux is started.
set -g @continuum-boot 'on'
# terminal window will go fullscreen
set -g @continuum-boot-options 'fullscreen'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
# run-shell "mkdir -p ~/.tmux/plugins/"
# run-shell "git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm"
run '~/.tmux/plugins/tpm/tpm'
# Hit prefix + I to install plugins for the first time.
# It takes few seconds. So, wait before panic.
#
# resurrect key bindings:
# prefix + Ctrl-s - save
# prefix + Ctrl-r - restore
#
注意:所有这些调整均已在 Ubuntu 20.10 和 tmux 3.1b 上测试并有效。
你可以在这里访问我的 tmux 配置。
文章来源:https://dev.to/krishnam/tmux-13-cool-tweaks-to-make-it-personal-and-powerful-487p