Copy, with line wrapping!
If you've been trying to copy/paste text from a multi-pane tmux
session with the mouse, you've probably been pretty pissed at the blissful ignorance a terminal application has of the rodent in your hand.
The alternative, which is quote-unqoute native copy/pasting using copy-mode takes a bit to get used to. So this is one solution for copying and pasting lines from a session with correct line wrapping behaviour, albeit keyboard only.
Disclaimer
Since copy-mode has similar concepts of marks, regions, and temp buffers to Emacs .. you'll probably find it straight forward if you're familar with Emacsen. For people using vi-mode in tmux
, the same still applies but obviously the default key bindings will differ alot from what I show below.
I've listed C-a
as the send-prefix key even though C-b
is the default. The word on the street is tmux
developers don't even use C-b
.
The following steps work for me using a OSX/iterm2/tmux/dvtm
combo, if you really need the mouse, you can try out: http://code.google.com/p/iterm2/wiki/TmuxIntegration
Steps:
1) Follow the instructions here: https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard to get pbcopy/pbpaste
working from inside of tmux
.
2) Doctor the following code snippet for your tastes and add it to ~/.tmux.conf
:
# Set bash to whichever shell you are using, zsh for example
set-option -g default-command "reattach-to-user-namespace -l bash"
# Remove the default binding for copy-selection, substitute emacs-copy with vi-copy if you're using vi-mode
unbind -t emacs-copy M-w
# Bind a different key to copy-selection
bind -t emacs-copy c copy-selection
# Bind prefix-M-w to copy-selection, save selection to buffer, and then pipe it's contents to pbcopy
bind M-w send-keys c\;\
save-buffer /tmp/tmux-buffer\;\
run-shell "reattach-to-user-namespace -l bash -c 'cat /tmp/tmux-buffer|pbcopy'"
The above code removes the default copy-selection key in copy-mode (M-w
in my case) and substitutes it with a prefixed version: C-a M-w
. This is because (without patching) tmux cannot bind multiple commands to a key under a specific key-table, only globally.
3) You can check what keys you currently have set for copy-mode by using C-a
-> list-keys -t vi-copy|emacs-copy
to list mode key bindings or use setw -g mode-keys vi|emacs
to switch modes.
To copy the desired piece of text from a tmux pane to your OS' clipboard, you will need to do the following:
- Mark the start of the region using
C-a C-space
- Use next/previous char/line to expand the marked region until it encompasses what you want to copy
C-a Alt-w
to copy the region into a buffer (success will cause the highlighted region to become unselected)C-]
to paste the buffer to the current cursor position (outside of copy-mode)
tmux homepage
http://tmux.sourceforge.net/
man tmux
http://manpages.ubuntu.com/manpages/lucid/man1/tmux.1.html
.tmux.conf
https://github.com/brendanhay/dotfiles/blob/master/.tmux.conf
Default emacs-copy keys:
C-a
PrefixC-a [
Copy modeC-a C-space
MarkC-n
Next lineC-p
Previous lineC-f
Next charC-b
Previous charC-a Alt-w
Copy marked region into bufferC-a ]
Paste saved bufferESC
Exit copy mode
For anyone still fighting with this I recommend the following link:
http://robots.thoughtbot.com/tmux-copy-paste-on-os-x-a-better-future