Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I copy some code from webpages and paste it to VIM,I find it becomes a mess style like a ladder as follows


Since it messed so regularly so I think maybe there's something wrong with my .vimrc which is as below:

set number
set nocompatible
set nowritebackup
set noswapfile
syntax on
filetype indent on
filetype plugin on
filetype on
set background=light
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions=T
set fileencodings=utf-8,prc
set ruler
set incsearch
map gs :%s
set t_Co=256
:colorscheme evening
filetype plugin indent on

Usually I write python in VIM.And help would be appreciated.

share|improve this question
Thanks. This irked me for years, and I never got around to asking anyone about it. +1. –  DevSolar Apr 26 '10 at 9:15
add comment

3 Answers

up vote 23 down vote accepted

Do this before:

:set paste

Then after:

:set nopaste
share|improve this answer
Not needed for gvim; it turns these on and off automatically when pasting. –  Benjamin Bannier Apr 26 '10 at 4:49
any idea why that is? –  Nathan Fellman Apr 26 '10 at 10:13
Just guessing: Plain vim is command-line only, and doesn't "know" about the clipboard. It takes the "paste" as a input character sequence, and applies auto-indenting. The GUI-aware gvim might "know" that the incoming data is from the clipboard, and thus disable the autoindenting for the purpose. –  DevSolar Apr 26 '10 at 10:55
That's correct. The terminal treats the "paste" operation as raw character input. –  Ignacio Vazquez-Abrams Apr 26 '10 at 11:16
This isn't purly a vim issue - you can get a similar affect pasting into the old DOS editor in a command prompt. This program seems to be missing in Windows 7 64-bit - presumably because it really is an old 16-bit DOS app and it wasn't worth the hassle of updating it. It's in Windows XP, though, and pasting into that (1) works really slowly, acting out the character-by-character input, and (2) makes a real mess. –  Steve314 Jul 17 '12 at 4:17
add comment


Toggle auto-indenting for code paste


If you use Vim commands to paste text, nothing unexpected occurs. The problem only arises when pasting from another application, and only when you are not using a GUI version of Vim. In a console or terminal version of Vim, there is no standard procedure to paste text from another application. Instead, the terminal may emulate pasting by inserting text into the keyboard buffer, so Vim thinks the text has been typed by the user. After each line ending, Vim may move the cursor so the next line starts with the same indent as the last. However, that will change the indentation already in the pasted text.

Paste toggle

Put the following in your vimrc (change to whatever key you want):

set pastetoggle=<F2>

To paste from another application:

  • Start insert mode.
  • Press F2 (toggles the 'paste' option on).
  • Use your terminal to paste text from the clipboard.
  • Press F2 (toggles the 'paste' option off).

Then the existing indentation of the pasted text will be retained.

You do not have to start insert mode first, but if you are in normal mode and have a mapping for F2, that mapping will apply, and the 'pastetoggle' function will not operate.

Some people like the visual feedback shown in the status line by the following alternative for your vimrc:

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode

The first line sets a mapping so that pressing F2 in normal mode will invert the 'paste' option, and will then show the value of that option. The second line allows you to press F2 when in insert mode, to toggle 'paste' on and off. The third line enables displaying whether 'paste' is turned on in insert mode.

share|improve this answer
+1 for the hotkey solution. For the not-too-firm in VIM (like me) who already have mapped the F-keys to something else (like me), <C-F2> would be Ctrl-F2... –  DevSolar Apr 26 '10 at 10:53
excellent - I am now using this –  Richard Feb 12 '11 at 0:11
add comment

It is the autoindent that is messing with you.

set autoindent
set smartindent

Try to disable them when you cut and paste your code, and then enable them again when you are done.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.