tmux使用方法
tmux是一种终端多路复用器:它可以在一个屏幕上创建、访问和控制多个终端。
启动tmux时,它会创建一个带有单一窗口的新会话并显示在屏幕上。 屏幕底部的状态行显示当前会话的信息,并用于输入交互式命令。
本文目前主要记录了一些tmux使用的快捷键、常用配置以及插件使用方法。
session
新建会话:tmux new -s <session-nmae>
查看会话:tmux ls
进入已有会话:tmux attach -t <session-name>
切换会话:tmux switch -t <session-name>
退出会话但不关闭session:tmux detach
关闭会话:exit
window
新建窗口:ctrl+b
c
删除窗口:ctrl+b
x
切换到下一个窗口:ctrl+b
n
切换到上一个窗口:ctrl+b
p
选择会话选择窗口:ctrl+b
w
pane
左右分屏:ctrl+b
%
上下分屏:ctrl+b
"
分屏跳转:ctrl+b
<方向键>
关闭当前pane:ctrl+b
x
和下一窗格交换位置:ctrl+b
}
和上一窗格交换位置:ctrl+b
{
最大或最小化当前pane:ctrl+b
z
将当前pane转为单独的window再转换回去
将当前pane转为单独的window:ctrl+b
!
window转换回pane:
- 按
Ctrl+b
然后按:
进入命令模式。 - 输入
join-pane -s <源窗口编号> -t <目标窗口编号>
,然后按回车。
例如,如果你想将 window 2 转换回 pane 并加入到 window 1,可以输入:
join-pane -s 2 -t 1
分屏时保持上一个pane的工作目录
在~/.tmux.conf
中添加下面两行:
# 设置新的pane在当前目录中打开
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"
创建新窗口时保持工作目录
# 创建新窗口时使用之前窗口所在的目录
bind c new-window -c "#{pane_current_path}"
交换窗口顺序
ctrl+B :
进入命令行模式。swap-window -s 1 -t 2
交换窗口位置(swap-window -s <源窗口编号> -t <目标窗口编号>
)。
创建新窗口插入到当前窗口之后
# Create new window after current window with Ctrl-b c
bind c new-window -a
# 与上面的配置合并之后为
bind c new-window -a -c "#{pane_current_path}"
窗口关闭时重新排序
# 窗口关闭时自动排序
set-option -g renumber-windows on
tmux.conf
# ----------------------------------------------------------------------------------------基本配置
# 基本配置放在前面的原因在于tmux-continuum插件会修改status-right的内容以使其自定义钩子生效
setw -g mode-keys vi
set -g history-limit 2000
# 解决tmux启动之后失色的问题
# set -g default-terminal "screen-256color"
set-option -g base-index 1 # 窗口的初始序号:默认为0
set-option -g display-time 5000
set-option -g repeat-time 1000
set-option -g status-keys vi
set-option -g mouse off
setw -g monitor-activity off # 非当前窗口有内容更新时在状态栏通知
# panes
set-option -g pane-base-index 1
set -g pane-border-style 'fg=red'
set -g pane-active-border-style 'fg=black bg=green'
# 在上方显示pane的编号
set -g pane-border-status bottom
set -g pane-border-lines simple
# set -g pane-border-format "#{pane_title} #{pane_index}"
set -g pane-border-format "#{pane_index} #{pane_current_path}"
# set -g pane-border-format "#{?#{>:#{window_panes},1},#{pane_index},}"
# 设置新的pane在当前目录中打开
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"
# 创建新窗口时使用之前窗口所在的目录
bind c new-window -a -c "#{pane_current_path}"
# 窗口关闭时自动排序
set-option -g renumber-windows on
# statusbar
set -g status on
set -g status-interval 1 # 状态栏刷新时间
set -g status-position bottom
set -g status-justify right
set -g status-style 'fg=red'
set -g status-left-style 'fg=black bg=yellow'
set -g status-left ' #S '
# set -g status-left-length 30
set -g status-right-style 'fg=black bg=yellow'
set -g status-right ' %H:%M:%S '
set -g status-right-length 200
# 当前窗口的状态栏风格
setw -g window-status-current-style 'fg=black bg=green'
setw -g window-status-current-format ' #I #W #F '
# 其他非当前窗口的状态栏风格
setw -g window-status-style 'fg=red bg=black'
setw -g window-status-format ' #I #[fg=white]#W #[fg=yellow]#F '
setw -g window-status-bell-style 'fg=yellow bg=red bold'
# vi模式复制内容到系统粘贴板
# 前提需要安装xclip sudo apt install xclip
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard -i"
# ----------------------------------------------------------------------------------------插件配置
# 插件管理器
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com:user/plugin'
# set -g @plugin 'git@bitbucket.com:user/plugin'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
# 插件安装
# 安装插件步骤(prefix+I)
# - Add new plugin to ~/.tmux.conf with set -g @plugin '...'
# - Press prefix + I (capital i, as in Install) to fetch the plugin.
# 卸载插件步骤(prefix+U)
# - Remove (or comment out) plugin from the list.
# - Press prefix + alt + u (lowercase u as in uninstall) to remove the plugin.
# 插件安装列表
# tmux保存会话依赖于tmux进程,因此关机重启后会话将被清空
# 目前有resurrect和continuum两个插件可以持久保存用户会话
# 无须任何配置就能够备份tmux会话中的各种细节,包括窗口、面板的顺序、布局、工作目录,运行程序等等数据
# 1. 保存的快捷键: <prefix + Ctrl-s> tmux状态栏在保存开始
# 2. 恢复的快捷键: <prefix + Ctrl-r> tmux状态栏在恢复开始
# 保存时,tmux会话的详细信息会以文本文件的格式保存到~/.tmux/resurrect目录,恢复时则从此处读取
# 由于数据文件是明文的,因此你完全可以自由管理或者编辑这些会话状态文件,如果备份频繁记得定期清除历史备份
set -g @plugin 'tmux-plugins/tmux-resurrect'
# 自定义恢复文件,如果保存了多个会话,想要恢复指定的会话,需要将其中的last指向其
set -g @resurrect-dir '~/.tmux/resurrect'
# 重载vim/neovim会话
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
# 保存和恢复pane中的内容
set -g @resurrect-capture-pane-contents 'on'
# 持续备份tmux会话
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'
# 设置自动保存会话时间间隔,单位/分钟
set -g @continuum-save-interval '30'
# 设置tmux开机自启动
# set -g @continuum-boot 'on'
# 其中copycat搜索插件支持正则表达式搜索/搜索结果高亮/预定义搜索
# 正则格式搜索: <prefix+/>
# 搜索结果高亮: 使用grep命令搜索且搜索不区分大小写
# 预定义的搜索:
# 1. 文件搜索 <prefix+ctrl-f>
# 2. URL搜索 <prefix+ctrl-u>
# 3. 数字搜索 <prefix+ctrl-d>
# 4. IP地址搜索 <prefix+alt-i>
# 当然可以使用n/N进行高亮选择进行一个个的跳转
set -g @plugin 'tmux-plugins/tmux-copycat'
# 文件目录树
# 切换到侧栏的目录树: <prefix+Tab>
# 光标移动到侧边栏上: <prefix+Backspace>
set -g @plugin 'tmux-plugins/tmux-sidebar'
# 日志记录插件
set -g @plugin 'tmux-plugins/tmux-logging'
set -g @logging-path "~/.tmux/log"
# 运行脚本安装插件
# 仅第一次安装运行即可
# run '~/.tmux/plugins/tpm/bin/install_plugins'
- 原文作者:生如夏花
- 原文链接:https://DBL2017.github.io/post/%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/tmux%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。