Child-friendly pasting in vim

If you’ve got various indenting and text wrapping options turned on in vim, pasting text into the editor results in screwed up results. You can get around this by turning on paste mode using :set paste and off with :set nopaste. To make things a little easier, you can use the following snippet in your .vimrc to allow you to toggle paste on and off using a single keypress:

nmap <F4> :set invpaste paste?<CR>
imap <F4> <C-O>:set invpaste<CR>
set pastetoggle=<F4>

(Warning: my vim settings have organically grown over the last 10 years, so they may not be the best or modern way of achieving an effect.)

Filed under vim

Tagged gotchastipsvim

4 comments

Comments are closed. These are preserved from the original site.

I just have:
<code>
set pastetoggle=<F6>
</code>

I see lots of references to invpaste, etc., but I cannot figure out what it achieves that the short version doesn't. It would be great if someone could explain this.
You don't actually need the mappings, just a single line is sufficient:

  set pt=<F11>  " 'pt' is short for 'pastetoggle'

Kelly: set invXXX toggles the option instead of just turning it on.
I prefer to just read in command output.  Either cat, or if installed, xclip, can be used:

! cat <hit enter, paste your text>
! xclip -o <reads current X clipboard buffer>

This sidsteps the indent / text-wrap issue entirely.
Karsten, do you mean
:r !cat<enter><paste><control-d>

Cheers,
Rajesh Duggal