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

How do I paste text from outside Vim without messing up indentation?

Answer

:set paste

Explanation

The :set paste command enables paste mode, which temporarily disables auto-indentation, smart tabs, and other insert-mode features that can mangle text pasted from your system clipboard or terminal. Without paste mode, pasting formatted text into Vim often results in a cascade of unwanted indentation.

How it works

  • :set paste turns on paste mode — Vim disables autoindent, smartindent, cindent, and several other options that interfere with pasting
  • Paste your text using your terminal's paste shortcut (Cmd+V, Ctrl+Shift+V, or middle-click)
  • :set nopaste turns paste mode back off, restoring your normal insert-mode behavior

The problem paste mode solves

When you paste multi-line text into Vim through the terminal (not using Vim's p command), Vim sees each character as if you typed it manually. With autoindent enabled, each new line gets indented further than the last, creating a "staircase" effect:

function greet() {
    const name = "Vim";
        if (name) {
                console.log(name);
                    }
                        }

With :set paste enabled before pasting, the indentation is preserved correctly:

function greet() {
    const name = "Vim";
    if (name) {
        console.log(name);
    }
}

Tips

  • Always remember to run :set nopaste after pasting — leaving paste mode on disables useful features like auto-indentation and abbreviations
  • Use :set pastetoggle=<F2> in your vimrc to toggle paste mode with a single key press — press <F2> before and after pasting
  • If your Vim supports the +clipboard feature, use "+p to paste from the system clipboard instead — this does not require paste mode at all
  • In Neovim, paste mode is largely unnecessary because Neovim handles bracketed paste automatically
  • Use :set paste? to check whether paste mode is currently enabled
  • Some terminal emulators support bracketed paste mode, which tells Vim when a paste operation starts and ends — this makes :set paste unnecessary in modern setups

Next

How do I edit multiple lines at once using multiple cursors in Vim?