tmux 2 书籍笔记

Posted on Posted in tmux

以前记录过tmux使用,刚看了一本官方的书 Pragmatic.Bookshelf.tmux.2.Productive.Mouse-Free.Development.1680502212

一、基本使用

  1. 简单使用
$ ​​tmux​
$ ​​exit​
  1. 给sessions命名
$ ​​tmux​​ ​​new-session​​ ​​-s​​ ​​basic​
​$ ​​tmux​​ ​​new​​ ​​-s​​ ​​basic​
$ exit​
  1. prefix = Ctrl + b
$ tmux​

Ctrl + b t 显示时间,enter退出
  1. 简单使用
$ ​​tmux​​ ​​new​​ ​​-s​​ ​​basic​
$ top
Ctrl + b d
$ tmux list-sessions
$ tmux ls
$ tmux attach # 恢复
​$ ​​tmux​​ ​​new​​ ​​-s​​ basic001 ​​-d​ # 创建一个后台运行的
$ ​​tmux​​ ​​attach​​ ​​-t​​ ​​basic001 # 恢复会话
$ ​​tmux​​ ​​kill-session​​ ​​-t​​ ​​basic​001 # 强制关闭会话
  1. windows 工作
$ ​​tmux​​ ​​new​​ ​​-s​​ ​​windows​​ ​​-n​​ ​​shell​ # 创建一个叫windows的会话,加个标签shell
Ctrl + b c # 创建一个新的windows
Ctrl + b , # 重新命名shell标签
Ctrl + b n # 切换到下一个windows
Ctrl + b p # 切换到上一个windows
Ctrl + b 0 # 切换到0号windows
Ctrl + b 1 # 切换到1号windows
Ctrl + b w # windows 列表
Ctrl + b f # 查找windows
Ctrl + b & # 退出windows
  1. panes 工作
$ ​​tmux​​ ​​new​​ ​​-s​​ ​​panes​
Ctrl + b % # 拆分两列
Ctrl + b " # 拆分两行
Ctrl + b o # 在拆分的面板中进行切换,也可以使用上下左右键
Ctrl + b q # 显示窗格的编号

tmux 的几种默认排列

  • even-horizontal 水平堆叠所有窗格,从左到右
  • even-vertical 垂直堆叠所有窗格,从上到下
  • main-horizontal 在顶部创建一个大的窗格,下面是个小的窗格
  • main-vertical 在屏幕左侧创建一个大的窗格,并将其余的垂直放在右侧
  • tiled 均匀排列
Ctrl + b 空格 # 循环切换排列
Ctrl + b x # 退出窗格
  1. tmux的命令模式

Ctrl + b : # 进入tmux的命令模式,输入 new-window -n console, 这样就新建了一个window,这样就相当于 tmux new-window -n console 命令

new-window -n processes "top"  # 新建一个window,并运行top
  1. 帮助模式
Ctrl + b ? # 进入帮助模式

