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.
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.
=, 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
.
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"?
In Vim, use :insert
. This will keep all your formatting and not do autoindenting. For more information help :insert
.
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
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.
: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.
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 %
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
.