I have a few files that I have an annoyingly repetitive group of VIM commands to apply to.
They are basic VIM commands just to delete some lines which are not supported by the process I need the files for.
Say I have a file called postgresdb.out
On this file I will
vim ./postgresdb.out
Then from inside VIM I will
:%s/old/new/g
and then
:g/deletethis/d
followed by
:%s/\n\n//g
then save the file and go about my day
I would like to know if it is possible to write a bash script that can do this process instead of retyping all the time
Best Answer
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: