tmuxとMacのクリップボードを共有する(tmux2.4以降対応)
なぜ書いたか
この記事を読んで、tmuxとMacのクリップボードの共有をしたらエラーになりました。ネットで調べたら情報が少ないので、まとめました。
再現手順
環境:
まずrettach-to-user-namespace
をインストールします。tmuxセッションの中で、MacOS Xのクリップボードにアクセスできるパッケージです。
$ brew install reattach-to-user-namespace
~/.tmux.conf
を以下のように書きます。
# ~/.tmux.conf # Mac OS X pasteboardを使用できるようにする set-option -g default-command "reattach-to-user-namespace -l zsh" # コピーモードでvimキーバインドを使う setw -g mode-keys vi # 'v' で選択を始める bind-key -t vi-copy v begin-selection bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy" # `Enter` でもcopy-pipeを使う unbind -t vi-copy Enter bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy" # ']' でpbpasteを使う bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"
bind-key
の引数の使い方が間違っているとのエラーメッセージが表示されます。
$ tmux source-file ~/.tmux.conf /Users/ryuutarou/.tmux.conf:8: usage: bind-key [-cnr] [-T key-table] key command [arguments] /Users/ryuutarou/.tmux.conf:9: usage: bind-key [-cnr] [-T key-table] key command [arguments] /Users/ryuutarou/.tmux.conf:12: usage: unbind-key [-an] [-T key-table] key /Users/ryuutarou/.tmux.conf:13: usage: bind-key [-cnr] [-T key-table] key command [arguments]
原因
tmux2.4からコマンドがリネームされているみたいです。
これが公式のドキュメントです。
https://raw.githubusercontent.com/tmux/tmux/master/CHANGES
CHANGES FROM 2.3 TO 2.4, 20 April 2017 Incompatible Changes ==================== * Key tables have undergone major changes. Mode key tables are no longer separate from the main key tables. All mode key tables have been removed, together with the -t flag to bind-key and unbind-key. The emacs-edit, vi-edit, emacs-choose and vi-choose tables have been replaced by fixed key bindings in the command prompt and choose modes. The mode-keys and status-keys options remain. The emacs-copy and vi-copy tables have been replaced by the copy-mode and copy-mode-vi tables. Commands are sent using the -X and -N flags to send-keys. So the following:
解決
まずは、tmuxのバージョン確認からです。私が今使用しているtmuxは2.6です。
$ brew list tmux /usr/local/Cellar/tmux/2.6/bin/tmux /usr/local/Cellar/tmux/2.6/etc/bash_completion.d/tmux /usr/local/Cellar/tmux/2.6/share/man/man1/tmux.1 /usr/local/Cellar/tmux/2.6/share/tmux/example_tmux.conf
2.4以降の記法にて.tmux.conf
を書き換えていきましょう。
2.3から2.4へ書き換える際に気をつけることは以下の通りです。
- フラグ
-t
を-T
に変更する vi-<name>
を<name>-mode-vi
に変更する- 実行するコマンドの前に
send -X
もしくはsend-keys -X
を追加する
参考 github.com
書き換えた後の.tmux.conf
がこちらです。
# .tmux.conf # Mac OS X pasteboardを使用できるようにする set-option -g default-command "reattach-to-user-namespace -l zsh" # コピーモードでvimキーバインドを使う setw -g mode-keys vi # 'v' で選択を始める bind-key -T copy-mode-vi v send-keys -X begin-selection bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy" # `Enter` でもcopy-pipeを使う unbind -T copy-mode-vi Enter bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy" # ']' でpbpasteを使う bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"
再度読み込みます。
$ tmux source-file ~/.tmux.conf tmux source-file ~/.tmux.conf
エラーがなくなり使えるようになりましたね!!
これでctrl + b
の後に[
を押して、選択モードにしてから、v
で選択開始、y
で選択終了してコピーします。