279

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:

enter image description here

Mark Boulder
  • 13,577
  • 12
  • 48
  • 78
lyuba
  • 6,250
  • 7
  • 27
  • 37
  • 2
    If your vim is compiled with `+clipboard` and `mouse=a`, you will be able to copy/paste from/to vim without using terminal popup menu. You can check if `clipboard` is enable using `vim --version`. – Jérôme Pouiller Dec 14 '16 at 09:30
  • @Jezz Worth clarifying that A)`mouse=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. – barlop May 17 '19 at 05:24
  • Maybe installing `xsel` works for you too, you can select text with editor or with mouse and hit `ctrl+c` to copy text, by the way it is for neovim, https://github.com/neovim/neovim/issues/7945#issuecomment-361970165 – Naeem Baghi Oct 31 '19 at 14:52

16 Answers16

504

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)

Community
  • 1
  • 1
François
  • 7,988
  • 2
  • 21
  • 17
  • 5
    OK, this is indeed the solution. But what if you want to select a lot of code that is more than on your screen? (then you have to scroll and select at the same time) – Ozkan Apr 02 '13 at 12:25
  • Ozkan: see Josh Lee's answer to copy text to the clipboard. – François Apr 03 '13 at 12:24
  • 3
    There is also an option to use block select by pressing ctrl + shift + left mouse button to start block selection. Mouse middle button pastes copied content into other window. – micrub Mar 18 '14 at 16:29
  • 8
    If you have line numbers (as the screenshot shows) then when selecting using ALT on a Mac OS X then the line numbers are also copied. It is better in this case to use ALT + CMD when selecting for Block Selection mode so you can avoid selecting the numbers. – Aaron Jun 06 '14 at 23:29
  • 22
    For Mac Terminal, after **OS X 10.11 El Capitan**, `Fn` key is the right way to go. I.e, (with `mouse=a` enable) selecting text while press `Fn` key, then use CMD + C to copy and paste to anywhere you want. – YaOzI Jul 03 '17 at 04:36
  • On Windows a side-effect of `set mouse=a` with Shift seems to be that you have to press Ctrl+c / Ctrl+v multiple times for it to have any effect. Nowhere near as smooth as in gVim. Anybody know of a way to fix this? – Mark Boulder Sep 02 '17 at 17:40
  • 4
    I don't know how that answer has so many upvotes. If you enabled mouse support [to avoid copying line numbers](https://stackoverflow.com/questions/5728259/how-to-clear-the-line-number-in-vim-when-copying) then this suggestion is totally useless. – Stelios Adamantidis Apr 12 '19 at 12:11
  • On Mac, mouse selection, when `mouse=a` is enabled can be done by keeping `Alt` pressed while selecting with the trackpad or mouse. (The `shift` did not work for me). – mljrg Apr 09 '20 at 09:53
  • This bypasses vim mouse select and uses the console(I think...). This means that it doesn't work well in tmux with a screen split for example or the line numbers that Stelios mentioned above – Colin D Oct 25 '20 at 16:57
  • Unfortunately starting from Ubuntu 21.04 this doesn't work anymore with XTerm :-( For others that experience the same, toggling Num Lock triggers/resolves this: https://unix.stackexchange.com/questions/617021/how-do-i-restore-shiftmousebutton-select-when-mouse-protocol-is-enabled-in-xt – Giel Jul 12 '21 at 10:02
61

Use ", +, y after making a visual selection either with the keyboard or the mouse. 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:

  • ",+ tells Vim to use the register named + 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)
  • y is the yank command, which tells Vim to put the selection in the register named previously.

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 4
    Or possibly `"*y` depending on OS. – frabjous Jan 05 '11 at 20:55
  • That worked! However, the combination is pretty complex, 4 buttons actually.. Probably I need to map the other one while using "shift" way from the other answer. – lyuba Jan 05 '11 at 21:35
  • Mapping works perfectly fine also :) Now I don't know which of the answers from you guys should I mark as accepted :) – lyuba Jan 06 '11 at 16:21
  • 2
    For me this is a better solution than the accepted answer because with this method, line numbers are not copied. – Saulo Silva Nov 25 '14 at 14:55
  • What about `+clipboard`? And what about `+clipboard` with `xterm_clipboard` off? – barlop May 17 '19 at 05:29
  • +Saulo the (currently) accepted answer from francois is terrible. This answer is better but requires that a compilation option was set and has no suggestion if it hasn't been. – barlop May 17 '19 at 05:31
  • On Ubuntu, you can get `+xterm_clipboard` by running `sudo apt install vim-gtk vim-gnome` – Flimm Mar 05 '20 at 10:07
  • It works! In m case despite not beingh such clipboard options in `vim --help`. I'm using neovim: `vim --version | grep -i clip` shows nothing. – Gerard Bosch Nov 25 '21 at 17:39
