1

What is the best way of linking the Ctrl-V key combination to pasting the clipboard in insert mode? I used

map <C-v> "+gP
imap <C-v> <C-r><C-p>+

in .vimrc. However in insert mode, this definition sometimes doesn't work, it pasts the inserted text in a new line above the current cursor position.

Edit: I have a more detailed bug description now: The problem seems to appear when the String in the clip board terminates with a new line. Here a receipt to reproduce:

I open gvim and a LibreOffice word processor window. In LibreOffice I type

Hello Test 
Foo bar 

Then I select "Hello Test"+the line break (but not the 'F' of foo) and press Ctrl-c. In Gvim I enter insertmode, Tyle Enter (new line), type 'some text', press Ctrl-v. The Result will be that "Hello Test" is inserted in a new line above "some text" and the cursor is positioned at first char of the "some text" line.

If in LibreOffice I copy only "Hello Test" without the linebreak, the behavor is normaL ("Hello Test" will be inserted after "some text" and the cursor is right of the inserted string).

This problem is not specific to LibreOffice, it also happens when I copy paste full lines from PDF Files with okular or acroread. Generally it happens always when the copy pasted string terminates in newline.

Any ideas?

  • 2
    Why do you have the <C-p>? I think that's what's causing your problem. – Tumbler41 Apr 10 '17 at 19:43
  • 2
    @Tumbler41 :h i_CTRL-R_CTRL-P – Tommy A Apr 11 '17 at 2:34
2

I think the problem is that the + register contains linewise text. I can't say if that's a bug or not.

This seems to give the same effect while ignoring the register type:

inoremap <c-v> <esc>:set paste<cr>a<c-r>=getreg('+')<cr><esc>:set nopaste<cr>mi`[=`]`ia

It temporarily enables the paste option to enter text from the expression register literally. By using getreg('+') you're getting the text without consideration of the register type and placing it at the cursor. mi`[=`]`ia is used to save the cursor position, format the inserted text, and return the cursor.

  • This indeed solves the problem! (Sorry for my earlier comment stating the contrary.) – LaTechneuse Apr 11 '17 at 16:50
  • @Tommy A, Any reason you didn't use <C-o> instead of <esc>...ia? – Tumbler41 Apr 11 '17 at 16:52
  • @Tumbler41 If the insert cursor is at the end of the line, it will re-enter insert mode one character to the left. So, I chose <esc>...a because it was more predictable. – Tommy A Apr 11 '17 at 17:40
  • 1
    @TommyA I don't see that behavior with <C-o>. For me, it's returning to insert mode after the last character if that's where it was (unless the normal mode command moved the cursor). But on the other hand, at the beginning of the line, <esc>...a returns to insert mode after the first character, instead of before it. With your mapping, this may result in the paste happening in the wrong place. – 8bittree Apr 11 '17 at 18:26
  • @8bittree Hmm, you're right. It appears to be a bug in Neovim, but I can't reproduce it with -u NONE. Thanks for pointing that out. – Tommy A Apr 11 '17 at 20:34
2

It has to do with the <C-p>. I know it's supposed to "Insert the contents of a register litterally", but it's causing problems. I've reproduced your problem with Microsoft Word and gVim 8.0.69. But If I change the mapping to:

imap <C-v> <C-r>+

it works as expected. It might be a bug, but I'm not sure.

  • Thanks! This also solves my problem. But now I'm confused seeing these two solutions. The one simple the other complicated. Does anyone know which one is "better"? ;) – LaTechneuse Apr 11 '17 at 16:51
  • I've never used the <C-p> option before, but it sounds like it does some indenting. I've never used it, but Tommy A's solution might preserve this behavior. – Tumbler41 Apr 11 '17 at 16:56
  • <c-r>+ will work just as well as <c-r>=getreg('+') in my answer. I just prefer getreg('+') because it's "less mysterious" in my mind. – Tommy A Apr 11 '17 at 17:45
  • Now I found a difference between the two solutions: If I copy paste indented source code e.g. from stackexchange, indentation will be random and rather unreadable with the imap <C-v> <C-r>+ setup, but it will be preserved with the more complicated solution suggested by Tommy A. To see the difference, go to a random site such as stackoverflow.com/questions/43338084/, copy the code example and past it to gvim. – LaTechneuse Apr 12 '17 at 10:17
  • Yes, that's due to the :set paste, :set nopaste. It probably should be set when pasting, so his is probably better. – Tumbler41 Apr 12 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.