How to make Vim and Tmux friends with system clipboard

Fist of all, a small apology. The tmux solution is a bit specific and works only if you are using macOS and iTerm as your terminal emulator. Get Vim to play nice #

Fist of all, a small apology. The tmux solution is a bit specific and works only if you are using macOS and iTerm as your terminal emulator.

Get Vim to play nice # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#get-vim-to-play-nice) Getting Vim working with the system clipboard is the easy part.

On macOS ( Sierra ) # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#on-macos-(-sierra-)) This is very easy. You will have clipboard support out of the box. You can get the clipboard contents by using

“*p

for paste and

“*y

for copy ( yank )

On Linux # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#on-linux) Well, its a bit harder on Linux, not that much though. First you will have to install a clipboard manager ( I am not sure its called that ) like xclip. Now, once that’s down you can use

“+p

for paste and

“+y

for copy ( yank )

Get Tmux to play nice # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#get-tmux-to-play-nice) This is the hard part and I am really sorry that I haven’t got time to play around with it on Linux.

Set up iTerm # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#set-up-iterm) This solution is specific to iTerm (https://www.iterm2.com/), so download and install it first. Now enable Applications in terminal may access clipboard option from iTerm preferences.

Set up Tmux # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#set-up-tmux) Now we have to set up Tmux.

First step is to install reattach-to-user-namespace using brew:

brew install reattach-to-user-namespace Now add this line to your tmux config file at ~/.tmux.conf

set-option -g default-command “reattach-to-user-namespace -l bash”

Replace bash at the end with your shell. For example if your shell is zsh do:

set-option -g default-command “reattach-to-user-namespace -l zsh”

Now you can set tmux to use vim keys for copy and stuff. Just add the below lines to your tmux config file at ~/.tmux.conf

set-window-option -g mode-keys vi if-shell “test ‘( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -ge 4 )’” ‘bind-key -Tcopy-mode-vi v send -X begin-selection; bind-key -Tcopy-mode-vi y send -X copy-selection-and-cancel’ if-shell ‘( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -lt 4) -o #{$TMUX_VERSION_MAJOR} -le 1’ ‘bind-key -t vi-copy v begin-selection; bind-key -t vi-copy y copy-selection’

And you are good to go! # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#and-you-are-good-to-go!) And a bonus tip # (https://blog.meain.io/2017/make-vim-and-tmux-friends-with-system-clipboard/#and-a-bonus-tip) Want to get mouse scrolling on tmux? Add the following to your tmux config.

bind -n WheelUpPane if-shell -F -t = “#{mouse_any_flag}” “send-keys -M” “if -Ft= ‘#{pane_in_mode}’ ‘send-keys -M’ ‘copy-mode -e’”

This will, as soon as you start scrolling get you into copy mode. You can even select text inside tmux and it will copy as soon you have completed selecting into your clipboard.

Linux daily driver user here. Just bringing some information: xclip is reliant on X11, but it works nicely even with remote sessions (e.g. SSHing into a Linux box with X11 forwarding enabled, this works even when the remote Linux box got no graphical environment, although it depends on having xorg packages). Newer distros are shipping with Wayland, and clipboard management is a nightmare caused by “muh security” BS rhetoric from Wayland developers quite complicated because it depends on the compositor or Window Manager being used: KDE’s KWin, Sway, etc. There’s wl-paste but compatibility varies a lot.

But then Linux comes in all shapes and sizes. My parenthetical example on a graphic-less Linux box? It can easily be a headless server (i.e. neither monitors nor graphic cards are attached; examples are internet routers, especially those running OpenWRT; also some kind of embedded systems; the kind of NAS devices one purchases and plugs it online inside a shoe cardboard box then forgets it; etc) so their terminal interfaces will be text-only (akin to those ttys a Linux user can access via Ctrl+Alt+Fnumber). In such case, copying and pasting is less expected because it generally involves a graphical interface (selecting a text with a mouse or other kinds of pointing devices such as Wacom touchpads, for example). There are niche things such as gpm (Virtual console mouse, which listens for /dev/input/mouseN raw messages and translates it into control of a highlighted full block \u2588 that can then be moved across the framebuffer like a graphical cursor pointer).

Also Linux has two concepts of clipboard (differing from Windows, which only got the famous Ctrl+C Ctrl+V): primary (which changes every time a fragment of text is selected; in graphical DEs, it can be “pasted” using mouse’s middle button or Ctrl+Shift+Insert (at least on KDE Plasma)) and that other kind of clipboard mentioned. And this is also something to consider when pasting/copying to/from vim/tmux/whatever.

Finally, I particularly find it pretty trivial (but I’m long since accustomed to Linux on a daily basis) to type :r!xclip -sel clip -o and then the clipboard pasting happens. For a Wayland Linux setup with wl-paste support, it’s even easier, :r!wl-paste. I may assign it to a vim macro but, well, it’s simply that, no DBus path, no long UUID string representing some kind of interface/device class, so it tends to be easy to memorize.