Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting in VIM with all the files in Folder

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.

like image 678
hari Avatar asked Jul 10 '10 08:07

hari


People also ask

How do I indent all codes in Vim?

Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.

How do I auto indent the whole file?

Fix indentation in the whole file Start in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

How do I indent multiple times in Vim?

Select what you want (typically with v or Shift + v ) then type 5> . If you need to fix or repeat the same selection, use gv . Show activity on this post. You can select the current line by pressing v , and then type 5> to indent the current line 5 times, the equivalent of pressing > 10 times.

How do I indent a file in Vim?

I use formatpgm with tidy and astyle and then gq. Here are some examples from my .vimrc: au FileType xml set fp=tidy\ -q\ -i\ -xml and au FileType java set fp=/usr/local/bin/astyle\ --mode=java\ --indent=tab This indents the entire file! And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.

How do I indent the last line in a file?

You can use tidy application/utility to indent HTML & XML files and it works pretty well in indenting those files. I typed slowly, you won't believe what happened next. 1G=G. That should indent all the lines in the file. 1G takes you the first line, = will start the auto-indent and the final G will take you the last line in the file.

How to insert text without auto indentation in vi editor?

For vi Editor, use :insert. This will keep all your formatting and not insert auto-indenting.Once done press escape to view the actual formatted file otherwise you'l see some garbage characters. like ^I e.g:

Why can't I use Vim's = filter command in C++?

For complex C++ files vim does not always get the formatting right when using vim's = filter command. So for a such situations it is better to use an external C++ formatter like astyle (or uncrustify) e.g.:


2 Answers

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.)

like image 129
nicholas a. evans Avatar answered Oct 05 '22 10:10

nicholas a. evans


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.

like image 24
amphetamachine Avatar answered Oct 05 '22 12:10

amphetamachine