日記帳

プログラミングのことをつぶやく日記です。

tmuxとMacのクリップボードを共有する(tmux2.4以降対応)

tmuxとMacクリップボードを共有する(tmux2.4以降対応)

f:id:leokun0210:20171216114259p:plain

なぜ書いたか

この記事を読んで、tmuxとMacクリップボードの共有をしたらエラーになりました。ネットで調べたら情報が少ないので、まとめました。

qiita.com

再現手順

環境:

macOS Sierra 10.12.4

zsh


まずrettach-to-user-namespaceをインストールします。tmuxセッションの中で、MacOS Xクリップボードにアクセスできるパッケージです。

github.com

$ 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からコマンドがリネームされているみたいです。

qiita.com

これが公式のドキュメントです。

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へ書き換える際に気をつけることは以下の通りです。

  1. フラグ-t-Tに変更する
  2. vi-<name><name>-mode-viに変更する
  3. 実行するコマンドの前に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で選択終了してコピーします。