How do I execute a vim macro on the command line?
Macro in Vim is extremely useful to perform tasks that are difficult to perform in the usual command-line tool ( sed
, awk
, perl
etc.), is there a way to perform these types of macros on the command line?
Something like the following:
// execute macro stored in register a 100 times for filenames
vim execute -s "100@a" filenames [filenames2, filenames3, ...]
source to share
For those who have trouble executing a number of normal commands (such as macros) and exiting again, it is possible to define a macro first.
vim files* -c "let @l=\"ggOStart\<Esc>GoEnd\<Esc>\" | argdo normal @l | ZZ"
The command will insert "Start" at the beginning of each file, starting from files*
and "End" to the end, but any macro can be applied.
Thus, it is possible to define and execute a macro without its presence. You will have to use double quotes and escape them \"
if you want to use any of the key labels, such as the ones \<Esc>
mentioned here .
By appending ZZ
to the end, the file is written and closed again. Commands can be combined using |
symbolic notation.
source to share