bind-key        C-b send-prefix                                                                                                                                                                                                                               [15/15]
bind-key        C-o rotate-window
bind-key        C-z suspend-client
bind-key      Space next-layout
bind-key          ! break-pane
bind-key          " split-window
bind-key          # list-buffers
bind-key          $ command-prompt -I #S "rename-session '%%'"
bind-key          % split-window -h
bind-key          & confirm-before -p "kill-window #W? (y/n)" kill-window
bind-key          ' command-prompt -p index "select-window -t ':%%'"
bind-key          ( switch-client -p
bind-key          ) switch-client -n
bind-key          , command-prompt -I #W "rename-window '%%'"
bind-key          - delete-buffer
bind-key          . command-prompt "move-window -t '%%'"
bind-key          0 select-window -t :0
bind-key          1 select-window -t :1
bind-key          2 select-window -t :2
bind-key          3 select-window -t :3
bind-key          4 select-window -t :4
bind-key          5 select-window -t :5
bind-key          6 select-window -t :6
bind-key          7 select-window -t :7
bind-key          8 select-window -t :8
bind-key          9 select-window -t :9
bind-key          : command-prompt
bind-key          ; last-pane
bind-key          = choose-buffer
bind-key          ? list-keys
bind-key          D choose-client
bind-key          L switch-client -l
bind-key          [ copy-mode
bind-key          ] paste-buffer
bind-key          c new-window
bind-key          d detach-client
bind-key          f command-prompt "find-window '%%'"
bind-key          i display-message
bind-key          l last-window
bind-key          n next-window
bind-key          o select-pane -t :.+
bind-key          p previous-window
bind-key          q display-panes
bind-key          r refresh-client
bind-key          s choose-session
bind-key          t clock-mode
bind-key          w choose-window
bind-key          x confirm-before -p "kill-pane #P? (y/n)" kill-pane
bind-key          { swap-pane -U
bind-key          } swap-pane -D
bind-key          ~ show-messages
bind-key      PPage copy-mode -u
bind-key -r      Up select-pane -U
bind-key -r    Down select-pane -D
bind-key -r    Left select-pane -L
bind-key -r   Right select-pane -R
bind-key        M-1 select-layout even-horizontal
bind-key        M-2 select-layout even-vertical
bind-key        M-3 select-layout main-horizontal
bind-key        M-4 select-layout main-vertical
bind-key        M-5 select-layout tiled
bind-key        M-n next-window -a
bind-key        M-o rotate-window -D
bind-key        M-p previous-window -a
bind-key -r    M-Up resize-pane -U 5
bind-key -r  M-Down resize-pane -D 5
bind-key -r  M-Left resize-pane -L 5
bind-key -r M-Right resize-pane -R 5
bind-key -r    C-Up resize-pane -U
bind-key -r  C-Down resize-pane -D
bind-key -r  C-Left resize-pane -L
bind-key -r C-Right resize-pane -R

二、配置tmux

$ ​​touch​​ ​​~/.tmux.conf​

# 设置前缀为 Ctrl+a
​set -g prefix C-a
# 释放 Ctrl+b
unbind C-b
# 重新加载 配置文件
ctrl+b : # 进入命令模式,然后输入 source-file ~/.tmux.conf
# 设置tmux的延迟,文档说当有干扰的时候可以设置这个参数,比如影响vim编辑的时候
set -s escape-time 1
# 设置初始的window编号为1,默认是0,强迫症必选
set -g base-index 1
# 设置初始的窗格编号为1,默认是0
setw -g pane-base-index 1
# 绑定r键,进行重新加载配置文件。也就是说,Ctrl+b r 就可以重新加载,不用进入命令模式
bind r source-file ~/.tmux.conf
# 使用display参数,加载成功后,会显示Reloaded
​bind r source-file ~/.tmux.conf ​\;​ display ​"Reloaded!"​
# 定义一个不需要前缀的快捷键,小心使用,这里会禁用组合键
bind-key -n C-r source-file ~/.tmux.conf
# 解决冲突,可以在其他应用中使用Ctrl+a,使用的时候,按Ctrl+a 两次
bind C-a send-prefix

​# 绑定 | - 切分窗格
​bind | split-window -h
​bind - split-window -v

​# 绑定h,j,k,l​键,上下左右切换窗格
​bind h ​select​-pane -L
​bind j ​select​-pane -D
​bind k ​select​-pane -U
​bind l ​select​-pane -R


bind -r C-h ​select​-window -t :-
​bind -r C-l ​select​-window -t :+

# 绑定调整窗格大小
bind H resize-pane -L 5
​bind J resize-pane -D 5
​bind K resize-pane -U 5
​bind L resize-pane -R 5

# Ctrl + b H 后可以在延迟时间内,多次按H。-r 代表重复
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

# 鼠标支持开关
​set -g mouse off

# 显示颜色
set -g default-terminal ​"screen-256color"​

# 状态栏
set -g status-style fg=white,bg=black
# 或
set -g status-style fg=white,bold,bg=black

# window list​ 颜色
setw -g window-status-style fg=cyan,bg=black
# 或
setw -g window-status-current-style fg=white,bold,bg=red

​# 窗格 borders​
​setw -g pane-border-style fg=green,bg=black
​setw -g pane-active-border-style fg=white,bg=yellow
# 或
​# active pane normal, other shaded out​
​setw -g window-style fg=colour240,bg=colour235
​setw -g window-active-style fg=white,bg=black

# Command / message line​
set -g message-style fg=white,bold,bg=black

