-1

I don't know why but Vim always mess up my formatting if I am copying code from some external program through CTRL + V + P shortcut. For instance this snippet of XML code

<dependency>
    <groupId>org.jboss.test</groupId>
    <artifactId>richfaces-selenium</artifactId>
    <scope>test</scope>
</dependency>

is copied into Vim like this (sorry for | characters, that is from indentLine plugin )

<dependency>
    │   │   │   │   │   <groupId>org.jboss.test</groupId>
    │   │   │   │   │   │   │   │   <artifactId>richfaces-selenium</artifactId>
    │   │   │   │   │   │   │   │   │   │   │   <scope>test</scope>
    │   │   │   │   │   │   │   │   │   │   │   │   │   </dependency>

So how do I fix this behaviour? Something similar also happens when I paste Java or Python code.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115

2 Answers2

2

Use the paste mode in Vim, which disables the auto-indentation. This can be done while in insert mode. :help paste

Joce
  • 2,220
  • 15
  • 26
0

I have always had that problem. Personally I have not tried to fix how I paste it in, but I do a quick fix after I paste it in.

After you past your code, Hit Esc a few times and do this command

gg=G

Basic breakdown:

gg --> Go to beginning of File
=  --> Indent
G  --> Go to end of File

Should indent everything correctly, based on your indentation settings on .vimrc (if you have none, then it will just do basic tab indentation).

dan.m.kumar
  • 758
  • 2
  • 9
  • 20