Fix Vim + Tmux yank/paste on unnamed register

VimTmux

Vim Problem Overview


While using vim inside a tmux session I cannot yank or paste to the unnamed register. Going to a named register works fine, but unnamed never works.

Error is:

E353: Nothing in register *

Without tmux, vim works fine using my current settings. How do I fix it so I can use y then p without errors and without specifying the register?

Vim Solutions


Solution 1 - Vim

From the error message (Nothing in register *), it appears that when you do a plain? p, your instance of Vim is using the * register instead of the unnamed register*. This is probably because your clipboard option includes the value unnamed. When configured this way, Vim will use the * register instead of the unnamed register for yank, delete, change, and put operations by default (i.e. unless you specify another register with a " prefix; e.g. "ap to put from the a register).

*The unnamed register is actually named " (double quote). It is only “unnamed” in the sense that you do not have to name it to use it (it is the default). I.e. you do not have to say ""p to put from the unnamed register, just p.

The default value of clipboard does not contain unnamed, so it is probably coming from some bit of your configuration (or a plugin). The command :verbose set clipboard? will show you the script that set the current value. If this is being done in your configuration file, then you might want to not do it when you are running under tmux. E.g:

if $TMUX == ''
    set clipboard+=unnamed
endif

Alternatively, there may be some way to let instances of Vim-inside-tmux access the GUI selection/clipboard (thus work with the * register and/or unnamed in clipboard). If you are running Mac OS X, you may want to look at my workaround wrapper that re-enables clipboard access for processes running inside a tmux session. If you are using some other OS or GUI, then you will need to find out how Vim would normally talk to the GUI and why it is not working (e.g. wrong DISPLAY value under X11, possibly due to attaching to an old session that is running a shell that has an out-of-date value).

Solution 2 - Vim

Here is what works for me in vim/tmux/osx:

  1. Install Homebrew
  2. Install reattach-to-user-namespace: brew install reattach-to-user-namespace
  3. in .vimrc: set clipboard=unnamed
  4. Tell tmux to use the system clipboard: In .tmux.conf: set-option -g default-command "reattach-to-user-namespace -l bash"

Source: https://coderwall.com/p/j9wnfw

Solution 3 - Vim

The fakeclip plugin makes the clipboard register behave as expected in many terminals and has support for tmux/screen. Are you using it? It may resolve your issue.

As well, you may be interested in this tip... It's not applicable to your question, but related. Depending on what type of system/terminal you're running tmux in, you may need some tweaks in your .tmux.conf. For example here's an excerpt of my .tmux.conf on OS X (with some instructions in comments):

# To use pbcopy and pbpaste on OS X, get this wrapper and install
#    git clone https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard.git
#    cd tmux-MacOSX-pasteboard/
#    make reattach-to-user-namespace
#    mv reattach-to-user-namespace /usr/local/bin
# After installing, the default command can be reset to use the 'reattach-to-user-namespace' 
# wrapper that was compiled/installed as descripted above.
set -g default-command "reattach-to-user-namespace -l /bin/bash"
# #Next, create Ctrl-c and Ctrl-v mappings
bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste); tmux paste-buffer"

Solution 4 - Vim

Late answer, but it also might be that your .tmux.conf contains the following line:

set -g set-clipboard off

combined with a .vimrc containing

set clipboard=unnamed

this will lead to vim trying to use a clipboard that is not there.

Solution 5 - Vim

To add a couple notes from my experience on OSX to the accepted answer:

  • Make sure you do set clipboard=unnamed and not set clipboard=unnamedplus
  • I had to kill my tmux server ($ killall tmux or $ tmux kill-session -a). Reloading the tmux configuration files showed that default-command was set but did not give vim access to the system clipboard.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonView Question on Stackoverflow
Solution 1 - VimChris JohnsenView Answer on Stackoverflow
Solution 2 - VimakofinkView Answer on Stackoverflow
Solution 3 - VimdarcyparkerView Answer on Stackoverflow
Solution 4 - VimBeat RichartzView Answer on Stackoverflow
Solution 5 - VimnbwoodwardView Answer on Stackoverflow