24

Instead of set mouse=a use set mouse=r in .vimrc

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Nefeline
  • 373
  • 3
  • 4
19

On OSX use fn instead of shift.

ssasa
  • 1,616
  • 1
  • 18
  • 30
  • worked for me with `option` key which is `alt` key equivalent on MacOS host. It was remote terminal on debian9 though. didn't worked by adding either of `set mouse=a` / `set mouse=r` in `/etc/vim/vimrc` on remote. – mrtipale Nov 27 '18 at 05:47
7

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.

Note for Multiline with line numbers:

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.

Community
  • 1
  • 1
reor
  • 820
  • 9
  • 22
6

In Ubuntu, it is possible to use the X-Term copy & paste bindings inside VIM (Ctrl-Shift-C & Ctrl-Shift-V) on text that has been hilighted using the Shift key.

Marc Gibbons
  • 1,190
  • 10
  • 5
1

You can use :set mouse& in the vim command line to enable copy/paste of text selected using the mouse. You can then simply use the middle mouse button or shiftinsert to paste it.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
Madhu
  • 29
  • 3
  • Then when you have `:set number` it highlights numbers. So not much good. So you undo the good done by `set mouse=a` – barlop May 16 '19 at 22:13
1

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

Holger Böhnke
  • 971
  • 11
  • 17
1

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 "+

barlop
  • 12,887
  • 8
  • 80
  • 109
  • Both vims have `-xterm_clipboard`. Maybe a `+xterm_clipboard` would be beneficial in some way but it doesn't matter to this solution. – barlop May 17 '19 at 05:23
1

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

Jérôme Pouiller
  • 9,249
  • 5
  • 39
  • 47
0

If you are using, Putty session, then it automatically copies selection. If we have used "set mouse=a" option in vim, selecting using Shift+Mouse drag selects the text automatically. Need to check in X-term.

0

em... Keep pressing Shift and then click the right mouse button

levinit
  • 394
  • 3
  • 7
0

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.

Dorian B.
  • 1,101
  • 13
  • 22
0

A good workaround which is worth adding:

GPM daemon can be used which is a a cut and paste utility and mouse server for virtual consoles. It will provide functionalities across all the virtual consoles!

Copy-Paste actions can be done by <CTRL-C>/<CTRL-V>.

sudo apt-get install gpm

MAN pages of GPM

Vishnu
  • 495
  • 8
  • 14
0

set set mouse=a in vi, using MobaXterm, after installing vim-gtk3 on server, dragging with mouse and Ctrl + Insert works, but seems it only work with MobaXterm

after installing vim-gtk3, vi will link to it

lala@kubu:~$ sudo apt install gvim
[sudo] password for lala:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package gvim is a virtual package provided by:
  vim-gtk3 2:8.2.2434-3ubuntu3.2
  vim-athena 2:8.2.2434-3ubuntu3.2
You should explicitly select one to install.

E: Package 'gvim' has no installation candidate
lala@kubu:~$ which vi
/usr/bin/vi
lala@kubu:~$ file /usr/bin/vi
/usr/bin/vi: symbolic link to /etc/alternatives/vi
lala@kubu:~$ file /etc/alternatives/vi
/etc/alternatives/vi: symbolic link to /usr/bin/vim.gtk3
lala@kubu:~$
tteng little
  • 161
  • 1
  • 1
  • 6
-3

In ESC mode, when set mouse=a, select the text using mouse. This would enable the visual mode in vim. Then you can press 'y' to yank the selected text and 'p' to paste it wherever you want. This happens only within vim.