Thu, 24 Jan 2008

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.)

[vim,gotchas,tips] | # Read Comments (4) |

Comments

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.
Posted by Kelly Clowers at Thu Jan 24 16:29:07 2008
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.
Posted by Marius Gedminas at Thu Jan 24 21:52:23 2008
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.
Posted by Karsten M. Self at Thu Jan 24 22:25:36 2008
Karsten, do you mean
:r !cat<enter><paste><control-d>

Cheers,
Rajesh Duggal
Posted by Rajesh Duggal at Wed Apr 9 18:22:46 2008

Name:


E-mail:


URL:


Comment:


Please enter "fudge" to prove you are a human