On Tue, 2012-04-24 at 04:42 -0700, Antonio Recio wrote:
> I am trying to write an script to execute a vim command to multiples txt files and to overwrite these txt with the result. Something like that.
> #!/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?
One obvious thing I can see is the missing filename in the vim
command :)
Antonio Recio wrote:
> I am trying to write an script to execute a vim command to
> multiples txt files and to overwrite these txt with the
> result. Something like that.
> #!/bin/sh
> for i in *.txt; do vim ":%s/foo/bar/g"; done
As others have mentioned, that is not going to work well.
On Tue, 2012-04-24 at 04:51 -0700, Antonio Recio wrote:
> Oups, you are right I have forgotten the filename:
> for i in *.txt; do vim $i "%s/foo/bar/g"; done
> Now it opens each txt file but it doesn't replace or save them.
How about trying to save and quit as well in the command. Although not
the preferred way but something like this should work :
vim $i -c "s/foo/bar/|w|q"
or
vim $i -c "silent! s/foo/bar/|w|q"
If you simply want to replace pattern in files, why not use sed.
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.
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.
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)