12

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?

Evan Hahn
  • 12,147
  • 9
  • 41
  • 59
  • 1
    Do you usually start *MacVim* through the GUI (e.g. the dock, *Finder*, Spotlight, some launcher application), or via the command line? If you usually use the command line (e.g. `mvim`), do you regain clipboard access if you quit (not just close all the windows, but e.g. Command-Q) then restart “via the GUI”? Is there anything special about the environment from which you launch *MacVim*? E.g. `mvim` from inside a *tmux* pane that has not used something like my [`reattach-to-user-namespace` program](https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard). – Chris Johnsen May 18 '13 at 07:31
  • @ChrisJohnsen That's probably the issue. I'll take a look soon. Thanks! – Evan Hahn May 18 '13 at 20:40
  • @ChrisJohnsen Thanks! `reattach-to-user-namespace` is excellent. You've saved a lot of nerds like me. – Evan Hahn May 20 '13 at 23:14

1 Answers1

28

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"
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • How would I go about wrapping the whole shell? – Evan Hahn May 21 '13 at 17:33
  • 1
    If you already have a shell started, you can manually start a new, “wrapped” shell like this: `exec reattach-to-user-namespace zsh` (you can do it without the `exec` (e.g. for debugging purposes), but you will end up with an extra layer of shell). To have it done automatically, you will need to reconfigure whatever is starting your shell (e.g. *tmux*) to use the wrapper instead (e.g. via `default-command`); i.e. basically, just add `reattach-to-user-namespace` as a prefix. – Chris Johnsen May 21 '13 at 19:49