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

4 thoughts on “Child-friendly pasting in vim

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.