Indenting in VIM with all the files in Folder

hari picture hari · Jul 10, 2010 · Viewed 8.3k times · Source

I have a folder containing hundreds of TTL (TeraTermLanguage) files. Now I wanted indent all these files.

I have created teraterm.vim for indentation and I open a file using VIM and do "gg=G" and whole file gets indented properly.

But is there any way, where I can indent all the files in folder.

I wanted to do with help of Shell. But in VIM I couldnt pass file indent command as the argument to VIM.

Please suggest which is the best way I can do indentation to all the files in VIM.

Answer

nicholas a. evans picture nicholas a. evans · Jul 15, 2010

Much simpler than scripting vim from the bash command line is to use vimscript from inside of vim (or perhaps a much simpler one-liner for scripting vim from the command line). I personally prefer using the arg list for all multi-file manipulation. For example:

:args ~/src/myproject/**/*.ttl | argdo execute "normal gg=G" | update
  • args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
  • | lets us run multiple commands on one line
  • argdo runs the following commands on each arg (it will swallow up the second |)
  • execute prevents normal from swallowing up the next pipe.
  • normal runs the following normal mode commands (what you were working with in the first place)
  • update is like :w, but only saves when the buffer is modified.

This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via %s/foo/bar/ge or setting uniform fileformat or fileencoding).

(other people prefer a similar pattern using the buffer list and :bufdo, but with the arg list I don't need to worry about closing current buffers or opening up new vim session.)