Using the system clipboard in tmux, Emacs and Vim
One "unconventional" default behaviour of tmux, Emacs and Vim is that they maintain their own internal clipboards, which can make it awkward to copy/paste between programs. I prefer to use the system clipboard by default, which can be achieved with minimal changes.
Tmux requires the most setup. The below example can be used in
tmux.conf to support both macOS and Linux:
bind-key -T copy-mode-vi 'y' send -X copy-pipe \
"if command -v reattach-to-user-namespace >/dev/null; \
then reattach-to-user-namespace pbcopy; \
elif command -v xclip >/dev/null; \
then xclip -selection clipboard; fi"
copy-pipecommand was added in tmux 1.8. It sends the selected text to stdin for a given command, which can be an appropriate system clipboard tool.
- On macOS,
pbpastecan be used to interact with the system clipboard. To use these with tmux, you have to use reattach-to-user-namespace.
- GNU/Linux systems can install various X clipboard tools.
xclipis a common choice.
The ELPA package xclip provides cross-platform integration with the GUI
clipboard for terminal Emacs. Despite using the xclip name, it supports multiple
clipboard programs. You just have to initialise it in
had this running for years without issue:
Vim must have been compiled with
+X11, which can be checked using
vim --version. On Arch this means installing the
gvim package rather than
- You can either use
set clipboard=unnamed, which will use the
set clipboard=unnamedplus, which will use the
clipboardX selection. My experience on Arch is that most programs use the
clipboardselection, so I use