tmux Copy & Paste on OS X: A Better Future

Chris Toomey

tmux recently updated to version 1.8, and with the update came a new command that greatly simplifies integrating with the OS X clipboard.

tmux Copy

With prior versions of tmux, there wasn’t a straightforward method for getting text copied in tmux to the OS X clipboard. In How to Copy and Paste with tmux on Mac OS X we shared the best solution available at the time, but it required a handful of other configurations working together to make copy and paste work.

What Changed

The key update that comes with tmux version 1.8 is a new command called copy-pipe. This command allows you bind a key to both copy the selected text to the tmux paste buffer and pipe the selected text into an arbitrary shell command.

Setup

In order to make use of this new command, you will still need to install the reattach-to-user-namespace wrapper, which will connect tmux to the OS X clipboard service. Thankfully, homebrew makes this easy:

brew install reattach-to-user-namespace

From there you can add the following lines to your .tmux.conf file and you will be able to copy and paste:

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

Note: these settings will cause tmux to use Vim-like key bindings when in copy-mode. You can see the list of key-bindings this provides by running the following command in your shell:

tmux list-keys -t vi-copy

Usage

  1. <prefix> [ to start “copy-mode” (Not sure what <prefix> is? This post can help)
  2. Move to the text you want to copy using Vim-like key bindings, i.e., k to move up a line, / to search, etc.
  3. v to start selection
  4. Move to end of text with Vim key-bindings. The selection will be highlighted
  5. y to copy the highlighted/selected text

The selected text is now in your clipboard, and your day is that much better for it.