Reputation: 53
I want to run a vim command for example gg=G
but in the terminal.
I have tried vim gg=G filename
but it didn't work.
This would be useful as I normally work on gedit, but vim is very powerful, so would be cool to use some of its features without having to enter file in vim running the command and exiting vim, especially since exiting takes 14 contextually dependent instructions.
the other question with a similar name as this one, is about a different topic: How to run vim commands on vim it self. While this question is about runing vim command on a file
Upvotes: 5
Views: 3913
Reputation: 386
I guess you want to loop through every file and indent it, not even opening it.
If indent.vim file would be
:exec "normal gg=G"
:wq
Run for example in all js files:
for filename in ./*.js; do
vim -es ./$filename <indent.vim
-e runs the Vim in ex mode on file filename and reads from file indent.vim
-s operate in silent mode, i.e. does not output any prompt (like the : prompt)
If you open 1 by 1 file. Others answers are great, but just for autoindent, the ALE plugin can make that automatic.
Or check its linters for your file extension, e.g. for javascript: link, I am sure eslint has a shell command flag to just indent a file. Something like:
for filename in ./*.js; do
eslint --flagToIndent $filename;
1st open desired files (might bug if many or heavy files):
$ vim -p ./*.js;
2nd run in any
:silent! argdo exec "normal gg=G"
Upvotes: 3
Reputation: 193
Vim can run commands perfectly the way I interpret your desires.
You simply use ex +"<command>" <filename>
or vim -c "<command>" <filename>
, they are equivalent.
Try running the following:
$ ex +"norm gg=G" +"wq" <filename>
It will open your file, filter the whole file with the first command and save it (overwriting) with the wq
command.
Upvotes: 5
Reputation: 6036
vim --help
does state multiple possibilities for that:
vim --cmd run command before config is loaded
vim -c run command after config is loaded
vim -s read normal mode commands from <scriptin>
Upvotes: 2
Reputation: 506
you have to insert all your command inside a file, for example:
$ cat pippo
gg=G<enter>
after that you have to run vim -s script yourfile, like this:
$ vim -s pippo yourfile.txt
Upvotes: 1