Macos – Tmux copy-pipe with mouse selection

clipboardcopy/pastemacosmousetmux

I use copy-pipe on OS X to share the system clipboard with Tmux 1.8:

unbind -t vi-copy Enter
bind -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

This works when entering select mode and copying text (select text, then hit Enter), but not when selecting text with the mouse. Is it possible to set a command that should run after mouse selection, just like copy-pipe runs after hitting Enter? I.e. I need to run reattach-to-user-namespace pbcopy when the selection is left.

Best Answer

There is no built-in way to customize the “mouse up” behavior that ends a selection that was started by the mouse. For the details, see the reset_mode label of window_copy_mouse() in window-copy.c (links are to the source of tmux 1.8); specifically, there are no references to window, session, or server options in this bit of code.

There is a workaround, but it is probably a bit of an edge case:

  • Activate your copy-pipe binding (e.g. press Enter) before releasing the mouse button.
    This will run your shell command, copy the selection into a buffer, and exit copy mode (i.e. the usual behavior for copy-pipe).

There is a small side effect though:

  • The mouse event that your terminal will send when you finally release the button will cause tmux to re-enter copy mode. You will need to manually exit (e.g. Escape or C-c) this “extra” copy mode.
    This could probably be considered a bug (input_mouse() does not check that the mouse event is a “button up” event before starting a new copy mode); but, it is mostly harmless (other than the inconvenience of having to exit the second copy mode instance).
Related Question