2

Is there a command line utility to format multiple files from different programming languages ?

I was thinking something like:

format --indentation=spaces --size=2 --trim-whitespaces ./*.{php,js,cpp,sh}

If a command like this does not exist, how to do that in Vim? To format a single file in normal mode it is gg=G. But what about multiple files?

1 Answer 1

0

You can use vim in batch mode:

vim -c "normal gg=G" -c "x"  x.py

This will invoke gg=G command in normal mode then x (save and exit) command. For multiple files you can create simple loop with globs:

for i in ./*.{php,js,cpp,sh}; do
    vim -c "normal gg=G" -c "x" $i
done
1
  • For some weird reason this does not work inside loops. Doing vim -c "normal gg=G" -c "x" $filename works but if I do it inside a for loop or while loop vim will stop after the first files is formatted. From within loops Vim also alerts Vim: Warning: Input is not from a terminal
    – user462354
    Apr 3, 2021 at 21:09

You must log in to answer this question.