当前位置:首页>技术文档>Linux终端复用工具Tmux使用教程

Linux终端复用工具Tmux使用教程

为什么要使用tmux:唯一的强大理由,保持永久连接!

如果有使用过vnc的同学,大家可以把tmux想象成一个字符模式的vnc(没有图形界面,通过控制台操作)

举例使用场景:

当大家用ssh连接服务器编译android固件时,突然断网或者本地电脑突然死机了,编译就被中断了,如果你用了tmux就不会有这种烦恼了
或者晚上想编译固件(可能需要2-3个小时),打算明天来看看结果,还想把本地的电脑关掉,这时可以用tmux
或者在公司编译固件,想在家里远程vpn链接看看编译结果(中间有没出错拉?),这时就可以用tmux
或着在公司调试固件,遇到问题,想让深圳的同学,看看问题,这时可以用tmux分享控制台终端

tmux很方便开多个控制终端。如:左上窗口进行android的编译,右上窗口进行代码的编辑,下面打开了一个htop监控系统资源状况。

 

新建会话 (常用)
第一个启动的 Tmux 窗口,编号是0,第二个窗口的编号是1,以此类推。这些窗口对应的会话,就是 0 号会话、1 号会话。

使用编号区分会话,不太直观,更好的方法是为会话起名。

新建一个指定名称的会话

  1. $ tmux new -s <session-name>

分离会话(常用)
在 Tmux 窗口中,按下 Ctrl+t d 或者输入tmux detach 命令,就会将当前会话与窗口分离。

  1. # 在tmux的窗口中输入 这个 tmux detach 命令
  2. $ tmux detach

上面命令执行后,就会退出当前 Tmux 窗口,但是会话和里面的进程仍然在后台运行。

查看当前所有的 Tmux 会话。

  1. $ tmux ls
  2. # or
  3. $ tmux list-session

接入会话(常用)
tmux attach 命令用于重新接入某个已存在的会话。

  1. # 使用会话编号
  2. $ tmux attach -t 0
  3.  
  4. # 使用会话名称
  5. $ tmux attach -t <session-name>
  6.  
  7. # 如果只有一个会话 直接这样就行了
  8. $ tmux a
  1. 复制粘贴搜索(常用)
  2. #log关键字搜索
  3. Ctrl+ t [
  4. #按空格键+方向键开始选择复制内容,按回车Enter结束选择复制内容
  5. Ctrl + t ] 粘贴
  6. #搜索:/关键字
  7. #重命名window:Ctrl +t ,

 

杀死会话 (选学)
tmux kill-session 命令用于杀死某个会话。

  1. # 使用会话编号
  2. $ tmux kill-session -t 0
  3.  
  4. # 使用会话名称
  5. $ tmux kill-session -t <session-name>

切换会话 (选学)
tmux switch 命令用于切换会话。

  1. # 使用会话编号
  2. $ tmux switch -t 0
  3.  
  4. # 使用会话名称
  5. $ tmux switch -t <session-name>

重命名会话 (选学)
tmux rename-session命令用于重命名会话。

  1. $ tmux rename-session -t 0 <new-name>
  2. 默认打开的会话名 0 这里重命名成 my 拉。

会话快捷键
Ctrl+t d:分离当前会话。
Ctrl+t s:列出所有会话。
Ctrl+t $:重命名当前会话。
下面介绍 窗口操作。窗口叫 window

 

新建窗口

  1. # 新建一个指定名称的窗口
  2. $ tmux new-window -n <window-name>

# 对应的快捷键
ctrl + t c

  1. 切换窗口
  2. # 切换到指定编号的窗口
  3. $ tmux select-window -t <window-number>
  4.  
  5. # 切换到指定名称的窗口
  6. $ tmux select-window -t <window-name>

# 对应的快捷键
ctrl + t <number> # 在下面的状态栏都有每个窗口的编号。

重命名窗口

  1. $ tmux rename-window <new-name>

# 对应的快捷键
ctrl + t , # 英文逗号。
窗口快捷键

Ctrl+t c:创建一个新窗口,状态栏会显示多个窗口的信息。
Ctrl+t p:切换到上一个窗口(按照状态栏上的顺序)。
Ctrl+t n:切换到下一个窗口。
Ctrl+t <number>:切换到指定编号的窗口,其中的<number>是状态栏上的窗口编号。
Ctrl+t w:从列表中选择窗口。
Ctrl+t ,:窗口重命名。

 

窗格操作(pane)

划分窗格
tmux split-window 命令用来划分窗格。

  1. # 划分上下两个窗格
  2. $ tmux split-window
  3.  
  4. # 划分左右两个窗格
  5. $ tmux split-window -h

# 对应的快捷键
ctrl + t - 水平分割
ctrl + t | 竖直分割

