84

I was trying to copy paste something from vim to another application and also, from that application to vim using right click with mouse and then copy and paste (or with Ctrl+v and Ctrl+c and also tried the Command version for mac OSX, obviously.). However, when I try doing it, it only copies the first word when I do it from vim or when I copy from the application to vim, it copies everything, but inserts strange tabs and spaces. I think this happened when I decided to set my mouse on in the terminal. As in:

:set mouse=a

I have that line on my .vimrc file on iTerm (mac os x). Though, is it possible to make my copy paste with other applications that are not in vim not to break with the mouse=a on? Or is it at least possible to set my mouse off while I do the copy paste? I did :help mouse but the comments were not useful for me. I would paste them here but... my copy paste tool is broken!

I did try :set mouse! and :set mouse=a! but these did nothing useful... :(


Additional info of my environment:

I am also using tmux most of the time, though, I tested this error/bug without a tmux session, thats why I posted this mainly as a vim question.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 5
    I can tell you that vim is capable of yanking to the Primary Selection just fine using the y verb. I don't know why you wouldn't just use that. – HalosGhost Jun 27 '14 at 19:44
  • 1
    I wouldn't do that because I don't know how to do that I guess, apologize for my incompetence. I didn't even know that existed until you mentioned it. Please share your knowledge! :) – Charlie Parker Jun 27 '14 at 20:07
  • no it is not its just iTerm (as I mentioned on the question, but good question though). – Charlie Parker Jun 28 '14 at 01:17
  • 4
  • Do copying and pasting work with other terminal applications? Also, does it work with vim running in the native OS X Terminal? – depquid Jul 01 '14 at 01:22
  • well, the thing is that :set mouse=a does not work properly with the native terminal. In fact it doesn't do anything as far as I can tell (so yes, copy pasting does seem to work in the native terminal) – Charlie Parker Jul 01 '14 at 01:38
  • if I could just :set mouse=OFF or something like that, that would be kind of a hack (not a "real" solution), but at least I would be able to copy paste by turning the mouse=off whenever I need to copy paste... – Charlie Parker Jul 01 '14 at 01:39
  • None of the answers here worked for me, but there are some useful ones here (which work well in Linux at least). – Sparhawk Oct 20 '14 at 23:33
  • On macOS: hold "fn" key while selecting! – ɹɐʎɯɐʞ May 11 '21 at 18:52
  • @HalosGhost an example is using vim over SSH. Yanking is great for when you want to use the word in Vim itself, but what if I want to copy something to my clipboard (not the clipboard on the host where Vim is running), maybe simply to paste it into a web browser? – Attila O. Nov 08 '23 at 08:43
  • Charlie, I wonder if you could settle something for me? There's been some debate re whether or not your objective was to be able to simply copy & paste - or was your objective to be able to copy and paste *while using one of the mouse commands*? Do you recall that far back? Thanks - I'll "owe you one" :) – Seamus Feb 06 '24 at 23:11

12 Answers12

113

mouse=a prevents the ability of copying and pasting out of vim with readable characters.

Change mouse=a to mouse=r and that should fix your issue with that.

one thing I am wondering is, are you changing the config file for your vim with the mouse set to mouse=a?

orignal answer ^

If mouse=r doesn't give you all the copy past options change it to mouse=v

Both mouse=r and mouse=v have the same functions you are needing, but depending on the vimrc you are using one will work better then the other.

