Copying text outside of Vim with set mouse=a enabled
Asked Answered
B

16

283

After enabling set mouse=a, text copied inside of Vim will not paste outside of Vim. Is there 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

Backscratcher answered 5/1, 2011 at 19:6 Comment(3)
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.Armorial
@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.Interference
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, github.com/neovim/neovim/issues/7945#issuecomment-361970165Gilburt
G
508

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)

Gibeon answered 5/1, 2011 at 19:31 Comment(10)
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)Tetryl
Ozkan: see Josh Lee's answer to copy text to the clipboard.Dynah
There is also an option to use block select by pressing <kbd>ctrl</kbd> + <kbd>shift</kbd> + left mouse button to start block selection. Mouse middle button pastes copied content into other window.Stamin
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.Sixpenny
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.Giselegisella
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?Vickeyvicki
I don't know how that answer has so many upvotes. If you enabled mouse support to avoid copying line numbers then this suggestion is totally useless.Wite
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).Stroy
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 aboveEyler
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: unix.stackexchange.com/questions/617021/…Golly
B
63

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.

Burseraceous answered 5/1, 2011 at 19:10 Comment(8)
Or possibly "*y depending on OS.Chronicles
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.Backscratcher
Mapping works perfectly fine also :) Now I don't know which of the answers from you guys should I mark as accepted :)Backscratcher
For me this is a better solution than the accepted answer because with this method, line numbers are not copied.Satori
What about +clipboard? And what about +clipboard with xterm_clipboard off?Interference
+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.Interference
On Ubuntu, you can get +xterm_clipboard by running sudo apt install vim-gtk vim-gnomeWorkman
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.Mnemonic
K
24

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

Ko answered 30/8, 2017 at 21:32 Comment(5)
But it disables scrollingMinton
Please say why. At a glance, this seems basically the same as disabling mouse mode completely, which is not the point of the question.Declamation
@Declamation right, see my answer also Josh Lee's answer may helpInterference
Unfortunately, this will also copy the line numbers if they are enabled.Overcautious
Also, if the goal is to disable mouse reporting completely, I believe a more idiomatic way to do that is :set mouse=. The only reason that :set mouse=r works is because it disables mouse reporting for everything except for certain prompts when the intention here is more likely to disable mouse reporting for everything.Lathe
D
19

On OS X, use Fn instead of Shift.

Donndonna answered 10/9, 2017 at 14:16 Comment(1)
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.Daphene
E
7

Another OS X-Mac option is to uncheck menu ViewAllow 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.

Examine answered 21/1, 2016 at 7:2 Comment(0)
G
6

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

Goode answered 14/4, 2013 at 14:12 Comment(0)
Z
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 Shift + Insert to paste it.

Zymogen answered 4/4, 2018 at 22:53 Comment(1)
Then when you have :set number it highlights numbers. So not much good. So you undo the good done by set mouse=aInterference
L
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

Lambent answered 16/3, 2019 at 19:36 Comment(0)
I
1

Compilation settings that Vim was compiled with, are part of the issue. vim --version shows these.

In OS X, the default Vim has -clipboard. But you need +clipboard.

On OS X, you can and apparently generally should, use MacVim. You can do brew cask install macvim. That one has +clipboard.

Them you'll have two Vim installations.

~$ 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, because /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, because otherwise running vi stays at the default Vim! You can rewrite or delete (with rm) and recreate the vi symbolic link, with ln. And to do that without an 'operation not permitted" error, you have to (temporarily) disable SIL. How do I disable System Integrity Protection (SIP) AKA "rootless" on macOS?.

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

Interference answered 17/5, 2019 at 0:1 Comment(1)
Both vims have -xterm_clipboard. Maybe a +xterm_clipboard would be beneficial in some way but it doesn't matter to this solution.Interference
S
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).

Saucy answered 3/6, 2020 at 12:47 Comment(0)
U
0

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

Unreality answered 23/8, 2014 at 10:9 Comment(0)
E
0

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

Eastertide answered 5/12, 2018 at 14:4 Comment(1)
Why so simple after more than seven years? Why did the previous answers miss it?Calc
M
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.

Means answered 25/3, 2021 at 1:17 Comment(0)
B
0

A good workaround which is worth adding:

The GPM daemon can be used which is 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 and Ctrl + V.

sudo apt-get install gpm

Man pages of GPM

Bra answered 12/11, 2021 at 7:38 Comment(0)
A
0

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

After installing vim-gtk3, vi will link to it:

sudo apt install gvim

Output:

[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

And:

which vi

Output:

/usr/bin/vi

And:

file /usr/bin/vi

Output:

/usr/bin/vi: symbolic link to /etc/alternatives/vi

And:

file /etc/alternatives/vi

Output:

/etc/alternatives/vi: symbolic link to /usr/bin/vim.gtk3
Automation answered 26/4, 2022 at 13:6 Comment(0)
B
-3

In ESC mode, when set mouse=a, select the text using the 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.

Blende answered 11/11, 2014 at 9:14 Comment(1)
no this won't work for when you paste outside of vimInterference

© 2022 - 2024 — McMap. All rights reserved.