views:

4178

answers:

8

In Vim, what is the command to correct the indentation of all the lines?

Often times I'll copy and paste code into a remote terminal and have the whole thing messed up. I want to fix this in one fell swoop.

+44  A: 

=, the indent command can take motions. So, gg to get the start of the file, = to indent, G to the end of the file, gg=G.

Logan Capaldo
worked beautifully. Thank you
Simucal
Nice - I didn't know you could do that.
LeopardSkinPillBoxHat
+1 but for a different reason. I've always used "1G" to go to the first line, "gg" would have save me millions of SHIFT-key-presses over my career :-)
paxdiablo
+1 for gg. I also came to know about that very recently. I used to do "<ESC>:1"
artknish
I'll never be able to unlearn my precious 1G =) One of my favorites is =% standing on an opening bracket. It fixes the indents of the whole block.
PEZ
:0<return> is not so bad but gg is nice.(yeah, I learned ed first)
Erik Olson
A: 

vi should respect tabs and spaces, however you should consider that vi may be using different length tabs than your other editor. Can you be any more specific than "whole thing messed up"?

Zachery Delafosse
I don't use tabs as indentation. What I mean by messed up is when you copy and paste code over a remote terminal each line is randomly offset from the beginning of the line.
Simucal
If Vim is set to auto-indent, then pasting code that already has space indentation will indent even farther. It's compounded with each pasted line.
Rob Kennedy
+1  A: 

In Vim, use :insert. This will keep all your formatting and not do autoindenting. For more information help :insert.

Eric Johnson
+16  A: 

Before pasting into the terminal, try :set paste (and then :set nopaste after you're done). This will turn off the auto-indent, line-wrap, etc. features that are messing up your paste.

edit: Also, I should point out that a much better result than = indenting can usually be obtained by using an external program. For example, I run :%!perltidy all the time. astyle, cindent, etc. can also be used. And, of course, you can map those to a key stroke — and map different ones to the same keystroke depending on file type

derobert
another useful answer! Thank you
Simucal
Thanks a million for ":set paste"! I've been lacking that knowledge for years on!
PEZ
You can set the `equalprg` option in a ftplugin to use an external filter for `=` indenting, rather than a custom keybinding.
jleedev
Theres also a pastetoggle keybinding option eg. :set pt \p to flip between modes
michael
+3  A: 

1G=G. That should indent all the lines in the file. 1G takes you the first line, = will start the auto-indent and the final G will take you the last line in the file.

jinxed_coder
+1  A: 

:set paste is your friend I use putty and end up copying code between windows. Before I was turned on to :set paste (and :set nopaste) copy/paste gave me fits for that very reason.

John Spooner
Yes, I'm also using putty. :set paste is awesome
Simucal
+1  A: 

You can use tidy application/utility to indent HTML & XML files and it works pretty well in indenting those files.

Prettify an XML file :!tidy -mi -xml %

Prettify an HTML file :!tidy -mi -html %

Naga Kiran
A: 

if you do not want to use :set paste, middle-click, set nopaste, you can also paste the content of the clipboard:

"*p
"+p

That way you don't have to leave normal mode. if you have to paste + or * depends on how you selected the text, see :help quoteplus.

0x89