views:

905

answers:

4

Unfortunately, I am not an experienced vim user. But, I am making the effort to learn it.

When I paste code into my document from the clipboard, I get extra spaces at the start of each new line:

line
  line
    line

I know you can turn off auto indent but mine doesn't seem to work because I have some other settings conflicting or something (which look pretty obvious in my .vimrc but don't seem to matter when I take them out).

Can someone please show me the way to turn this off when I paste code but still have vim auto indent when I am writing code? Please see my .vimrc file:

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
set bg=dark
set nowrap

Many thanks

+13  A: 

To turn off autoindent when you paste code, there's a special "paste" mode.

Type

:set paste

Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

:set nopaste
Pavel Shved
This isn't any easier than `:set noai` followed by `:set ai`. The suggestion of `:r! cat` is shorter.
Leopd
+6  A: 

A useful command to have in your .vimrc is set pastetoggle=<F10> or some other button, to easily toggle between paste and nopaste.

Jacob R
A: 

I didn't know about set paste. It feels like the right answer. I've been using set noai for years. (noai is an abbreviation for noautoindent)

Levi
He he! I'm glad you learned something too!
Rimian
+1  A: 

I usually use :r! cat and then paste ( shift + insert ) the content, and CTRL+D.

No need to enable & disable, direct usage.

thegeek