Better Pasting from the Clipboard

Something I find myself doing fairly regularly is copying something from outside of Vim into Vim. Usually this is something like a chunk of code from a Stack Overflow post, or a new reference to add to my Bibtex file (I’m in the middle of my University dissertation…). The problem with pasting some code in is, as you’ve probably seen, that Vim tries to indent the code as you paste it. If the code already has indents (which usually it does) this is a bit of a nightmare, as you end up with incorrect indenting and have to perfom == on a number of lines to correct it.

There is a better way. We can run :set paste, which tells Vim to be in paste mode. In this mode, when you’re in insert mode and paste in, Vim will not perform any indenting itself, and let what you’re pasting in dictate it. You can then run :set nopaste to turn off paste mode.

This is good, but it’s still too many steps for me:

  1. Exit insert mode.
  2. Run :set paste
  3. Go into insert mode.
  4. Hit Cmd+V to paste in.
  5. Exit insert mode.
  6. Run :set nopaste.

That’s far too much work, and far too many keystrokes. Thankfully, we can turn this into a handy mapping. I should add that credit for this mapping goes to Ben Orenstein, as it was taken directly from his .vimrc file, all I’ve done is write it up as I thought it warranted it. Here’s the mapping:

map <Leader>p :set paste<CR>o<esc>"*]p:set nopaste<cr>

Let’s step through that:

:set paste turns on paste mode
o inserts a new line below and goes back into normal mode
“*]p gets the content from the system clipboard, and pastes it in.… Click Here