Search code examples
vimeditorclipboard

How to disable vim pasting to/from system clipboard?


Basically, I need advice on how to do the opposite of what is described here:

How to make vim paste from (and copy to) system's clipboard?

I do not want vim to overwrite system clipboard when I delete something within the editor. The usual workflow for me is to select a piece of code (let us call it piece #1) with a mouse, then go to another piece of code (let us call it piece #2), select piece #2 in visual mode in vim, delete it using "c", and paste piece #1 using mouse middle button. With the new behavior, instead of pasting piece #1 I paste back piece #2.

In vim 7.2 I can do that, but in vim 7.4 I can not. I want to use 7.4 due to proper C++11 syntax highlighting but this changed default behavior is killing me.

I think, it has something to do with the "+xterm_clipboard" and/or "+clipboard" features of vim (vim --version), and I need to change them somehow to "-xterm_clipboard" and/or "-clipboard". I tried to recompile vim 7.4 with the option "--with-x=no" but somehow it did not help.

Please, help me to disable copying/pasting in vim to system clipboard by default.

Thank you!


Solution

  • Well, to answer the question in your title, you can just do

    :set clipboard=
    

    Which will make vim use it's internal register instead of the system one.

    But really, this behavior has nothing to do with the system clipboard. It's more just a general misunderstanding of vim registers.

    Vim by default "cuts" rather than "deletes". This means that every delete command (c and d) yanks to the unnamed register at the same time. To get around this, just use the black hole register, e.g. use "_c or "_d instead of just doing c or d.

    9. Black hole register "_               *quote_*
    When writing to this register, nothing happens.  This can be used to delete
    text without affecting the normal registers.  When reading from this register,
    nothing is returned.  {not in Vi}
    

    To make this the default behavior for your specific case, you can do

    xnoremap c "_d
    

    The 'x' in 'xnoremap' means this mapping applies to visual mode (with v) and select mode (with the mouse)

    Related SO link.