2025年7月9日 19:50:24 星期三

tmux使用奇技淫巧

最近在用screen,后来发现,tmux更好用啊。这是一个笔记。
参考资料:
https://cloud.tencent.com/developer/article/1195836

安装

  1. sudo apt-get install tmux

配置

配置快捷键

  1. #首先创建几个alias
  2. vi ~/.zshrc
  3. alias tls="tmux ls"
  4. alias ta="tmux attach -t "
  5. alias tk="tmux kill-session -t"
  6. alias ts="tmux switch -t "
  7. alias tn="tmux new -s"

编辑配置文件

  1. touch ~/.tmux.conf
  2. vi ~/.tmux.conf

内容:
本配置中,prefix被重设为ctrl+x

  1. set -g status-bg colour14
  2. # 新版配置,配置当前窗口的颜色
  3. setw -g window-status-current-style fg=red,bg=white
  4. set -g window-status-format '#I:#{=-10:window_name}'
  5. set -g window-status-current-format '#I:#{=-10:window_name}'
  6. # 老配置,配置当前窗口的颜色
  7. # set -g window-status-current-fg red
  8. # set -g window-status-current-bg white
  9. # 这是为了在使用ctrl+x+r的时候,重新把窗口的编号重排
  10. move-window -r
  11. set-option -g escape-time 50
  12. set -g default-terminal "screen-256color"
  13. set -g base-index 1 # 窗口从1开始计数
  14. set -g history-limit 10000 # 设置历史记录最大条数
  15. setw -g mode-keys vi # 设置按键模式为 Vi 模式
  16. set -wg window-status-separator ""
  17. set-window-option -g automatic-rename off
  18. set-option -g allow-rename off
  19. # 复制模式下使用系统剪贴板
  20. bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -se c -i'
  21. # 旧版本 复制模式下使用系统剪贴板
  22. # bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'
  23. # mac 复制模式下使用系统剪贴板
  24. # bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'
  25. # 重新载入配置
  26. bind r source-file ~/.tmux.conf \; display "Reloaded!"
  27. # 以下配置为取消原有 hjkl 键的绑定
  28. # 并绑定 Pane 选择,和 Vim 操作一致
  29. unbind-key h
  30. bind-key h select-pane -L
  31. unbind-key j
  32. bind-key j select-pane -D
  33. unbind-key k
  34. bind-key k select-pane -U
  35. unbind-key l
  36. bind-key l select-pane -R
  37. bind-key u command-prompt -p "join pane from:" "join-pane -s '%%'"
  38. bind-key o command-prompt -p "send pane to:" "join-pane -t '%%'"
  39. # 切换窗格大小
  40. bind Down resize-pane -D 5
  41. bind Right resize-pane -R 5
  42. bind Left resize-pane -L 5
  43. bind Up resize-pane -U 5
  44. # 在远端和本地使用shift进行切换,套娃
  45. # 参考:http://stahlke.org/dan/tmux-nested/
  46. unbind C-b
  47. set -g prefix C-x
  48. bind -n C-t new-window -a
  49. bind -n C-h prev
  50. bind -n C-l next
  51. bind -n S-left swap-window -t -1
  52. bind -n S-right swap-window -t +1
  53. bind -n M-F11 set -qg status-bg colour4
  54. bind -n M-F12 set -qg status-bg colour14
  55. bind -n C-k \
  56. send-keys M-F12 \; \
  57. set -qg status-bg colour4 \; \
  58. unbind -n C-h \; \
  59. unbind -n C-l \; \
  60. unbind -n S-left \; \
  61. unbind -n S-right \; \
  62. unbind -n C-t \; \
  63. set -qg prefix C-b
  64. bind -n C-j \
  65. send-keys M-F11 \; \
  66. set -qg status-bg colour14 \; \
  67. bind -n C-h prev \; \
  68. bind -n C-l next \; \
  69. bind -n S-left swap-window -t -1 \\\; prev \; \
  70. bind -n S-right swap-window -t +1 \\\; next \; \
  71. bind -n C-t new-window -a -c "#{pane_current_path}" \; \
  72. set -qg prefix C-x
  73. # 覆盖原来的 ctrl+x+(/) 作为切换前一个,后一个session的配置,现在直接ctrl+x+9/0
  74. bind '0' switch-client -n
  75. bind '9' switch-client -p

下面具体解释一下这个配置文件。

复制到系统剪贴板

网上的教程是使用xclip,首先要安装xclip,此时,在copy模式下,按空格选取,再按一下y。就复制到系统剪贴板了。

子窗口管理

  1. # 创建子窗口
  2. ctrl+x c
  3. # 显示列表
  4. ctrl+x w
  5. # 关闭子窗口
  6. ctrl+x x
  7. # 修改窗口名
  8. ctrl+x ,

分屏

用这个软件,就是用来用分屏玩的。

  1. # 注意已经把唤醒键改成了ctrl+x
  2. # 上下分屏
  3. ctrl+x "
  4. # 左右分屏
  5. ctrl+x %
  6. # 此时可以用ctrl+x h/j/k/l 来切换分屏了

分屏后的窗格管理

有时,已经打开了N个window。(比如窗口 0,1),并且通过分屏想将其中的部分窗格自由组合。可以这样操作:
首先对应配置文件为,它会让pane(窗格)有join/send的功能。

  1. bind-key u command-prompt -p "join pane from:" "join-pane -s '%%'"
  2. bind-key o command-prompt -p "send pane to:" "join-pane -t '%%'"

如果想把1号window加入到0号window,使之成为0号window的一部分。

  1. 切换到window0,使用ctrl+h/l
  2. 此时按下 ctrl+x u,输入:1
  3. window1从远端加入到window0成为一个pane。window1随即关闭。
    1. | pane0 |
    2. |----------------|
    3. | pane1 |

如果想将pane1作为一个独立的window

  1. 先创建一个新window。ctrl +t。假设这个window的标号为1
  2. 切换为window0。此时window0有两个窗格:pane0,pane1。如下图所示。
    1. | pane0 |
    2. |----------------|
    3. | pane1 |
  3. 使用ctrl+x+k将光标切换到pane1,按快捷键ctrl+x o,进入send模式,输入:1。此时pane1被移动到了window 1,成为它的一个pane。window0的pane1消失。

在远端和本地进行切换

tmux的强大之处在于,可以在远程的机器上装上tmux,类似于套娃的玩法。
效果图:

上面的 0:bash 1:bash window是远程的。下面的9个window是本地的。
但是套娃的切换窗口是个问题。国外大神给出了解决方案:
http://stahlke.org/dan/tmux-nested/

使用本文的配置,可以使用ctrl+h/l 切换window(左右切换)。也可以使用ctrl + j/k切换是本地还是远程(上下切换)。谁用谁知道。

去掉填充屏幕的小圆点

有时在不同的终端登录同一个tmux,会发生屏幕适配的问题。空白屏幕会被小圆点填充。
此时,在命令模式下:

  1. :a -d

来自 大脸猪 写于 2020-03-16 18:24 -- 更新于2021-11-17 17:11 -- 1 条评论

1条评论

字体
字号


评论:

● 来自 超级大猪 写于 2020-07-31 11:28 回复

我是一只大猪头。