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 pasteturns on paste mode — Vim disablesautoindent,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 nopasteturns 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 nopasteafter 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
+clipboardfeature, use"+pto 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 pasteunnecessary in modern setups