How I can run vim commands from a bash script?

1,202 views
Skip to first unread message

Antonio Recio

unread,
Apr 24, 2012, 7:42:01 AM4/24/12
to vim...@googlegroups.com
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?

Amit Agarwal

unread,
Apr 24, 2012, 7:43:26 AM4/24/12
to vim...@googlegroups.com
One obvious thing I can see is the missing filename in the vim
command :)

Antonio Recio

unread,
Apr 24, 2012, 7:51:12 AM4/24/12
to vim...@googlegroups.com
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.

Marc Weber

unread,
Apr 24, 2012, 7:54:36 AM4/24/12
to vim_use
vim was not designed for that kind of task.
You can pass commands via -c and --cmd (see --help).

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

Jürgen Krämer

unread,
Apr 24, 2012, 8:02:29 AM4/24/12
to vim...@googlegroups.com

Hi,

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)

John Beckett

unread,
Apr 24, 2012, 8:04:15 AM4/24/12
to vim...@googlegroups.com
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.

See the following for the Vim procedure:
http://vim.wikia.com/wiki/Search_and_replace_in_multiple_buffers

John

Amit Agarwal

unread,
Apr 24, 2012, 8:09:06 AM4/24/12
to vim...@googlegroups.com
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.

Marc Weber

unread,
Apr 24, 2012, 8:10:51 AM4/24/12
to vim_use
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.

Marc Weber

Antonio Recio

unread,
Apr 24, 2012, 8:18:50 AM4/24/12
to vim...@googlegroups.com
Using sed how I can replace this symbol ¦ with tab?

sed -i 's/¦/\t/' *.txt

Jürgen Krämer

unread,
Apr 24, 2012, 8:22:50 AM4/24/12
to vim...@googlegroups.com

Hi,

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.

Reid Thompson

unread,
Apr 24, 2012, 8:41:32 AM4/24/12
to vim...@googlegroups.com, Reid Thompson
On Tue, 2012-04-24 at 05:18 -0700, Antonio Recio wrote:
> Using sed how I can replace this symbol ¦ with tab?
>
> sed -i 's/¦/\t/' *.txt
>

sed -i 's/|/\t/g'

or

perl -p -i.bak -e 's/\|/\t/g'


Reply all
Reply to author
Forward
0 new messages