vimtricks.wiki Concise Vim tricks, one at a time.

How do I assign a key to toggle paste mode on and off without typing :set paste each time?

Answer

:set pastetoggle=<F2>

Explanation

The pastetoggle option assigns a single key to toggle Vim's paste mode on and off without typing :set paste or :set nopaste every time. This is essential when working in terminal Vim and pasting from the system clipboard, where auto-indent, abbreviations, and insert-mode mappings would otherwise corrupt the pasted content.

How it works

Add to your ~/.vimrc:

set pastetoggle=<F2>
  • Press <F2> in normal or insert mode to toggle paste mode on
  • Vim displays -- INSERT (paste) -- in the statusline when active
  • Press <F2> again to return to normal insert behavior
  • Any key sequence works: <F2>, <F10>, or even a custom sequence

Example

Before pasting from the clipboard:

Press F2  →  paste mode ON  (auto-indent disabled)
Paste content from clipboard
Press F2  →  paste mode OFF (auto-indent restored)

Without this, pasting indented code into Vim's terminal interface cascades indentation levels.

Tips

  • Neovim and terminals supporting bracketed paste (most modern terminals) handle this automatically — pastetoggle is mainly needed in classic Vim or older terminal emulators
  • The toggle works in insert mode too, so you can enable paste mode without leaving insert
  • :set paste! also toggles from the command line without a key binding
  • If F2 conflicts with a terminal shortcut, try <F10> or <Leader>p

Next

How do I comment out all lines matching a pattern at once using vim-commentary?