R. Peek
R. Peek

Reputation: 71

running vim from a windows batch file; quoting quotation marks therein; inserting text

To do repetitive edits, like replacing a string with another in many files, I've been using running vim from windows batch files (help on stack overflow to do that was very useful!). My follow up questions are how to handle it if:

For instance in regard to (a) I'd like to do:

call vim fn.txt +"1,$s/$/\"/" +wq

To add a quotation mark at the end of every line in file fn.txt, but of course this will not work because windows does not know that the 2nd quotation mark int the line above is quoted.

As an example of (b), how do I insert a line containing the text "inserted line" after line number 7 in file fn.txt

call vim fn.txt +:7 +oinserted line????

where the question marks above indicate where I don't know how to continue.

Upvotes: 0

Views: 87

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

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. However, as you've found out, the escaping on the Windows command-line is tricky. The escape character should be ^ (so " becomes ^"), but I also couldn't get it to work in some few quick tests. Just read the documentation for the /S switch on cmd.exe /? and try to understand it :-( Because of that, it's better to put the Vim commands into a separate script, as pass only the script's filespec to Vim. The following details all use that trick.

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.

REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"

Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.

# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"

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:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE        Do not load vimrc and plugins, alternatively:
--noplugin        Do not load plugins.
-n                No swapfile.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                user's settings).
-es               Ex mode + silent batch mode -s-ex
                Attention: Must be given in that order!
-S ...            Source script.
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                with messages or output to avoid blocking.

Upvotes: 2

Related Questions