Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a bash script I want to edit a file. Is there a way to write a macro in vim, save it and then call it in bash script?

share|improve this question

No problem: Create the macro file with vi commands. For example:


Then run vi -s macro_file data_file

One advice: Try to use sed/awk and instead of vi for this purpose

share|improve this answer
And there is the equivalent but more obscure + syntax as in: vim +PluginInstall +qall for installing Vundle. – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Mar 28 '14 at 18:21

This is an example of the use of vim to create HTML versions of files with vim :

for f in *.c; do
    vim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" "$f"
share|improve this answer

You could use ex, which comes with vim and is the command line tool for vim.

Eran's answer is definitely correct, but I'd personally use ex -c (vim -c is also possible):

> echo asdf > blub
> ex file -c "normal a_" -c "normal l.l.l." -c wq
> cat blub

This is my preferred way, because there are not multiple files.

If you remap escape to jj (for example), you can easily make more complicated normal statements:

ex file -c "normal a_jjl.l.l." -c wq

For your information:

  • normal sends letters in normal mode to
  • wq quits the program
  • to send multiple commands in one string (no additional -c option), use the pipe character |. But: This does not work in the case of normal.
  • This works only for 10 -c's. But for more commands, I would recommend to create a file.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.