Is it possible to have vim automatically indent an entire file?
For example, in Eclipse you can use the shortcut Ctrl + A + Ctrl + I.
Is it possible to have vim automatically indent an entire file?
For example, in Eclipse you can use the shortcut Ctrl + A + Ctrl + I.
To indent an entire file in vim, just type the command gg=G
.
However, this command causes the cursor position to change to the first line of code.
To avoid this, use the command gg=G''
or gg=G''
. The first back to the same line as the user was when executing the command, and the second back to the same position (same row and same column).
However, this command causes the screen position to change.
To avoid this, use the mqHmwgg=G'wzt'q
command. Note that this command writes a macro to the w
label. Since the command is too large, I recommend creating a shortcut for it. For example: map <D-i> mqHmwgg=G'wzt'q
.
Note: inspired response in this answer of Stack Overflow.
For this operation you can create a shortcut. Here's how to create a shortcut with F7:
map <F7> mzgg=G'z