bgrif
  • 1,342
  • also have you tryed highlighting the the text and use you mouse middle button to past into another place. that tends to work better when tryng to Ctrl+v and Ctrl+c – bgrif Jul 03 '14 at 15:31
  • I don't know what your question means: one thing I am wondering is, are you changing the config file for your vim with the mouse set to mouse=a? You mean if I have :mouse=a on in my .vimrc file? I do have :mouse=a in my .vimrc file file. – Charlie Parker Jul 03 '14 at 15:57
  • that is what I mean. That is my mistake on asking it like that. you need to change it to mouse=x..... – bgrif Jul 03 '14 at 16:10
  • I did the command :set mouse=x in my vim command and it said E539: Illegal character <x>: mouse=x, you sure there is such a command? I tried to see if it was an option when I did help mouse in vim and nothing came up. – Charlie Parker Jul 03 '14 at 16:19
  • it is a commmand. But I looked further into it and some people had the same issue. it is due to a patch that disabled mouse=x. they made it mouse=r instead. I don't know why but thats what they did. i will modify my post to be mouse=r – bgrif Jul 03 '14 at 16:38
  • another thing do you have --with-x=yes in your .vimrc file. If this isn't in there a lot of the copy and paste methods don't work properly. – bgrif Jul 03 '14 at 16:44
  • 1
    just --with-x=yes? I do not need to put :set --with-x=yes mouse=a or something? Could you clarify what you mean? – Charlie Parker Jul 07 '14 at 14:57
  • Also, your first suggestion (:set moutse=r) solved half the problem, now I can paste from vim to the outside! Though, when I paste back it inserts a lot of extra spaces randomly and it doesn't respect the indentation from the original text... :( – Charlie Parker Jul 07 '14 at 14:59
  • 1
    Let me research if you need to combine --with-x=yes to set mouse=a. When i was reading up on it all i saw was it was by itself in the config file. But it could be different so I want to make sure that I tell you the right thing that will work for you. – bgrif Jul 07 '14 at 15:06
  • I have done a lot of research on where --with-x=yes needs to be placed. The --with-x=yes is its own stand alone inside the vimrc. But what happened is that mouse=a was supposed to replace that option and make everything work again. But it just has created a big problem for them. Dolby has made posts about it and they came up with a work around to get the old settings that worked right in the first place. you can install the older vim packages like gvim to work with the new ones and it will bring back all the old settings that worked. – bgrif Jul 11 '14 at 13:39
  • One more thing that I have found out is people are changing the mouse=a to mouse=v and have all the functions back. Now i know that mouse=r works and lets you copy and paste out of the vim file just fine. But change it to mouse=v and see if that fixes the rest of the issues you were having when coping into it. A lot of people seem to have the same issue and have sent in bug reports and were told the same thing I said and change it to mouse=r or mouse=v – bgrif Jul 11 '14 at 13:43
  • I have also edited my answer so it will be easier to see for everyone else. Hopefully the information I have given you does the trick – bgrif Jul 11 '14 at 14:09
  • This answer doesn't seem right: mouse=r or mouse=v disable vim controlling the selection, resulting in line numbers from set number to appear in copied text. – Vincent Scheib Jan 12 '15 at 22:28
  • 1
    Thanks a lot, this suggestion work like a charm with mouse=v – Pamungkas Jayuda Mar 11 '19 at 12:17
33

For OS X users: To copy paste with mouse=a use alt instead of Shift to selec the text. Then cmd-c and cmd-v work as expected.

Found this answer here https://stackoverflow.com/a/4608387/671639 after a lot of googling.

BetaRide
  • 431
14

From https://stackoverflow.com/questions/4608161/copy-text-out-of-vim-with-set-mouse-a-enabled/4608387

Press 'shift' key while selecting with the mouse. This will make mouse selection behave as if mouse=a was not enabled.

This works and has been verified by reviewers of stack-overflow who have accepted this as answer :)

BHS
  • 281
11

Had the same problem: could not highlight using the mouse so could not copy and paste from VIM in a terminal session to Windows document and browser: Did the following and it now works:

: set clipboard=unnamedplus
: set mouse=r
Bob
  • 111
6

Bharath's solution, pressing Shift while copying, will do it, but it will copy more than what you'd like to copy in-case of numbering or hash comments.

One way to do so even with mouse=a is use visual mode by pressing V for single cursor or Shift+V for full line highlight, and you can move up or down with arrows and use Y to copy or D to cut and that should work.

amrx
  • 159
  • 1
    I rephrased your self-pronounced comment to make it more like an answer that way the mods will probably leave it as is (and you might gain some reputation on it so you can comment). If the original had been a more minor correction, commenting would have been more appropriate. – Anthon Apr 02 '15 at 14:15
4

Refer: http://vimdoc.sourceforge.net/htmldoc/options.html

Look at Note section at the bottom of the reference:
When enabling the mouse, the mouse buttons can still be used by keeping the shift key pressed. This includes copy paste using mouse buttons.