状态条选项

#H  Hostname of local host
#h  Hostname of local host without the domain name
#F  Current window flag
#I  Current window index
#P  Current pane index
#S  Current session name
#T  Current window title
#W  Current window name
##  A literal #
#(shell-command)    First line of the shell command’s output
#[attributes]   Color or attribute change

set -g status-left ​"#S"​
​set -g status-left ​"#[fg=green]#S"​
set -g status-left ​"#[fg=green]#S #[fg=yellow]#I #[fg=cyan]#P"​
# Status line left side to show Session:window:pane​
set -g status-left-length 40
set -g status-left ​"#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P"​
​# Status line right side -  31-Oct 13:37​
​set -g status-right ​"#[fg=cyan]%d %b %R"​

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


​# Center the window list in the status line​
​set -g status-justify centre

​# enable activity alerts​
​setw -g monitor-activity on
​set -g visual-activity on

小结说明


Commands to control tmux’s behavior
Command                                                     Description
        
set -g prefix C-a                                           Sets the key combination for the Prefix key.
set -sg escape-time n                                       Sets the amount of time (in milliseconds) tmux waits for a keystroke after pressing Prefix.
set -g base-index 1                                         Sets the base index for windows to 1 instead of 0.
setw -g pane-base-index 1                                   Sets the base index for panes to 1 instead of 0.
source-file [file]                                          Loads a configuration file. Use this to reload the existing configuration or bring in additional configuration options later.
bind C-a send-prefix                                        Configures tmux to send the prefix when pressing the Prefix combination twice consecutively.
bind-key [key] [command]                                    Creates a keybinding that executes the specified command. Can be shortened to bind.
bind-key -r [key] [command]                                 Creates a keybinding that is repeatable, meaning you only need to press the Prefix key once, and you can press the assigned key repeatedly afterwards. 
                                                            This is useful for commands where you want to cycle through elements or resize panes. Can be shortened to bind.
unbind-key [key]                                            Removes a defined keybinding so it can be bound to a different command. Can be shortened to unbind.
display-message or display                                  Displays the given text in the status message.
set-option [flags] [option] [value]                         Sets options for sessions. Using the -g flag sets the option for all sessions.
set-window-option [option] [value]                          Sets options for windows, such as activity notifications, cursor movement, or other elements related to windows and panes.
set -a                                                      Appends values onto existing options rather than replacing the option’s value.
set -g mouse off                                            Disables mouse support in tmux. Set to on if you wish to use the mouse.
set -g default-terminal "screen-256color"                   Defines the terminal type for windows. Sets the value of TERM, which other programs will use. screen-256color ensures the widest compatibility with programs originally written for the screen program.
        
        
        
Commands to control tmux’s appearance       
Command Description     
        
set -g status-style                                         Sets the foreground and background color for the status line. Supports the options dim, bright (or bold), reverse, and blink in addition to colors.
                                                            Example: set -g status-style fg=white,bold,bg=black
setw -g window-status-style                                 Sets the foreground and background color of the window list in the status line. Uses the same options as status-style.
setw -g window-status-current-style                         Sets the foreground and background color of the active window in the window list in the status line. Uses the same options as status-style.
setw -g window-status-activity-style                        Sets the foreground and background color of any window with background activity. Uses the same options as status-style.
setw -g pane-border-style                                   Sets the foreground and background color of the pane borders. Uses the same options as status-style.
setw -g pane-active-border-style                            Sets the foreground and background color of the active pane’s border. Uses the same options as status-style.
setw -g window-style                                        Sets the foreground and background color of the window. Uses the same options as status-style.
setw -g window-active-style                                 Sets the foreground and background color of the active window. Uses the same options as status-style.
setw -g message-style                                       Sets the foreground and background color of the message area and tmux command line. Uses the same options as status-style.
set -g status-length-left and set -g status-length-right    Controls the number of visible characters in the left and right sides of the status line.
set -g status-left and set -g status-right                  Configures the items that appear in the left and right sides of the status line.
set -g status-interval n                                    Defines the refresh interval for the status line, where n is the number of seconds between refreshes.
set -g status-justify centre                                Centers the window list in the status line.
setw -g monitor-activity on                                 Looks for activity in other windows and highlights the name of the window with background activity.
set -g visual-activity on                                   Displays a message in the message area when there is activity in another window.

