Reputation: 1439
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.
Upvotes: 38
Views: 9929
Reputation: 1
Taking reference from the above answers I would like to make this complete. I will start from the scratch so that a beginner can understand.
Step-1 Install astyle (tool used for formatting ) by using the following command Open up a terminal. Type:
sudo apt-get install astyle
Step-2 Make sure you have vim installed on your system.Run the below commands from the directory in which your code files are.The below command will create a script that we intend to run recursively so as to beautify each and every file in our directory.(as mentioned in the above answer)
vim -w indentme.scr foo.c
Step-3 Then, type this exactly (in command mode) and press enter
:%!astyle
Step-4Then type this exactly (in command mode) and press enter
:wq
Step-5 Last run this recursively by the following command:
find . -type f -name '*.cpp' -exec vim -s indentme.scr "{}" \;
All your cpp files will be formatted properly.
Upvotes: 0
Reputation: 179
I went off of amphetamachine's solution. However, I needed to recursively search through multiple directories. Here's the solution that I used:
$ find . -type f -name '*.ttl' -exec vim -s indentme.scr "{}" \;
Upvotes: 3
Reputation: 2233
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 lineargdo
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.)
Upvotes: 69
Reputation: 30575
Open up a terminal. Type:
$ vim -w indentme.scr foo.c
Then, type this exactly (in command mode):
gg=G:wq
This will close vim, saving the process of indenting all lines in the file to a Vim script called indentme.scr
.
Note: indentme.scr
will contain a record of all key commands typed, so when you are done indenting the file, don't spend a lot of time using the arrow keys to look around the file, because this will lead to a much larger script and will severely slow down batch operations.
Now, in order to indent all the lines in a file, just type the following command:
$ vim -s indentme.scr unindented-file.c
Vim will flash open-shut (if you're on a fast computer and not editing a huge file), indenting all lines, then saving the file in-place.
Unfortunately, this will only work on one file at a time, but you can scale the functionality easily using sh
's for
loop:
for filename in *.ttl ; do
vim -s indentme.scr "$filename"
done
Note: This will save-over any file. Unless set bk
is in your ~/.vimrc
, don't expect a backup to be saved.
Upvotes: 12