After enabling set mouse=a
, text copied inside of Vim will not paste outside of Vim. Does anybody know of a way to fix this?
Here, selecting text with the mouse turns on visual mode and disables the Copy
option in the popup menu:
Press shift while selecting with the mouse. This will make mouse selection behave as if mouse=a
was not enabled.
Note: this trick also applies to "middle button paste": if you want to paste in vim text that was selected outside, press shift while clicking the middle button. Just make sure that insert mode is activated when you do that (you may also want to :set paste
to avoid unexpected effects).
OS X (mac): hold alt/option while selecting (source)
Use ", +, y after making a visual selection. You shouldn’t be using the terminal’s copy command anyway, because that copies what the terminal sees instead of the actual content. Here is what this does:
+
for the next delete, yank or put. The register named +
is a special register, it is the X11 clipboard register. (On other systems, you would use *
instead, I think, see :help clipboard
and :help x11-selection
)You could map it like this:
:vmap <C-C> "+y
And then highlight something with the mouse and press Control-C to copy it.
This feature only works when Vim has been compiled with the +xterm_clipboard
option. Run vim --version
to find out if it has.
Another OSX-Mac option is to uncheck View->Allow Mouse Reporting
(or press ⌘-R to toggle it.) This allows you to toggle between mouse interaction and mouse selecting, which might be useful when selecting and copy/pasting a few bits because you don't have to hold a modifier key to do it.
I usually have line numbers enabled so this will also copy the line numbers if you select multiple lines. If you want to copy multiple lines without the line numbers disable the numbers with :set nonu
and then you can :set nu
to re-enable them after you're done copying.
I accidently explained how to switch off set mouse=a
, when I reread the question and found out that the OP did not want to switch it off in the first place. Anyway for anyone searching how to switch off the mouse (set mouse=
) centrally, I leave a reference to my answer here: https://unix.stackexchange.com/a/506723/194822
Compilation settings that vim was compiled with, are part of the issue. vim --version
shows these.
In OSX, the default vim has -clipboard
But you need +clipboard
On osx you can and apparently generally should, use macvim. You can do brew cask install macvim
That one has +clipboard
.
Them you'll have two vims.
~$ ls -l /usr/bin/vim <--- default vim
-rwxr-xr-x 1 root wheel 1745984 15 Jul 2017 /usr/bin/vim
~$ ls -l /usr/local/bin/vim <-- macvim, installed recently via that mentioned brew line.
lrwxr-xr-x 1 apple admin 42 16 May 23:32 /usr/local/bin/vim -> /Applications/MacVim.app/Contents/bin/mvim
~$
running vim will run macvim 'cos /usr/local/bin
should be before /usr/bin
in the path, though you can check with which vim
.
running vim(to run macvim), is fine but you may want to map vi to macvim 'cos otherwise running vi stays at the default vim! You can rewrite or delete(with rm) and recreate the vi sym link, with ln. And to do that without an 'operation not permitted" error, you have to (temporarily) disable SIL. https://apple.stackexchange.com/questions/208478/how-do-i-disable-system-integrity-protection-sip-aka-rootless-on-macos-os-x .
macvim has +clipboard
as shown by vim --version
Here is a working ~/.vim/vimrc
with just the required lines.
:set mouse=a
:map <leader>c "+y
:map <leader>v "+p
The default leader key is backslash.
I read a suggestion that one should use the leader key.. (certainly control has many keys already in use, so the suggestion was to not use control. I don't know if that applies to command key too, but anyhow).
With that mentioned mapping, \c
will do "+y
which will copy from the register known as +, to the clipboard. And \v
will paste from the register known as +.
So that's a copy/paste that works between windows.
Another OS may require "*
rather than "+
Add set clipboard=unnamed
to your .vimrc
. So it will use the clipboard register '*' instead of the unnamed register for all yank, delete, change and put operations (note it does not only affect the mouse).
The behavior of register '*' depends on your platform and how your vim
has been compiled (or if you use neovim
).
If it does not work, you can try with set clipboard=unnamedplus
, but this option only makes sense on X11 systems (and gvim therefore).
Also worth mentioning, by having set mouse=nvi
, when doing a selection and then pressing :
<ESC>
you will get the mouse selection copied to the primary selection clipboard (equivalent to a "*y
).
Reference: help mouse
Main advantage of this method is the fact that if you have multiple vertical splits, it will only select from the current buffer. Using <Shift>
as mentioned in the main answer, will, in this case, copy from all 3 files at the same time which is not exactly what one would want, expect or need.
+clipboard
andmouse=a
, you will be able to copy/paste from/to vim without using terminal popup menu. You can check ifclipboard
is enable usingvim --version
. – Jérôme Pouillermouse=a
is not a compile option it's something in vimrc B) When vim was compiled with+clipboard
it allows you to copy/paste when:set number
is on, and without copy/pasting the numbers, though perhaps you still have to do some mappings C) One doesn't have to recompile VIM themselves, it may be on in the vim on some package managers supported by whichever OS.. On osx, brew's vim has it though osx's default vim doesn't. My answer mentions this re osx. – barlopxsel
works for you too, you can select text with editor or with mouse and hitctrl+c
to copy text, by the way it is for neovim, github.com/neovim/neovim/issues/7945#issuecomment-361970165 – Naeem Baghi