三、定制tmux脚本环境

$ ​​tmux​​ ​​new-session​​ ​​-s​​ ​​development​
​$ ​​tmux​​ ​​split-window​​ ​​-h​​ ​​-t​​ ​​development​
​$ ​​tmux​​ ​​attach​​ ​​-t​​ ​​development​
$ ​​tmux​​ ​​split-window​​ ​​-v​​ ​​-t​​ ​​development​
​$ ​​touch​​ ​​~/development​
​$ ​​chmod​​ ​​+x​​ ​​~/development​

# 新建一个window
tmux new-session -s development -n editor -d
# 向development中发送命令,并回车(C-m)
tmux send-keys -t development ​'cd ~/devproject'​ C-m
tmux send-keys -t development ​'vim'​ C-m
# 切分窗格
tmux split-window -v -t development
tmux split-window -v -p 10 -t development
tmux ​select​-layout -t development main-horizontal
tmux send-keys -t development:1.2 ​'cd ~/devproject'​ C-m

​tmux new-window -n console -t development
​tmux send-keys -t development:2 ​'cd ~/devproject'​ C-m

tmux ​select​-window -t development:1
​tmux attach -t development

一个脚本

tmux has-session -t development
​if​ [ $? != 0 ]
​then​
​  ​tmux new-session -s development -n editor -d
​  tmux send-keys -t development ​'cd ~/devproject'​ C-m
​  tmux send-keys -t development ​'vim'​ C-m
​  tmux split-window -v -t development
​  tmux ​select​-layout -t development main-horizontal
​  tmux send-keys -t development:1.2 ​'cd ~/devproject'​ C-m
​  tmux new-window -n console -t development
​  tmux send-keys -t development:2 ​'cd ~/devproject'​ C-m
​  tmux ​select​-window -t development:1
​fi​
​tmux attach -t development

自定义tmux的配置环境

$ cat scripting/app.conf
​source-file ~/.tmux.conf
​new-session -s development -n editor -d
​send-keys -t development ​'cd ~/devproject'​ C-m
​send-keys -t development ​'vim'​ C-m
​split-window -v -t development
​select​-layout -t development main-horizontal
​send-keys -t development:1.2 ​'cd ~/devproject'​ C-m
​new-window -n console -t development
​send-keys -t development:2 ​'cd ~/devproject'​ C-m
​select​-window -t development:1

# 使用
$ ​​tmux​​ ​​-f​​ ​​app.conf​​ ​​attach​

四、复制编辑

