Can't paste into MacVim

2019-03-25 04:54发布

问题:

I copy text from outside of Vim. ⌘V in other apps pastes text without problem. In MacVim, it doesn't work.

In Insert Mode, nothing appears. In Normal Mode, I get E353: Nothing in register +. This happens when set clipboard=unnamed is on or off.

Oddly enough, this was working before. What's wrong?

回答1:

If you are using tmux and sometimes you initially launch MacVim via the mvim command-line program, then you might be encountering the problem that prompted me to write the reattach-to-user-namespace command.

My guess is that clipboard access worked on prior occasions because you happened to have launched MacVim via a “normal” GUI method (e.g. the dock, Finder, Spotlight, etc.). The clipboard later became inaccessible after you had quit the prior instance of MacVim and relaunched it through (e.g.) mvim from inside a tmux session.

The core problem is that programs launched in certain contexts (i.e. inside a tmux session) end up with an environment that denies them access to certain services (e.g. the OS X pasteboard).

The initial launch is the important one here. New windows started by mvim-in-tmux (even without the above-linked wrapper program) should have access to the clipboard as long as MacVim was previously started “via the GUI” (maybe there are still some MacVim windows open, or maybe you have MacVim configured to keep running even when there are no windows open). Correspondingly, to regain access to the clipboard you will need to close all your existing MacVim windows, quit the application, then restart it in a way that has access to the clipboard (e.g. via the GUI, or “inside” the wrapper).

Once you have the above-linked wrapper program installed (it is also available through MacPorts and Homebrew), you can use a command like reattach-to-user-namespace mvim to ensure that if it ends up starting a new MacVim instance, then that new instance will have access to the clipboard. You might use an alias, shell function, or a script to make sure you always “wrap” mvim.

Several other commands also benefit from “wrapping” (pbpaste, pbcopy, nohup, launchctl (depending on which subcommand you are using)), so you may want to “wrap” your whole shell instead of the individual commands. The bit of the process environment that the “wrapper” modifies is inherited by child processes, so “wrapping” your shell will affect most commands you run from it. If you are using tmux, you could configure your default-command to automatically “wrap” your default tmux shells:

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