0

I am trying to set up a paste shortcut in my .vimrc.

So far I've tried remapping Shift+Insert:

noremap <S-Insert> <ESC>:paste<CR><S-Insert><ESC>:nopaste<CR>

...which did not work. I've also tried to map system clipboard pasting:

noremap ´v <ESC>:set paste<CR>"*p<ESC>:set nopaste<CR>

...but it did not work, either. The backtick ` is my personal leader key for several custom mappings. I have a German keyboard where this thing is easily accessible.

What am I doing wrong? I thought "*p is for pasting the system's clipboard...and I expected <S-Insert> to work normally as a command.

Any help would be really appreciated.

daniel451
  • 10,626
  • 19
  • 67
  • 125
  • Have you tried `noremap \`v "*p`? – melpomene Jul 31 '16 at 11:27
  • Does `"*p` work by itself, outside of any mapping? – melpomene Jul 31 '16 at 11:28
  • Yes, it does work...but the mapping doesn't :/ Is there any other way I can map paste behavior to vim? Including the `:set paste` and `:set nopaste` before and after the pasting? (otherwise the indendation is messed up) – daniel451 Jul 31 '16 at 11:31
  • `:set paste` is only needed for text coming from the terminal, not pasting from within vim. – melpomene Jul 31 '16 at 11:32
  • Oh yeah, probably I should've mentioned this: I am talking about pasting from the clipboard, thus I've copied code from somewhere else (terminal, stackoverflow, ...), so that my indentation is always messed up without `:set paste`. Since manually calling `:set paste`, going to insert mode, hitting `Shift+Insert` and so on is really annoying & time wasting I wanted to create some map for this... – daniel451 Jul 31 '16 at 11:37
  • when i set your map and hit *\`v*, I get `E20: Mark not set`, so probably vim complain about wheter its a mark or map! I suggest you to test map with other keys: `v` or `,v` or anything else and see if it works. Any way personally i use this method (https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode). this way you even dont need to `set paste` , `set nopaste` and ... it gets automatically injected when pasting. – dNitro Jul 31 '16 at 11:55
  • @ascenator What OS are you on? You said `"*p` works. What happens with `noremap \`v "*p`? – melpomene Jul 31 '16 at 11:57
  • @dNitro the backtick is my leader for many different things, like easymotion for example. Additionally I am not getting an `E20` error or something like that, it just happens nothing. However, the link you've supplied seems interesting, will check this :) – daniel451 Jul 31 '16 at 13:30
  • @melpomene I'm on Manjaro, a fork of Arch Linux. `"*p" itself (manually) works, but not the map'ed version :/ – daniel451 Jul 31 '16 at 13:33
  • @ascenator Which mapped version? The broken one with `:set paste` or the right one? – melpomene Jul 31 '16 at 15:21
  • @melpomene by manually I mean typing it manually in Vim. With "mapped version" I wanted to describe the mappings in the `.vimrc` file. – daniel451 Jul 31 '16 at 16:15

2 Answers2

3

If your Vim is not built with clipboard support (:echo has('clipboard') returns 0), get a better Vim.

If your Vim is built with clipboard support (:echo has('clipboard') returns 1), use "+p or "*p for which you don't need to set paste.

If "+{command} and "*{command} are too much for you, you can synchronize the unnamed register with the * register with:

set clipboard^=unnamed

or with the * register with:

set clipboard^=unnamedplus

or with both registers with:

set clipboard^=unnamed,unnamedplus

and simply use p.

If you still want a custom mapping:

nnoremap <key> "*p
nnoremap <key> "*P
xnoremap <key> "*p
xnoremap <key> "*P
romainl
  • 186,200
  • 21
  • 280
  • 313
0

For some reason <S-Insert> and "*p as well as "+p always work manually, but never work in any mapping for me - despite the fact that my "original" vim did not have clipboard support: :echo has('clipboard') returns 0.

I have just tested gVim and had the same problem, although this gVim had clipboard support: :echo has('clipboard') returns 1.

However, I found some workaround to get the desired behavior:

nnoremap `v <ESC>:set paste<CR><ESC>:r!xclip -o<CR>:set nopaste<CR><ESC>

One does only have to install xclip, a clipboard utility that is present in Manjaro, Arch, Debian & Ubuntu universe. I guess it is also available for many other distributions, but I have just tested this with the named ones.

This command sets vim to the paste mode (deactivates automatic indentation and stuff, so that some code's format does not get messed up when pasting), reads the clipboards buffer via the corresponding xclip command, and resets to normal indentation behavior with :set nopaste.

daniel451
  • 10,626
  • 19
  • 67
  • 125