移动光标
tmux select-pane 命令用来移动光标位置。

  1. # 光标切换到上方窗格
  2. $ tmux select-pane -U
  3.  
  4. # 光标切换到下方窗格
  5. $ tmux select-pane -D
  6.  
  7. # 光标切换到左边窗格
  8. $ tmux select-pane -L
  9.  
  10. # 光标切换到右边窗格
  11. $ tmux select-pane -R

# 对应的快捷键
ctrl + t h,j,k,l # vim的上下左右键

 

以下是我的一个 配置文件,前缀键 是 ctrl + t

  1. # Setting the prefix from C-b to C-t
  2. set -g prefix C-t
  3.  
  4. # Free the original Ctrl-b prefix keybinding
  5. unbind C-b
  6.  
  7. # Setting the delay between prefix and command
  8. set -s escape-time 2
  9.  
  10. # Ensure that we can send Ctrl-a to other apps
  11. bind C-t send-prefix
  12.  
  13. # history buffer - max number of lines for each window
  14. set -g history-limit 10000
  15.  
  16. # Set Terminal Emulator Titles - OFF by default
  17. set -g set-titles on
  18.  
  19. # Set the base index for windows to 1 instead of 0
  20. set -g base-index 1
  21.  
  22. # Set the base index for panes to 1 instead of 0
  23. setw -g pane-base-index 1
  24.  
  25.  
  26. # Reload the file with Prefix r
  27. bind r source-file ~/.tmux.conf \; display "Reloaded Config!!!"
  28.  
  29. # Splitting panes replace % and "
  30. bind | split-window -h
  31. bind - split-window -v
  32.  
  33. # moving between panes
  34. bind h select-pane -L
  35. bind j select-pane -D
  36. bind k select-pane -U
  37. bind l select-pane -R
  38.  
  39.  
  40. # Pane resizing
  41. bind -r H resize-pane -L 5
  42. bind -r J resize-pane -D 5
  43. bind -r K resize-pane -U 5
  44. bind -r L resize-pane -R 5
  45.  
  46. # Mouse support
  47. # setw -g mode-mouse on
  48. # set -g mouse-select-pane off
  49. # set -g mouse-resize-pane off
  50. # set -g mouse-select-window off
  51. # incompatible since tmux 2.1
  52. set -g mouse on
  53.  
  54. # Set the default terminal mode to 256color mode
  55. set -g default-terminal "screen-256color"
  56.  
  57. # Enable activity alerts
  58. setw -g monitor-activity on
  59. set -g visual-activity on
  60.  
  61. # tmux coloring
  62. # Set the status line's colors
  63. set -g status-fg black
  64. set -g status-bg default
  65.  
  66. # Set the color of the window list
  67. setw -g window-status-fg black
  68. setw -g window-status-bg default
  69. setw -g window-status-attr dim
  70.  
  71. # Set colors for the active window
  72. setw -g window-status-current-fg white
  73. setw -g window-status-current-bg red
  74. setw -g window-status-current-attr bright
  75.  
  76. # 面板之间的边框的颜色
  77. set -g pane-border-fg red
  78. set -g pane-border-bg default
  79. set -g pane-active-border-fg red
  80. set -g pane-active-border-bg default
  81.  
  82. # Command / message line
  83. set -g message-fg white
  84. set -g message-bg black
  85. set -g message-attr bright
  86.  
  87.  
  88. set -g status-left-length 70
  89. set -g status-left "#[fg=red][会话#S]#[fg=green][#(echo $USER)@#(hostname -I|tr -d ' ')]#[fg=blue][窗口#I]#[fg=blue][面板#P] "
  90.  
  91. # show session name, window & pane number, date and time on right side of
  92. # status bar
  93. set -g status-right-length 60
  94. set -g status-right "#[fg=green]#(date '+%Y年%m月%d日 %H:%M')"
  95.  
  96. # Update the status bar every sixty seconds
  97. set -g status-interval 60
  98.  
  99. # Center the window list
  100. set -g status-justify eentre
  101.  
  102. # Enable vi keys
  103. setw -g mode-keys vi
  104.  
  105. # set shell 如果配置了zsh 可以打开下面的配置
  106. # set -g default-shell /bin/zsh
免责声明:
1. 本网站全部内容仅供用户学习、研究或欣赏使用。不得用于商业或其他非法目的。
2. 本站对所有下载内容不承担任何技术及版权问题的责任。用户下载和使用本站提供的软件,需自行承担风险以及相关法律责任,并请自行判断其安全性和完整性。
3. 本站内对文章打赏行为均属自愿,本站不提供额外支持。
4. 如因使用本站内容造成损失或损害,本站概不负责,亦不承担任何法律责任。一旦访问或下载本站内容,就代表您已同意上述条款。
技术文档

高通CamX初始化流程

2022-5-7 0:08:24

技术文档

Android.mk详解

2022-5-7 0:09:16

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索