天天看點

bash配置tmux來顯示tmux ssh的狀态

需求: 在tmux裡面連結ssh的時候, 如果存在多個ssh主機, 想要顯示對應的主機ip以示甄别

實作效果

bash配置tmux來顯示tmux ssh的狀态

編輯你的bashrc, 因為我習慣在root下操作, 是以/root/.bashrc, 你如果習慣用user, 那就編輯/home/你的使用者名/.bashrc

增加

ssh() {

    if [ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" = "tmux" ]; then

            tmux rename-window "$*"

            command ssh "[email protected]"

            tmux set-window-option automatic-rename "on" 1>/dev/null

    else

            command ssh "[email protected]"

    fi

}

到你的bashrc

儲存後即可。唯一需要注意的是,因為我習慣ssh -參數 參數值 [email protected] 這樣來通路, 因為修改了ssh的這個,導緻參數值會無法正常識别。這裡把參數參數值放到後面即可。

附tmux.conf配置

#bind r source-file ~/.tmux.conf \;display "Reloaded!"

# #      重置預設定

set -g prefix C-a               #設定指令為ctrl-a觸發prefix

unbind C-b                      #解綁ctrl-b

bind C-a send-prefix            #設定預指令為prefix

# #      面闆分割

unbind '"'

bind | split-window -h          #左右分割

unbind %

bind - split-window -v          #上下分割

bind k select-pane -U           #選擇上視窗 shift+k

bind j select-pane -D           #選擇下視窗

bind h select-pane -L           #選擇左視窗

bind l select-pane -R           #選擇右視窗

bind K resize-pane -U 5         #窗格上移5  (Ctrl-k一起按)

bind J resize-pane -D 5         #窗格下移5

bind H resize-pane -L 5         #視窗左移5

bind L resize-pane -R 5         #視窗右移5

bind -r K resize-pane -U 5      #視窗重複操作

bind -r J resize-pane -D 5      #視窗重複操作

bind -r H resize-pane -L 5      #視窗重複操作

bind -r L resize-pane -R 5      #視窗重複操作

bind ^u swapp -U                #與上窗格交換   (Ctrl-u)

bind ^d swapp -D                #與下窗格交換   (Ctrl-u)

# # enable vi keys.

setw -g mode-keys vi            #相容vi按鍵

# #      像vi一樣複制    所有複制都會被記錄在緩沖區,輸入#或tmux list-buffers檢視,=也可以選擇粘貼緩沖區

unbind [

bind Escape copy-mode           #綁定Esc進入複制模式

#unbind p                        #解綁p

bind P paste-buffer             #綁定p為粘貼

bind -t vi-copy 'v' begin-selection #綁定v為選擇

bind -t vi-copy 'y' copy-selection  #綁定y為複制

#setting the delay between PREFIX and command

set -sg escape-time 1

# # 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

# #      重新加載配置檔案

bind r source-file /etc/tmux.conf \; display "Reloaded!"

# # Quick pane selection        #Prefix+Ctrl+h/l循環windows

bind -r C-h select-window -t :-

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

# # mouse support - set to on if you want to use the mouse

setw -g mode-mouse off

set -g mouse-select-pane off

set -g mouse-resize-pane off

set -g mouse-select-window off

# # Set the default terminal mode to 256color mode

set -g default-terminal "screen-256color"

set -g display-time 2000

set -g history-limit 10000

# # enable activity alerts

setw -g monitor-activity on

set -g visual-activity on

# # set the status line's colors

set -g status-fg white

set -g status-bg black

# # set the color of the window list

setw -g window-status-fg cyan

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

# # pane colors

set -g pane-border-fg white

set -g pane-border-bg black

set -g pane-active-border-fg blue

#set -g pane-active-border-bg colour240

# # Command / message line

set -g message-fg white

set -g message-bg black

set -g message-attr bright

# # Status line left side

set -g status-left-length 20

set -g status-left "#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P"

set -g status-utf8 on

# # Status line right side

# # 15% | 28 Nov 18:15

set -g status-right "#[fg=cyan]%Y-%m-%d %H:%M:%S "

# # Update the status bar every sixty seconds

set -g status-interval 60

# # Center the window list

set -g status-justify centre

# # Maximize and restore a pane

unbind Up

bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp

unbind Down

bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp

# # Working with the Clipboard on Linux

bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"

bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

繼續閱讀