#!/bin/sh
for i in *.txt; do vim ":%s/foo/bar/g"; done
How I can run vim commands from a bash script? What I am doing bad?
Now it opens each txt file but it doesn't replace or save them.
Give this a try:
sed -i 's/foo/bar/' *.txt
-i = write back file "in place"
vim foo will open file name 'foo'.
vim '%/s...' will open file name '%/s...' (and fail)
for x in ..
vim -c "e $x| %s/ ... | wq!"
done
is close to what you requested, but still no proper escaping for
filenames.
Marc Weber
is there a reason you used ":e $x" inside the parameter of the -c switch
instead of passing the filename as a separate parameter?
for x in ..
vim -c "%s/ ... | wq!" $x
done
Another approach would be to use Vim's :argdo command:
vim -c "set autowrite nomore" -c "argdo %s/.../.../" -c "q" *.txt
Regards,
J�rgen
--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)
Marc Weber
Marc Weber wrote:
> Excerpts from J�rgen Kr�mer's message of Tue Apr 24 14:02:29 +0200 2012:
>> vim -c "set autowrite nomore" -c "argdo %s/.../.../" -c "q" *.txt
> I haven't thought about it for long because the simple sed command does
> the job. So no, there was no specific reason because it doesn't make
> sense to me anyway because sed is shorter.
yes, but only if you have a GNU sed, because AFAIK -i is a GNU extension.