Wow, for the longest time I've been suffering when using VIM to read and write code because I never knew how to reset the indentation! It's seriously amazing how much cleaner and clearer a piece of code can be when it's properly indented. Take this bit of wisdom, and don't forget it!
The Magical VIM Auto-Indenting Shortcut
As soon you open a file in VIM, just press the "g" key twice. This should move the cursor to the beginning of the file. Then hit the "=" key. Finally, hold "shift" and hit the "g" key again. This is basically telling VIM to, "remove all trailing spaces from my cursor position until the end of the file". And so these two commands together will correctly auto-indent the entire file. Pretty slick!
Adjust Default Spaces Per Tab
One thing that bothered me about the "gg =G" auto-indenting was that it was making each tab a monstrous 8 spaces wide when I preferred it to be only 2 spaces wide. However, we can easily change this by editing our vimrc file:
and adding this little snippet of code:
And that's all there is to it! I hope you enjoyed this post and have many hours of joyous glee from looking at your beautifully indented code!
The posts on this site are written and maintained by Jim Lynch. About Jim...