Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a vim macro string in command line?

Macro in Vim is extremely useful to perform tasks that is difficult to finish in normal command line tool (sed, awk, perl, etc.), is there any way to perform that kinds of macro string in command line?

Something like the following:

// execute macro stored in register a 100 times for filenames
vim execute -s "100@a" filenames [filenames2, filenames3, ...]
like image 287
alexzzp Avatar asked Aug 08 '15 17:08

alexzzp


2 Answers

For those having trouble to execute a number of normal commands (like a macro) and exiting again, it is possible to first define the macro.

vim files* -c "let @l=\"ggOStart\<Esc>GoEnd\<Esc>\" | argdo normal @l | ZZ"

The command will insert "Start" to the beginning of each file starting with files* and "End" to the end, but an arbitrary macro might be applied.

That way it's possible to define and execute a macro without it being already present. You will have to use and escape double quotes \" if you want to be using any key-notation such as \<Esc> mentioned here.

By adding ZZ to the end, the file is written and closed again. Commands can be concatenated by a | symbol.

like image 118
tfachmann Avatar answered Oct 23 '22 04:10

tfachmann


You are almost there:

$ vim file* -c "argdo norm 100@a"
like image 37
romainl Avatar answered Oct 23 '22 05:10

romainl