Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How I can run vim commands from a bash script?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Antonio Recio  
View profile  
 More options Apr 24 2012, 7:42 am
From: Antonio Recio <amdx6...@gmail.com>
Date: Tue, 24 Apr 2012 04:42:01 -0700 (PDT)
Local: Tues, Apr 24 2012 7:42 am
Subject: How I can run vim commands from a bash script?

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Amit Agarwal  
View profile  
 More options Apr 24 2012, 7:43 am
From: Amit Agarwal <a...@amit-agarwal.com>
Date: Tue, 24 Apr 2012 17:13:26 +0530
Local: Tues, Apr 24 2012 7:43 am
Subject: Re: How I can run vim commands from a bash script?

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 :)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antonio Recio  
View profile  
 More options Apr 24 2012, 7:51 am
From: Antonio Recio <amdx6...@gmail.com>
Date: Tue, 24 Apr 2012 04:51:12 -0700 (PDT)
Local: Tues, Apr 24 2012 7:51 am
Subject: Re: How I can run vim commands from a bash script?

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Weber  
View profile  
 More options Apr 24 2012, 7:54 am
From: Marc Weber <marco-owe...@gmx.de>
Date: Tue, 24 Apr 2012 13:54:36 +0200
Local: Tues, Apr 24 2012 7:54 am
Subject: Re: How I can run vim commands from a bash script?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jürgen Krämer  
View profile  
 More options Apr 24 2012, 8:02 am
From: Jürgen Krämer <jottka...@googlemail.com>
Date: Tue, 24 Apr 2012 14:02:29 +0200
Local: Tues, Apr 24 2012 8:02 am
Subject: Re: How I can run vim commands from a bash script?

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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Beckett  
View profile  
 More options Apr 24 2012, 8:04 am
From: "John Beckett" <johnb.beck...@gmail.com>
Date: Tue, 24 Apr 2012 22:04:15 +1000
Local: Tues, Apr 24 2012 8:04 am
Subject: RE: How I can run vim commands from a bash script?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Amit Agarwal  
View profile  
 More options Apr 24 2012, 8:09 am
From: Amit Agarwal <a...@amit-agarwal.com>
Date: Tue, 24 Apr 2012 17:39:06 +0530
Local: Tues, Apr 24 2012 8:09 am
Subject: Re: How I can run vim commands from a bash script?

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Weber  
View profile  
 More options Apr 24 2012, 8:10 am
From: Marc Weber <marco-owe...@gmx.de>
Date: Tue, 24 Apr 2012 14:10:51 +0200
Local: Tues, Apr 24 2012 8:10 am
Subject: Re: How I can run vim commands from a bash script?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antonio Recio  
View profile  
 More options Apr 24 2012, 8:18 am
From: Antonio Recio <amdx6...@gmail.com>
Date: Tue, 24 Apr 2012 05:18:50 -0700 (PDT)
Local: Tues, Apr 24 2012 8:18 am
Subject: Re: How I can run vim commands from a bash script?

Using sed how I can replace this symbol ¦ with tab?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jürgen Krämer  
View profile  
 More options Apr 24 2012, 8:22 am
From: Jürgen Krämer <jottka...@googlemail.com>
Date: Tue, 24 Apr 2012 14:22:50 +0200
Local: Tues, Apr 24 2012 8:22 am
Subject: Re: How I can run vim commands from a bash script?

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.

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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Reid Thompson  
View profile  
 More options Apr 24 2012, 8:41 am
From: Reid Thompson <Reid.Thomp...@ateb.com>
Date: Tue, 24 Apr 2012 12:41:32 +0000
Local: Tues, Apr 24 2012 8:41 am
Subject: Re: How I can run vim commands from a bash script?

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'


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »