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.
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.
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
=%
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.
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.
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 )
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");
}