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?
No problem: Create the macro file with vi commands. For example:
vi -s macro_file data_file
One advice: Try to use
awk and instead of
vi for this purpose
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
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 blub -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.