Automatically indent all code in vim

Asked

Viewed 2,109 times

0

It is possible to automatically make vim indent an entire file?

For example, in Eclipse you can use the shortcut Ctrl+A + Ctrl+I.

2 answers

0

For this operation it is possible to create a shortcut. Below is shown how to create a shortcut with F7:

map <F7> mzgg=G`z

0


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 the code.

To avoid this, use the command gg=G'' or gg=G``. The first goes back to the same row as the user was running the command, and the second goes back to the same position (same row and same column).

However, this command changes the position of the screen.

To avoid this, use the command mqHmwgg=G`wzt`q. Note that this command saves a macro to the marker w. Since the command is very large, I recommend creating a shortcut for it. For example: map <D-i> mqHmwgg=G`wzt`q.

Note: inspired response in this answer stack overflow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.