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

xxxxxx
   xxxxxx
      xxxxxx
         xxxxxxxxxx

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.

link|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
feedback

3 Answers

up vote 8 down vote accepted

Do this before:

:set paste

Then after:

:set nopaste
link|improve this answer
Not needed for gvim; it turns these on and off automatically when pasting. – honk 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
feedback

http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste

Toggle auto-indenting for code paste

Background

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.

link|improve this answer
1  
+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
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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