ChatGPT解决这个技术问题 Extra ChatGPT

How do I fix the indentation of an entire file in Vi?

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.

Prevention is better than cure. In that spirit comes this comment. Do a ":set paste" before entering insert mode and pasting code from remote terminal.

i
ib.

=, 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.


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.
:0 is not so bad but gg is nice. (yeah, I learned ed first)
Can I indent the entire file without leaving the current line?
@Fábio: '' (two single quotes) takes you back to where you were so gg=G'' should indent then return.
@ArchimedesTrajano Mac OS's stock vim is very old, I recommend HomeBrew's version; it's much newer. I had some annoying issues with Mac's vim. Also, I heard that you should not mess with the built-in one, so don't try to update it yourself unless you're very careful.
t
the Tin Man

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 and other 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.


You can set the equalprg option in a ftplugin to use an external filter for = indenting, rather than a custom keybinding.
Theres also a pastetoggle keybinding option eg. :set pt \p to flip between modes
Note: in grml's vimconfig the pastetoggle key is mapped to F11
I use formatpgm with tidy and astyle and then gq. Here are some examples from my .vimrc: au FileType xml set fp=tidy\ -q\ -i\ -xml and au FileType java set fp=/usr/local/bin/astyle\ --mode=java\ --indent=tab
just downloaded perltidy after reading this, it's so much better than the default vim auto indent
S
Sagar Jain

The master of all commands is

gg=G

This indents the entire file!

And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.

To indent the all the lines below the current line

=G

To indent the current line

==

To indent n lines below the current line

n==

For example, to indent 4 lines below the current line

4==

To indent a block of code, go to one of the braces and use command

=%

Thanks! Using =G enables you to repeat the command throughout tabs with the . (dot) command.
A
Andrew Wagner

If you want to reindent the block you're in without having to type any chords, you can do:

[[=]]

Sorry to revive this, but what did you mean by chords? Coords?
"Chords" here refers to commands issued by holding down one key while pressing another, in analogy to musical chords were several notes sound at once. So G is shift+g, ^] is ctrl+], and so on. These take longer to type than single-key bindings.
You can also use =aB which will not move cursor at all.
On the Nordic keyboard, [[=]] requires 5 chords :(
i
ib.

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 %

t
tokhi

press escape and then type below combinations fast:

gg=G

I typed slowly, you won't believe what happened next.
I just gave the answer and the comment an upvote because that was priceless! lol @k0pernikus
i
ib.

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.


Or even 1gg=G
1G=G fixed it for me. Awesome!
0
0x89

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.


E
Eric Johnson

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


i
ib.

: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.


Yes, I'm also using putty. :set paste is awesome
C
Community

For complex C++ files vim does not always get the formatting right when using vim's = filter command. So for a such situations it is better to use an external C++ formatter like astyle (or uncrustify) e.g.:

:%!astyle

Vim's '=' function uses its internal formatter by default (which doesn't always gets things right) but one can also set it use an external formatter, like astyle, by setting it up appropriately as discussed in this question.


t
the Tin Man

vim-autoformat formats your source files using external programs specific for your language, e.g. the "rbeautify" gem for Ruby files, "js-beautify" npm package for JavaScript.


a
alphayax

For XML files, I use this command

:1,$!xmllint --format --recover - 2>/dev/null

You need to have xmllint installed (package libxml2-utils)

(Source : http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html )


S
Sergio D. Márquez

You can create a mapping to do this for you.

This one will auto indent the whole file and still keep your cursor in the position you are:

nmap <leader>ai mzgg=G`z

R
Ramanand Yadav

Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.


I'd avoid this. Visual mode is for when you're unsure about your motions, if you know how to capture a set of characters then there's no point to the extra step of dropping into visual mode. Why ggvG= when you can gg=G. What was the point in making the motion visible? That goes for all actions which take motions.
A
Alex

For vi Editor, use :insert. This will keep all your formatting and not insert auto-indenting.Once done press escape to view the actual formatted file otherwise you'l see some garbage characters. like ^I e.g:

public static void main(String[] args) {
^I
^I System.out.println("Some Garbage printed upon using :insert"); 
}