4

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

4 Answers4

4

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.

Roger
  • 163
  • 7
  • Thank you @Roger the `ex +"norm gg=G" +"wq" ` works like a charm! Could you explain what the `norm` dos? Also when I use the `vim -c "" ` I get an error: `Error detected while processing command line: E492: Not an editor command: gg=G` – Carlos Martins Dec 16 '20 at 09:45
  • 1
    One can run **normal-mode** cmds (e.g. `G` to go EOF) as well as **exec-mode** (e.g. `:set textwidth=79`).Example bash running **both**: `$ vim ~/.vimrc -c ":set foldcolumn=6 textwidth=0 | :execute 'normal zR' | /map"` where `zR` unfold all and `/map` searches maps – Xopi García Dec 16 '20 at 10:59
  • I now understand why `vim -c "norm gg=G" + "wq" ` was not working. The correct syntax would be `vim -c "norm gg=G" -c "wq" `. Notice that we use the -c flag 2 times instead of using the `+` sign. – Carlos Martins Dec 29 '20 at 20:49
3

I guess you want to loop through every file and indent it, not even opening it.

  • solution A.1: through shell file-loop. Self-made Vim script.

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)

  • solution A.2: through shell file-loop. Using linters.

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;
  • solution B: or through Vim argdo, bufdo,...

1st open desired files (might bug if many or heavy files):

$ vim -p ./*.js;

2nd run in any

:silent! argdo exec "normal gg=G"
Xopi García
  • 359
  • 1
  • 2
  • 9
  • Shell flags better explained [https://stackoverflow.com/a/12410259/9391770](https://stackoverflow.com/a/12410259/9391770) – Xopi García Feb 16 '21 at 15:35
  • I'm using MAKE for this porpose. I just creat an instruction on make to indent each file and then make a **indent all** instruction that call all other indent instruction. I call this indent all instruction on my make compile. – Carlos Martins Jul 31 '21 at 05:06
  • @CarlosMartins I think you are trying to reinvent the wheel. The [makefiles](https://www.computerhope.com/unix/umake.htm) are not aimed to replace Vim indent strategies (linters). Maybe you can help the community improving the auto indents from [coc-nvim](https://github.com/neoclide/coc.nvim/wiki). – Xopi García Jul 31 '21 at 13:13
2

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>
Doktor OSwaldo
  • 5,732
  • 20
  • 41
1

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
Lews
  • 426
  • 4
  • 9
  • this dos works but I need to put the command inside the same folder I'm working on. So I need to copy a file to every folder I'm working on? I gess I could use alias or something give it a absolute path to script. Thanks – Carlos Martins Dec 16 '20 at 06:56
  • `pippo` file is not needed to be in same directory as `yourfile.txt`. Run: `pippo="./zzz_deleteMe.txt"; echo "gg=G" > $pippo; vim -s $pippo ~/.vimrc;` – Xopi García Dec 16 '20 at 11:24