9

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, ...]
1
  • vim -s runs a script from the command line. Getting it right can be tricky though. Commented Aug 8, 2015 at 17:26

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You are almost there:

$ vim file* -c "argdo norm 100@a"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.