77

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.

10
  • 4
    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, 2014 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! :) Jun 27, 2014 at 20:07
  • Is this macvim?
    – phemmer
    Jun 28, 2014 at 0:00
  • no it is not its just iTerm (as I mentioned on the question, but good question though). Jun 28, 2014 at 1:17
  • 3
    @Pinocchio - take a look here: How to make vim paste from (and copy to) system's clipboard?.
    – slm
    Jun 28, 2014 at 12:10

12 Answers 12

107
+25

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.

14
  • 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 3, 2014 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. Jul 3, 2014 at 15:57
  • 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? Jul 7, 2014 at 14:57
  • 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 7, 2014 at 15:06
  • 1
    Thanks a lot, this suggestion work like a charm with mouse=v Mar 11, 2019 at 12:17
32

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.

3
  • 1
    You are God...... :')
    – omerjerk
    Dec 14, 2016 at 6:54
  • 7
    For me on OSX works "fn" instead of "shift" or "alt".
    – ssasa
    Sep 10, 2017 at 14:17
  • Setting mouse=v worked for me with straight cmd + c Jul 24, 2020 at 13:36
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 :)

10

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

1
  • 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 2, 2015 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.
1
  • 1
    Thanks for contributing that reference, but, does it answer the question? If so, can you identify the part of the quote that does so, presenting a summary of the actual answer? Feb 23, 2015 at 18:36
3

On my Mac, what helped were those two things:

  • :set mouse-=a
  • fn+click (instead of shift or alt)
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
1

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
  • 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. May 14, 2022 at 5:20
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 :'(

5
  • Any edits to files in /usr/share/vim/ will be lost when the package is updated. Use something like vi.stackexchange.com/a/16103/205 instead
    – muru
    Aug 16 at 7:26
  • @muru Thanks for mentioning that! So do you know what the right place to edit this is?
    – AmanicA
    Aug 16 at 9: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 at 9:53
  • awesome, thanks @Muru that worked, Maybe post as an answer then I can vote for you
    – AmanicA
    Aug 16 at 12:23
  • 1
    That's OK, you can upvote vi.stackexchange.com/a/16103/205, and update your answer to what worked for you
    – muru
    Aug 16 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"

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .