Thursday, August 1, 2013

Re-indenting a script in VIM

I use vim daily on all of the systems I manage.  It's a critical tool for me; I can't stand emacs nor can I stand nano.  Whatever.

All of my scripts are perfectly formatted; I swear by this.  Four spaces for tabs.  Opening and closing braces are where they need to be.   I take pride in this.  But, every once in a while, you're going to run into a script or two that wasn't written by you- and you need this formatted proper. 

One note, this only works for scripts that VIM recognizes- so these obscure scripts won't get reformatted properly. 

When you open up the file (`vim`), while in command mode (not edit mode)- type gg=G.  That's without hitting the ":" first.   Literally,  hit 'g', then 'g' again, followed by '=' and *SHIFT*+'g'.  

It has saved me quite a bit of headaches, especially from people who insist tab should be two literal spaces.  /smh

Granted, you can map this to a fn key.  E.g. in .vimrc, have an entry somehwhere on a line showing (this is untested):

map <F5> mzgg=G`z<CR>

But, as I said, on over 50 machines so I'm not about to maintain 50 .vimrc files.  :)  Sometimes it's simpler to just remember the shortcut.

No comments:

Post a Comment