Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

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

:1,$s/a/b/g
:wq

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 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"
done
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
a_s_d_f_

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

 
discard

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.