*'mouse'* *E538*
'mouse'         string  (default "", "a" for GUI, MS-DOS and Win32)
            global
            {not in Vi}
    Enable the use of the mouse.  Only works for certain terminals
    (xterm, MS-DOS, Win32 |win32-mouse|, QNX pterm, *BSD console with
    sysmouse and Linux console with gpm).  For using the mouse in the
    GUI, see |gui-mouse|.
    The mouse can be enabled for different modes:
        n   Normal mode
        v   Visual mode
        i   Insert mode
        c   Command-line mode
        h   all previous modes when editing a help file
        a   all previous modes
        r   for |hit-enter| and |more-prompt| prompt
    Normally you would enable the mouse in all four modes with:
        :set mouse=a
    When the mouse is not enabled, the GUI will still use the mouse for
    modeless selection.  This doesn't move the text cursor.

    See |mouse-using|.  Also see |'clipboard'|.

    Note: When enabling the mouse in a terminal, copy/paste will use the
    "* register if there is access to an X-server.  The xterm handling of
    the mouse buttons can still be used by keeping the shift key pressed.
    Also see the 'clipboard' option.
3

Use these two lines below in your ~/.vimrc to use all nice features of a gui-mouse and paste into system clipboard using y (yank key) if you don't really need any additional clipboard buffers:

set mouse=a
set clipboard=unnamed
0xSheepdog
  • 2,750
CeDeROM
  • 317
3

On my Mac, what helped were those two things:

  • :set mouse-=a
  • fn+click (instead of shift or alt)
1

(Updated as per discussion in comments)

Ok so just follow these frustrating steps to make vim behave sanely (allow mouse copy and keep syntax highlighting):

Option 1: 'system-wide'

  1. in /etc/vimrc, uncomment source $VIMRUNTIME/defaults.vim
  2. in /etc/vimrc.local add set mouse=v
  3. for every user that is effected create a a .vimrc eg.:
    • touch ~/.vimrc
    • sudo touch /root/.vimrc

Option 2: per user

For every affected user add this to their ~/.vimrc

unlet! skip_defaults_vim 
source $VIMRUNTIME/defaults.vim
set mouse=v

More info / rant:

I had to play around a lot to figure this out, mainly on debian/mxlinux. I'm baffled why there is no easier way to configure system-wide defaults without having a .vimrc in each user home :'(

Apparently /etc/vimrc and /etc/vimrc.local are ignored if there is no ~/.vimrc https://github.com/vim/vim/issues/2042

Not having mouse select by default is sooo annoying :'(

AmanicA
  • 205
  • Any edits to files in /usr/share/vim/ will be lost when the package is updated. Use something like https://vi.stackexchange.com/a/16103/205 instead – muru Aug 16 '23 at 07:26
  • @muru Thanks for mentioning that! So do you know what the right place to edit this is? – AmanicA Aug 16 '23 at 09:44
  • Your ~/.vimrc is the right place. And then your problem is that if you add it to ~/.vimrc, the syntax highlighting goes away. That's because defaults.vim is not sourced if ~/.vimrc exists, so you add unlet! skip_defaults_vim source $VIMRUNTIME/defaults.vim so the defaults are loaded anyway (put set mouse=v after the source). – muru Aug 16 '23 at 09:53
  • awesome, thanks @Muru that worked, Maybe post as an answer then I can vote for you – AmanicA Aug 16 '23 at 12:23
  • 1
    That's OK, you can upvote https://vi.stackexchange.com/a/16103/205, and update your answer to what worked for you – muru Aug 16 '23 at 12:43
0

As for the second part of your question where in you specified that when pasting from other applications to vim it adds tabs/spaces. Try adding this to your ~/.vimrc set paste or in vim before pasting in normal mode press "shift" + ":" and type "set paste"

abhixec
  • 497
0

You can use y to copy(yank) and p to paste even with :set mouse=a set in ~/.vimrc. Use v+arrow keys to highlight the text. To copy an entire line quickly, use shift+y.

  • 1
    To anyone wondering - y won't copy it to the system clipboard. So you can only paste back into vim. Which defeats 50% of the purpose of a clipboard. – Sridhar Sarnobat May 14 '22 at 05:20
0

Two things to add:

  1. Be aware many times set mouse=v won't work as expected depending on the other directives you have set before or after in your .vimrc file, it also depends on which terminal you are running vim from;
  2. I would recommend to keep mouse=a so you have all the mouse mode enabled at the same time and use the trick proposed by @BHS to highlight text while pressing the Shift key when you need to paste the highlighted text using the middle mouse button (Copying text outside of Vim with set mouse=a enabled).
AdminBee
  • 22,803