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

有些话、适合烂在心里 提交于 2019-11-27 09:55:16
Logan Capaldo

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

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

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

[[=]]

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

=%
Naga Kiran

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 %

press escape and then type below combinations fast:

gg=G
Amjith

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.

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

Elsporko

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

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.

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

Pierz

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.

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"?

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 )

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

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.

Priya Sharma

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"); 
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!