Ctrl + b [ # 进入复制模式,enter退出
Ctrl + f # 上翻页
Ctrl + b # 下翻页

tmux.conf # 设置vi模式,可以使用hjkl
​# enable vi keys.​
​setw -g mode-keys vi

小结说明

Shortcut keys
Shortcut    Description
Prefix [            Enters Copy mode.
Prefix ]            Pastes current buffer contents.
Prefix =            Lists all paste buffers and pastes selected buffer contents.

Copy mode movement keys (vi mode)
Command Description

h, j, k, and l      Moves the cursor left, down, up, and right, respectively.
w                   Moves the cursor forward one word at a time.
b                   Moves the cursor backward one word at a time.
f                   followed by any character
                    Moves to the next occurrence of the specified character.
F                   followed by any character
                    Moves to the previous occurrence of the specified character.
Ctrl-b              Scrolls up one page.
Ctrl-f              Scrolls down one page.
g                   Jumps to the top of the buffer.
G                   Jumps to the bottom of the buffer.
?                   Starts a search backward through the buffer.
/                   Starts a search forward through the buffer.

Commands
Command Description

show-buffer             Displays current buffer contents.
capture-pane            Captures the selected pane’s visible contents to a new buffer.
list-buffers            Lists all paste buffers.
choose-buffer           Shows paste buffers and pastes the contents of the one you select.
save-buffer [filename]  Saves the buffer’s contents to the specified file.

附上一个配置

tmux.conf
​# Setting the prefix from C-b to C-a​
​set -g prefix C-a
​#​
​# Free the original Ctrl-b prefix keybinding​
​unbind C-b
​#​
​#setting the delay between prefix and command​
​set -s escape-time 1
​#​
​# Ensure that we can send Ctrl-A to other apps​
​bind C-a send-prefix
​
​# 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!"​
​
​# splitting panes with | and -​
​bind | split-window -h
​bind - split-window -v
​
​# moving between panes with Prefix h,j,k,l​
​bind h ​select​-pane -L
​bind j ​select​-pane -D
​bind k ​select​-pane -U
​bind l ​select​-pane -R
​# Quick window selection​
​bind -r C-h ​select​-window -t :-
​bind -r C-l ​select​-window -t :+
​
​# Pane resizing panes with Prefix H,J,K,L​
​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 - set to on if you want to use the mouse​
​set -g mouse off
​
​# Set the default terminal mode to 256color mode​
​set -g default-terminal ​"screen-256color"​
​
​# set the status line's colors​
​set -g status-style fg=white,bg=black
​
​# set the color of the window list​
​setw -g window-status-style fg=cyan,bg=black
​
​# set colors for the active window​
​setw -g window-status-current-style fg=white,bold,bg=red
​
​# colors for pane borders​
​setw -g pane-border-style fg=green,bg=black
​setw -g pane-border-active-style fg=white,bg=yellow
​
​# active pane normal, other shaded out​
​setw -g window-style fg=colour240,bg=colour235
​setw -g window-active-style fg=white,bg=black
​
​# Command / message line​
​setw -g message-style fg=white,bold,bg=black
​
​# Status line left side to show Session:window:pane​
​set -g status-left-length 40
​set -g status-left ​"#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P"​
​
​# Status line right side -  50% | 31 Oct 13:37​
​set -g status-right ​"#(~/battery Discharging) | #[fg=cyan]%d %b %R"​
​
​# Update the status line every sixty seconds​
​set -g status-interval 60
​
​# Center the window list in the status line​
​set -g status-justify centre
​
​# enable activity alerts​
​setw -g monitor-activity on
​set -g visual-activity on
​
​# enable vi keys.​
​setw -g mode-keys vi
​
​# shortcut for synchronize-panes toggle​
​bind C-s set-window-option synchronize-panes
​
​# split pane and retain the current directory of existing pane​
​bind _ split-window -v -c  ​"#{pane_current_path}"​
​bind ​\ ​split-window -h -c ​"#{pane_current_path}"​
​
​# Log output to a text file on demand​
​bind P pipe-pane -o ​"cat >>~/#W.log"​ ​\;​ display ​"Toggled logging to ~/#W.log"​
​#​
​# Load mac-specific settings​
​if​-shell ​"uname | grep -q Darwin"​ ​"source-file ~/.tmux.mac.conf"​
​
​# load private settings if they exist​
​if​-shell ​"[ -f ~/.tmux.private]"​ ​"source ~/.tmux.private"​
​
​is_vim=​"ps -o state= -o comm= -t '#{pane_tty}' ​​\​
​    | grep -iqE '^[^TXZ ]+ +(​​\\​​S+​​\\​​/)?g?(view|n?vim?x?)(diff)?​$'​"​
​bind-key -n C-h ​if​-shell ​"​$is_vim​"​ ​"send-keys C-h"​  ​"select-pane -L"​
​bind-key -n C-j ​if​-shell ​"​$is_vim​"​ ​"send-keys C-j"​  ​"select-pane -D"​
​bind-key -n C-k ​if​-shell ​"​$is_vim​"​ ​"send-keys C-k"​  ​"select-pane -U"​
​bind-key -n C-l ​if​-shell ​"​$is_vim​"​ ​"send-keys C-l"​  ​"select-pane -R"​
​bind-key -n C-​\ ​​if​-shell ​"​$is_vim​"​ ​"send-keys C-​​\\​​"​ ​"select-pane -l"​
​
​bind C-l send-keys ​'C-l'​
​
​set -g @plugin ​'tmux-plugins/tpm'​
​set -g @plugin ​'tmux-plugins/tmux-resurrect'​
​run ​'~/.tmux/plugins/tpm/tpm'​
» 转载请注明来源:若我若鱼 » tmux 2 书籍笔记

Leave a Reply

Your email address will not be published. Required fields are marked *

three + seventeen =