Vim has some pretty nice standarts on what indention etc. should look like, so it might make sense to run those from the command line on files without haveing to launch vim by hand. This is how it is done:
$ vim myfile.rb -s format.vim
Will execute all the commands specified in format.vim against the myfile.rb. The commands can be anything possible in vim in normal mode, so my example script will reindent the whole file (gg=G) and retab according to the rules, followed by a save.
Vim's retab command will change not only indentation but also spaces/tabs that aren't at the beginning of the line. Documentation invoked by
:h retab
includes this warning:You can get the same effect using the
expand
andunexpand
shell built-ins.I found this discussion while looking into whether vim could be used to reindent files that use spaces for indentation. After some searching, I'm using a solution that uses sed: