当前位置:首页>技术文档>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 号会话。

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

新建一个指定名称的会话

$ tmux new -s <session-name>

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

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

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

查看当前所有的 Tmux 会话。

$ tmux ls
# or
$ tmux list-session

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

# 使用会话编号
$ tmux attach -t 0

# 使用会话名称
$ tmux attach -t <session-name>

# 如果只有一个会话 直接这样就行了
$ tmux a
复制粘贴搜索(常用)
#log关键字搜索
Ctrl+ t [
#按空格键+方向键开始选择复制内容,按回车Enter结束选择复制内容
Ctrl + t ] 粘贴
#搜索:/关键字
#重命名window:Ctrl +t ,

 

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

# 使用会话编号
$ tmux kill-session -t 0

# 使用会话名称
$ tmux kill-session -t <session-name>

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

# 使用会话编号
$ tmux switch -t 0

# 使用会话名称
$ tmux switch -t <session-name>

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

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

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

 

新建窗口

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

# 对应的快捷键
ctrl + t c

切换窗口
# 切换到指定编号的窗口
$ tmux select-window -t <window-number>

# 切换到指定名称的窗口
$ tmux select-window -t <window-name>

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

重命名窗口

$ 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 命令用来划分窗格。

# 划分上下两个窗格
$ tmux split-window

# 划分左右两个窗格
$ tmux split-window -h

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

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

# 光标切换到上方窗格
$ tmux select-pane -U

# 光标切换到下方窗格
$ tmux select-pane -D

# 光标切换到左边窗格
$ tmux select-pane -L

# 光标切换到右边窗格
$ tmux select-pane -R

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

 

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

# Setting the prefix from C-b to C-t
set -g prefix C-t

# Free the original Ctrl-b prefix keybinding
unbind C-b

# Setting the delay between prefix and command
set -s escape-time 2

# Ensure that we can send Ctrl-a to other apps
bind C-t send-prefix

# history buffer - max number of lines for each window
set -g history-limit 10000

# Set Terminal Emulator Titles - OFF by default
set -g set-titles on

# Set the base index for windows to 1 instead of 0
set -g base-index 1

# Set the base index for panes to 1 instead of 0
setw -g pane-base-index 1


# Reload the file with Prefix r
bind r source-file ~/.tmux.conf \; display "Reloaded Config!!!"

# Splitting panes replace % and "
bind | split-window -h
bind - split-window -v

# moving between panes
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R


# Pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Mouse support
# setw -g mode-mouse on
# set -g mouse-select-pane off
# set -g mouse-resize-pane off
# set -g mouse-select-window off
# incompatible since tmux 2.1
set -g mouse on

# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"

# Enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# tmux coloring
# Set the status line's colors
set -g status-fg black
set -g status-bg default

# Set the color of the window list
setw -g window-status-fg black
setw -g window-status-bg default
setw -g window-status-attr dim

# Set colors for the active window
setw -g window-status-current-fg white
setw -g window-status-current-bg red
setw -g window-status-current-attr bright

# 面板之间的边框的颜色
set -g pane-border-fg red
set -g pane-border-bg default
set -g pane-active-border-fg red
set -g pane-active-border-bg default

# Command / message line
set -g message-fg white
set -g message-bg black
set -g message-attr bright


set -g status-left-length 70
set -g status-left "#[fg=red][会话#S]#[fg=green][#(echo $USER)@#(hostname -I|tr -d ' ')]#[fg=blue][窗口#I]#[fg=blue][面板#P] "

# show session name, window & pane number, date and time on right side of
# status bar
set -g status-right-length 60
set -g status-right "#[fg=green]#(date '+%Y年%m月%d日 %H:%M')"

# Update the status bar every sixty seconds
set -g status-interval 60

# Center the window list
set -g status-justify eentre

# Enable vi keys
setw -g mode-keys vi

# set shell 如果配置了zsh 可以打开下面的配置
# set -g default-shell /bin/zsh

给TA鼓励
共{{data.count}}人
人已鼓励
技术文档

高通CamX初始化流程

2022-5-7 0:08:24

技术文档

Android.mk详解

2022-5-7 0:09:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索