網頁

2014年6月7日 星期六

[WorkingEnv][tmux] 打造自己的工作環境-1 (resize pane and copy/paste mouse selection)

如何在tmux下直接使用mouse resize pane以及直接copy mouse選擇的範圍以及在不同pane之間使用mouse選擇後copy paste是很重要的。

以下設定完之後,進入tmux後會直接進mouse mode。而要跳出mouse mode就是 PREFIX + ctrl + M

進入mouse mode有許多好處,
好處1: pane之間的選擇我們一開始必須打 PREFIX + q + number,有 mouse mode之後直接點選就可以。
好處2: copy/paste可以直接用mouse選完之後, 直接按ctrl+c(沒有prefix)以及ctrl+v(沒有prefix)

偺們開始吧 !

如下:
進入Mouse mode : PREFIX + m  
離開Mouse mode: PREFIX + M
vim  ~/.tmux.conf
----------------------------------------------
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on


# Toggle mouse on
bind m \
   set -g mode-mouse on \;\
   set -g mouse-resize-pane on \;\
   set -g mouse-select-pane on \;\
   set -g mouse-select-window on \;\
   display 'Mouse: ON'


# Toggle mouse off
bind M \
   set -g mode-mouse off \;\
   set -g mouse-resize-pane off \;\
   set -g mouse-select-pane off \;\
   set -g mouse-select-window off \;\
   display 'Mouse: OFF'


copy (ctrl+s)
vim  ~/.tmux.conf
----------------------------------------------
bind -n C-s run "tmux save-buffer - | xclip -selection clipboard > /dev/null" \; display-message "Copied


past (ctrl+v)
vim  ~/.tmux.conf
----------------------------------------------
bind -n C-v run-shell "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer" \; display-message "Pasted"


Try it !
把全部混在一起
vim ~/.tmux.conf
-----------------------------------------------
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on

# Toggle mouse on
bind m \
   set -g mode-mouse on \;\
   set -g mouse-resize-pane on \;\
   set -g mouse-select-pane on \;\
   set -g mouse-select-window on \;\
   display 'Mouse: ON'

# Toggle mouse off
bind M \
   set -g mode-mouse off \;\
   set -g mouse-resize-pane off \;\
   set -g mouse-select-pane off \;\
   set -g mouse-select-window off \;\
   display 'Mouse: OFF'


bind -n C-v run-shell "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer" \; display-message "Pasted"
bind -n C-s run "tmux save-buffer - | xclip -selection clipboard > /dev/null" \; display-message "Copied"

下以下指令

shell> tmux start-server \; source ~/.tmux.conf

進入tmux之後就可以使用以上說的指令:
PREFIX+m
RREFIX+M
ctrl+s
ctrl+v

這樣在pane之間複製貼上就不成問題了。
在系統上開的記事本以及瀏覽器上的資料也可以直接ctrl+v到tmux上。

是不是很方便阿 XD


------------------------------------------- 2014.06.10 note (deprecated) ---------------------------------------
針對
bind -n C-v run-shell "tmux set-buffer \"$(xclip -o)\"; tmux paste-buffer" \; display-message "Pasted"
bind -n C-s run "tmux save-buffer - | xclip -selection clipboard > /dev/null" \; display-message "Copied"
後來我發現 xclip 其實不好用, xsel比xclip好用許多。
因此我的working env已經改成xsel的版本摟。

除此之外,我發現resize pane的部份其實可以避免使用到mouse,因此寫了以下的code在.tmux.conf(可以直接去看working env的.tmux.conf)

bind -r k run-shell "tmux resize-pane -U 1"

bind -r j run-shell "tmux resize-pane -D 1"

bind -r h run-shell "tmux resize-pane -L 1"

bind -r l run-shell "tmux resize-pane -R 1"

bind -r的意思就是說prefix只要按一次,其他的repeat按也會發生作用。
也就是說當你按 PREFIX(ctrl+b) + k 目前pane就會往上(1個pixel ?)
這樣就可以用純鍵盤的方式微調你的pane,避免使用到滑鼠還蠻不錯的。

------------------------------------------- 2015.06.08 note(deprecated) ---------------------------------------
2014.06.10描述的方法其實很糟,paste會很慢。

這裡提供一個新的設定 (~/.tmux.conf):
     bind-key -n C-v paste-buffer

1. 這樣就可以在tmux mouse mode開啟的情況下用滑鼠選完後 按 control + v 貼上
2. 
但前提就是只能在 tmux 環境下使用,
換句話說如果要copy到tmux環境外,還是用傳統的方法 
'shift + mouse 選擇'來複製

------------------------------------------- 2015.06.28 note ---------------------------------------
差點忘記以前之所以要用xsel/xclip的目的是想要在tmux和瀏覽器之間的copy/paste,
因此上面的作法會不支援,

這是我目前的設定, 可以支援從linux的tmux環境的copy/paste
bind -n C-s run-shell "tmux save-buffer - | xsel -i -b" # 'copy(from tmux to X11 clipboard)'
bind -n F12 run-shell "tmux set-buffer \"$(xsel -o -b)\"; tmux paste-buffer \;" #'paste(from X11 clipboard)'
bind-key -n C-v paste-buffer # 'paste' # 'paste(from tmux to tmux)'

用法:
平時在tmux環境內:
copy - 開啟mouse mode來選擇就copy完成了
paste - Ctrl+v

若想要把自己寫的code copy到網頁上:
copy - 開啟mouse mode來選擇後按下Ctrl+s
paste - Ctrl+v

若想要把網頁上的code copy到tmux環境上:
copy - Ctrl+c
paste - F12

解說:
為什麼要這樣? 其實平時不會有超出tmux環境的需求,因此額外再啟動一個xsel binary會比較慢。
因此額外開這兩個command (Ctrl+s, F12)



沒有留言:

張貼留言