Vim
Text Editing
Coding Tips
Auto Indent
Pasting Text

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.

Browse interview questions

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:

vim
1:set autoindent?
2:set smartindent?
3:set cindent?
4:filetype indent on

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:

vim
:set paste

After the content is pasted, turn it back off:

vim
:set nopaste

That is the classic answer because it works reliably in terminal Vim and GUI Vim.

A typical workflow looks like this:

  1. Move the cursor where the pasted block should start.
  2. Run :set paste.
  3. Enter insert mode if needed.
  4. Paste using the terminal or operating-system paste shortcut.
  5. 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:

vim
set pastetoggle=<F2>

Now pressing F2 flips paste on and off. You can also create a visible mapping:

vim
nnoremap <leader>p :set paste!<CR>
set showmode

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:

vim
1set autoindent
2set smartindent
3set pastetoggle=<F2>
4
5" Optional visual reminder.
6set ruler
7set showcmd

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:

python
1def greet(name):
2    if name:
3        print(f"Hello, {name}")
4    else:
5        print("Hello")

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:

vim
u
:set paste

Then paste once more, followed by :set nopaste.

Alternatives That Sometimes Help

Depending on your setup, these can also be useful:

  • use "+p or "*p for clipboard registers if Vim has clipboard support
  • use :read !command when the source is shell output
  • paste into a temporary buffer first if you need to inspect formatting

For example:

vim
:read !python script.py

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 paste before pasting and :set nopaste after.
  • 'pastetoggle makes 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions