LoginSignup
26

More than 3 years have passed since last update.

tmuxのマウス操作でドラッグしたらbufferにコピーするし、clipboardにも保存する

Posted at

最近 tmux を導入したんだが、ゆとり新卒プログラマなのでマウス操作もしたいこと、ありますよね:raised_hand:

ちなみに

$ tmux -V
tmux 2.5

マウス操作を可能にする

tmux で検索するとめちゃめちゃ上位に出てくる記事
tmuxチートシート

とりあえずこれだけ書いておく

tmux.conf
# 上記の記事のコード
set-option -g mouse on
bind-key -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind-key -n WheelDownPane select-pane -t= \; send-keys -M

# コピーモードをvi風にする
setw -g mode-keys vi

これでマウス操作ができるようになります。

tmuxのマウス操作について

ドラッグするとコピーができます。
Ctrl-b + ] でペーストできます。
でも、でも、私はCommand-vでペーストしたい弱小macユーザー故、対策を模索しました。

検索したら一番上に出てくる

tmuxとMacのクリップボードを共有する(copy-mode, vim)
全くうまくいかない、そもそもこのユーザーはドラッグでコピーすることを想定していない

謎のブログ

Everything you need to know about Tmux copy paste

Copy from tmux buffer to system buffer (clipboard)
In newer iTerm2, you need to enable one option in settings to copy a text into both tmux buffer and clipboard at the same time. Go to iTerm2 > Preferences > “General” tab, and in the “Selection” section, check the box which says “Applications in terminal may access clipboard” if it isn’t checked. See here for a screenshot.

ふむ、よくわからん。

公式のコマンド紹介

http://man.openbsd.org/OpenBSD-current/man1/tmux.1
うーん、よくわからん。
でもtmux list-keysでkeyを出力できそう

# いっぱい出力されるので、grep
$ tmux list-keys | grep copy-mode-vi

# それっぽいのを見つけた
bind-key    -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-selection-and-cancel

多分MouseDragEnd1Paneはドラッグがendしたらという意味だと予想する
以下の記事を参考にしつつ
tmux2.4からvi-copyがcopy-modeに変わっていた
.tmux.confに追記

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"

できた!:relaxed:
brew install reattach-to-user-namespace忘れないようにな

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26