I am relatively new to spending a lot of time in the terminal and using something like tmux to manage my terminal life.
There are lots of work-arounds out there to handle copy-pasting with tmux, whether to get stuff in and out of tmux and the system clipboard, or between different terminal programs inside a tmux session or whatever... most of the recommended solutions for this involve installing plug-ins, or a tool like
xsel, usually with a lot of specialized config file modifications to make it all work. Currently I'm not really a fan of plugins or really customized complex config files. I would like to work with something that's as simple as possible, so I can easily replicated it elsewhere to feel relatively at home on a default remote environment. And I'd rather just be able to use it than spend all my time fixing it whenever some random vim plugin I'm using breaks.
I found out that it is actually very easy to make copy-pasting in tmux painless just by using xterm as my terminal emulator, plus adding a few very basic modifications to config files. I figured I would share what I learned since it stands in contrast to all the more common complex solutions out there.
I also use QubesOS So I've included a few Qubes-specific notes.
For XTerm, add these 2 relevant sections to
~/.Xdefaults, depending on your system.
~/.Xdefaults appears to be the newer preferred place, but in Qubes debian-based templates (
~/.Xresources appears to be the correct location.
xterm*disallowedWindowOps: 20,21,SetXprop ! adds more familiar copy-paste shortcuts xterm*selectToClipboard: true xterm*VT100*translations: #override \n\ Shift Ctrl <Key> V: insert-selection(CLIPBOARD) \n\ Shift Ctrl <Key> C: select-end(CLIPBOARD)
xrdb -m ~/.Xresources should 'refresh' these settings, but you will still have to open a new XTerm for the changes to take effect.
xterm*disallowedWindowOps: 20,21,SetXprop is necessary for xterm and tmux's clipboards to interoperate, as per tmux's manpage and this link. This is really the only thing you need for all this to work really nicely. The rest is just making your life a tiny bit easier by adding more familiar key-bindings. Technically, you can just stop here, and get used to xterm and tmux's default keybindings and you'll be good almost anywhere.
But if you don't want to learn new keybindings for copy/paste, read on... By default, text is copied in xterm simply by selecting it. The selection will be copied. Usually, xterm only uses the Primary buffer, which is different from the system-wide clipboard. Typically this buffer is filled with whatever text was last selected with the mouse, and it can be pasted using the middle mouse button. In xterm,
Shift+Insert is the default keybinding to paste text from this primary buffer into the xterm window.
Also note that by default in xterm,
Shift+Print Screen will copy whatever text is selected to the clipboard.
xterm*selectToClipboard: true will cause xterm to treat the Primary buffer and Clipboard the same, in both directions. Effectively, text is copied from the clipboard, and pasted from the clipboard no matter what you are doing.
*You can also turn this option on in an xterm window by holding down Control, clicking the middle mouse button, and selecting
Select to Clipboard. *
Now, simply selecting any text in the xterm window will still copy that text into the primary buffer and to the Clipboard, which can be pasted elsewhere into xterm using
However, the bottom lines add the the more familiar keybinding
Control+Shift+V to paste whatever is on the clipboard.
Control+Shift+C has also been added to copy a selection to the clipboard. But this is redundant as far as I can tell. Any selection is copied immediately to the clipboard without the need for a additional keypress.
Qubes users may want to note I have changed the default setting for copying-pasting from the Qubes Global Clipboard. The default is
Control+Shift+C/V. I've changed it to
Control+Windows+C/V. This is done in the Global Qubes Settings in dom0. I do this so they don't interfere with the default gnome-terminal keybindings for copy-paste (which is
Control+Shift+C/V. Even though I don't use gnome-terminal as my preferred terminal emulator, as you can see above, I've decided to stick with that keybinding for copy/pasting into xterm. I still encounter gnome-terminal often enough (it is the default terminal in a disposable Qube, for example).
The config for tmux is also easy, in fact it's basically optional. I have a lot of stuff in my tmux config, but these are the relevant copy-paste bits to add:
# mouse on set -g mouse on # copy paste set-window-option -g mode-keys vi
In tmux, you enter copy-mode to copy text. This can be done by:
- Clicking the mouse anywhere and selecting text. Any text selected is copied to the tmux buffer, and also the system Clipboard, so it can then be pasted anywhere (I like using the mouse to select text, and
set -g mouse onwill allow that)
- Enter tmux copy-mode using the default keybinding
C-B [and then navigate using
hjkl(as in vim) or arrow keys.
C-Spaceto start selection and
Enterto copy the selection to the buffer (
set-window-option -g mode-keys visets the copy mode keybindings into 'vi mode' which is easier for me to understand than the default emacs mode. Without this, the copy-mode is controlled using keybindings that are emacs-like and I have no idea what they are)
Pasting into tmux can be done using
If you like, you can even use these copy paste mechanisms without issue in
vim when in
INSERT mode. The whole issue you might have is not being able to select more text than is on screen, since both tmux and xterm don't easily allow you to scroll back while selecting text in the vim window. So you'll be fine if you are just copying a few lines at a time, but if you want to copy large amounts of text, you are better off using vim's built in copy-paste mechanisms to move text around.