Fix Vim + Tmux yank/paste on unnamed register

混江龙づ霸主 提交于 2019-11-29 18:51:27

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).

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

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"

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.

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.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!