Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to write an insert mode mapping for pasting from the system clipboard. I have this:

imap <c-p> <c-o>:set paste<cr><c-r><c-r>+<c-o>:set nopaste<cr>

(Note that this is supposed to work according to this)

However when invoking the key mapping, it doesn't work, it stops short at the set paste part, so I just end up with paste mode enabled.

But if I do all those keys manually then I get what I want...so what's different when the commands are under a keymap? What am I missing to make this work?

I have also tried this:

imap <c-p> <esc>"+gP

This throws a slew of errors about gP being an unknown variable, but the same mapping in normal mode works as expected...so what's different with insert mode mappings?

share|improve this question
    
The author of the cited userobsessed article is not very experienced with Vim. I use vim in a terminal all day, have no problems pasting, and don't jump through all those hoops. To paste from the + register while maintaining the original formatting, use <C-R><C-O>+. See :help i_CTRL-R_CTRL-O for more on this. I don't know what's wrong with your second imap--it works fine for me. –  garyjohn Jan 31 '13 at 23:12
    
I did not see this comment until now, that's strange...did you edit? Anyway, thanks, that's how I ended up solving it. –  moljac024 Feb 1 '13 at 10:10

3 Answers 3

up vote 0 down vote accepted

You do not need to :set paste when inserting the clipboard's contents with i_CTRL-R. Because of the command, Vim already knows that this is from the clipboard. :set paste is for when you paste directly from the terminal (e.g. via the middle mouse button); there, Vim cannot differentiate between characters typed or pasted, and needs this hint.

To avoid auto-indenting, use i_CTRL-R_CTRL-O.

share|improve this answer
    
Yes but I noticed that when I paste code the indentation still gets destroyed without paste mode enabled. –  moljac024 Feb 1 '13 at 8:58
    
Then just use the right command: <C-R><C-O>{register} :-) –  Ingo Karkat Feb 1 '13 at 9:10
    
Yeah, that's what I ended up doing, but wasn't able to answer my own question for 8 hours :) Thanks! –  moljac024 Feb 1 '13 at 10:07

That second imap command should work fine. As I understand it, it should exit insert mode, paste from one of the system clipboards (+) then leave the cursor after the pasted text.

I suspect you are either running vi not vim or your vim is in compatible mode. The documentation for ["x]gP (:help gP) says "not in vi" so that might be your issue

share|improve this answer
    
I am certainly running vim and it is not in compatible mode. –  moljac024 Jan 31 '13 at 21:46

I still don't know what's wrong with the first mapping I tried, as I believe it should work, but looking into the vim documentation (specifically :help i_CTRL-R) I came up with a better solution:

inoremap <c-p> <c-r><c-o>+

From the docs:

CTRL-R CTRL-O {0-9a-z"%#*+/:.-=} *i_CTRL-R_CTRL-O* Insert the contents of a register literally and don't auto-indent. Does the same as pasting with the mouse ||. Does not replace characters! The '.' register (last inserted text) is still inserted as typed. {not in Vi}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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