Turning off auto indent when pasting text into vim
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When pasted text gets shifted to the right, double-indented, or filled with unwanted tabs in Vim, the problem is usually not the clipboard. It is Vim doing exactly what it was configured to do: applying indent logic as if you were typing the text line by line.
The fix is to use paste-aware behavior while inserting the block. Vim has built-in support for this, and once you know the workflow the problem becomes predictable.
Why Pasted Text Gets Mangled
Vim can apply several indentation features while you type:
- '
autoindent' - '
smartindent' - '
cindent' - filetype-specific indent scripts
Those features are useful during normal editing, but they interfere with pasted blocks. A line copied from a shell, browser, or another editor may trigger automatic reindentation on every newline.
You can inspect some of the relevant settings with:
The point is not to disable indentation forever. It is to suspend those features temporarily during the paste itself.
Use paste Mode for the Paste Operation
Vim provides a dedicated option for this:
After the content is pasted, turn it back off:
That is the classic answer because it works reliably in terminal Vim and GUI Vim.
A typical workflow looks like this:
- Move the cursor where the pasted block should start.
- Run
:set paste. - Enter insert mode if needed.
- Paste using the terminal or operating-system paste shortcut.
- Run
:set nopaste.
If you forget step 5, normal typing will feel wrong because features such as autoindent and some mappings remain suppressed.
Make Toggling Easier
Typing two Ex commands every time gets old quickly, so many users add a toggle in ~/.vimrc:
Now pressing F2 flips paste on and off. You can also create a visible mapping:
That gives you a fast toggle while staying explicit about the editor state.
A Minimal .vimrc Example
If pasted text is a recurring problem, this is a reasonable starting setup:
This keeps indentation features enabled for normal editing while giving you a safe escape hatch for pastes.
Modern Terminals and Bracketed Paste
In many modern terminal setups, Vim can detect a paste automatically through bracketed paste support. When that works, large pasted blocks often arrive cleanly without manual toggling.
Still, manual :set paste remains worth knowing because:
- not every terminal handles bracketed paste consistently
- remote SSH sessions can behave differently
- custom mappings or plugins can still interfere
So even if your current environment mostly works, the built-in paste mode is the reliable fallback.
A Quick Demonstration
Suppose a Python function copied from a web page keeps getting extra indentation:
Without paste mode, Vim may reindent lines according to the current cursor position and filetype rules. With paste mode enabled first, the block is inserted exactly as copied.
If you already pasted a mangled block, you can often recover by undoing, enabling paste mode, and pasting again:
Then paste once more, followed by :set nopaste.
Alternatives That Sometimes Help
Depending on your setup, these can also be useful:
- use
"+por"*pfor clipboard registers if Vim has clipboard support - use
:read !commandwhen the source is shell output - paste into a temporary buffer first if you need to inspect formatting
For example:
That inserts command output without using the terminal paste path at all.
Common Pitfalls
The most common mistake is enabling paste and forgetting to disable it. The next time you type, indentation and some insert-mode behavior feel broken.
Another pitfall is trying to solve the problem by permanently turning off indentation features. That avoids paste damage, but it also throws away useful editing behavior for normal coding.
Users also confuse terminal paste shortcuts with Vim commands. The terminal shortcut performs the paste, but Vim still decides how to handle the inserted text unless paste-aware behavior is active.
Finally, do not assume one environment matches another. A local terminal, a remote session, and a GUI Vim instance can all behave differently.
Summary
- Pasted text is usually mangled because Vim applies normal indent rules to each new line.
- Use
:set pastebefore pasting and:set nopasteafter. - '
pastetogglemakes the workflow practical for frequent use.' - Modern terminals sometimes handle paste automatically, but manual paste mode is still the reliable fallback.
- If a paste goes wrong, undo it, enable paste mode, and paste again.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.