I am writing a bash script that runs a command line program (Gromacs), saves the results, modifies the input files, and then loops through the process again. I am trying to use VIM to modify the input text files, but I have not been able to find a way to execute internal VIM commands like :1234, w, x, dd, ect. from the .sh file after opening my input files in VIM ("vim conf.gro").
Is there any practical way to execute VIM commands from the shell script?
You can script Vim via the
-c
flag.However that only gets you so far.
:normal
. e.g.:norm dd
commands.vim
) and then:source
via-S
.:h ex-cmd-index
So you will end up with something like this. With all your vim commands inside of
commands.vim
.You may also want to look into using
sed
and/orawk
for text processing.Alternatives
Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like
sed
,awk
, or Perl / Python / Ruby / your favorite scripting language here.That said, you can use Vim non-interactively:
Silent Batch Mode
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a
:substitute
command), use Ex-mode.Note: silent batch mode (
:help -s-ex
) messes up the Windows console, so you may have to do acls
to clean up after the Vim run.Attention: Vim will hang waiting for input if the
"commands.ex"
file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the-
argument.Full Automation
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
Here's a summary of the used arguments:
I think
vim -w/W and vim -s
is what you are looking for.The "vim operations/key sequence" you could record with
vim -w test.keys input.file
also, you could write thetest.keys
too. e.g. save this in the file:this will do:
with this
test.keys
file, you could do:your "myInput.file" would be processed by the above operations, and saved. you could have that line in your shell script.
vimgolf is using the same way to save users solution.