Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using subsitute (ie replace text) command on multiple files

2 views
Skip to first unread message

Alan

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
Go to the command prompt, and then to the directory containing
the files. do 'vim *.*' (w/o the quotes of course), then map a
key, say, z as ':map z :%s/hello/good by/g<C-v><C-m>:w<C-v><C-
m>:n<C-v><C-m>Z' along with the mapping ':map Z z'

Note :
1. No undo for this operation possible. Though a repeat of the
above commands with the reversal of the search patterns could be
done.
2. would work faster if ':syn off' is done.
3. <C-v> is ctrl+v.
4. <C-m> is ctrl+m.

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!


leon

unread,
Mar 14, 2000, 3:00:00 AM3/14/00
to
Hello

was just wondering - suppose i want to replace "hello" with "good by"
in ALL of the files contained in a certain directory.

is it possibel with vim?!

With best regards - Leon.

Ralf Arens

unread,
Mar 14, 2000, 3:00:00 AM3/14/00
to
leon <Le...@caresystems.com.au> wrote:

> was just wondering - suppose i want to replace "hello" with "good by"
> in ALL of the files contained in a certain directory.

> is it possibel with vim?!

Yes. Alan posted one solution, you can also use a function like this
one:

,----[ AllBuffers.vim ]
| " File: AllBuffers.vim
| "
| " Purpose: execute a command in all buffers
| "
| " Author: Ralf Arens
| " Last Modified: 2000-03-10 13:08:40 CET
|
|
| " AllBuffers(...)
| " executes a command in all buffers
| " attention: command has to be quoted,
| " e.g. :call AllBuffersDo("%s,foo,bar")
| fun! AllBuffers(cmnd)
| let cmnd = a:cmnd
| let i = 1
| while (i <= bufnr("$"))
| if bufexists(i)
| execute "buffer" i
| execute cmnd
| let i = i+1
| endif
| endwhile
| endfun
|
|
| " vim:set noet:ts=8:sw=8:sts=8
`----

To save all buffers use ":wqa".


Ciao,
Ralf

--
They speak English. Two, when they host a world championship,
they invite other countries. Three, visitors to the office of the
head of state are only expected to go down on one knee.
-- John Cleese on why the British are superior to Americans

leon

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Alan wrote:
>
> Go to the command prompt, and then to the directory containing
> the files. do 'vim *.*' (w/o the quotes of course), then map a
> key, say, z as ':map z :%s/hello/good by/g<C-v><C-m>:w<C-v><C-
> m>:n<C-v><C-m>Z' along with the mapping ':map Z z'
>

perhaps it is a bit off topic - but just seems rather compilcated -
would anyone know if there is a text utility (ala grep) except which can
not only find the needed text but also replace it?!

James Hu

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
On Wed, 15 Mar 2000 08:32:30 +1000, leon <Le...@caresystems.com.au> wrote:

>Alan wrote:
>> [create a map]

>perhaps it is a bit off topic - but just seems rather compilcated -
>would anyone know if there is a text utility (ala grep) except which can
>not only find the needed text but also replace it?!

How about ed?

$ (echo %s/old/new ; echo w ; echo q) | ed file1 file2 file3

will change all occurrences of old into new in files file1 file2 file3.

-- James

Thanh

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Search www.freshmeat.net for 'replace'.

Thanh

In article <esrla8...@ralf.dyndns.org>,


Sent via Deja.com http://www.deja.com/
Before you buy.

leon

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Thanh wrote:
>
> Search www.freshmeat.net for 'replace'.
>

yeah - but as far as i understand it is for Unix?
I had a dos thing in mind ;-)

Jürgen Krämer

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
leon wrote:
>
> perhaps it is a bit off topic - but just seems rather compilcated -
> would anyone know if there is a text utility (ala grep) except which
> can not only find the needed text but also replace it?!

perl -p -i -e 's/hello/good by/g' *

Regards,
Jürgen

--
Jürgen Krämer Softwareentwicklung/-support
Habel GmbH
Hinteres Öschle 2 Tel: (0 74 61) 93 53 15
78604 Rietheim-Weilheim Fax: (0 74 61) 93 53 99

Walter Prager

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
leon wrote:
>
> Alan wrote:
> >
> > Go to the command prompt, and then to the directory containing
> > the files. do 'vim *.*' (w/o the quotes of course), then map a
> > key, say, z as ':map z :%s/hello/good by/g<C-v><C-m>:w<C-v><C-
> > m>:n<C-v><C-m>Z' along with the mapping ':map Z z'
> >
>
> perhaps it is a bit off topic - but just seems rather compilcated -
> would anyone know if there is a text utility (ala grep) except which can
> not only find the needed text but also replace it?!

Try 'sed' (Stream EDit). The only problem is that it is a filter, meaning
that you cannot redirect its output into its input. You have to do changes in
two stages -- first filter the input file into a temp file, then replace the
input file with the temp file. For example:

foreach f(<list of files to change>)
cat $f | sed 's/foo/bar/g' > ${f}.temp
mv ${f}.temp $f
end
--
Walter Prager, Sr. S/W Designer| ||| "I don't have all the answers. In
TEL: (613) 599-3600 ext 6460 | |||||||| life, to be honest, I've failed as
EMAIL: wpr...@newbridge.com | ||||\||| much as I've succeeded. But I love
| ||||\\|| my wife, I love my life, and I wish
NEWBRIDGE NETWORKS CORP | ||||\\\| you *my* kind of success."
Kanata, Ontario, Canada | ||| Dicky Fox

Ralf Arens

unread,
Mar 15, 2000, 3:00:00 AM3/15/00
to
Thanh <tq...@my-deja.com> wrote:

> Search www.freshmeat.net for 'replace'.

Why?

> Thanh

> In article <esrla8...@ralf.dyndns.org>,
> ralf....@tu-clausthal.de (Ralf Arens) wrote:

[...]


Ciao,
Ralf

--
Science is the game we play with God to find out what His rules are.

Stefan Berglund

unread,
Mar 18, 2000, 3:00:00 AM3/18/00
to
On Wed, 15 Mar 2000 09:57:16 -0500, Walter Prager wrote:
> leon wrote:
> >
> > Alan wrote:
> > >
> > > Go to the command prompt, and then to the directory containing
> > > the files. do 'vim *.*' (w/o the quotes of course), then map a
> > > key, say, z as ':map z :%s/hello/good by/g<C-v><C-m>:w<C-v><C-
> > > m>:n<C-v><C-m>Z' along with the mapping ':map Z z'
> > >
> >
> > perhaps it is a bit off topic - but just seems rather compilcated -
> > would anyone know if there is a text utility (ala grep) except which can
> > not only find the needed text but also replace it?!
>
> Try 'sed' (Stream EDit). The only problem is that it is a filter, meaning
> that you cannot redirect its output into its input. You have to do changes in
> two stages -- first filter the input file into a temp file, then replace the
> input file with the temp file. For example:
>
> foreach f(<list of files to change>)
> cat $f | sed 's/foo/bar/g' > ${f}.temp
> mv ${f}.temp $f
> end

You can also use perl to get rid of the temp file:

for f in <files> ; do
perl -pi.bak -e 's/foo/bar/g' $f
done

this will do a inplace edit and create backup files with a .bak
extension for your old files.

--
/Stefan
sbl+...@dd.chalmers.se

Life - the ultimate practical joke

Mil

unread,
Mar 31, 2000, 3:00:00 AM3/31/00
to
Jurgen's worked great in Redhat!
I changed all the password's in my PHP files with it.
perl -p -i -e 's/password1/password2/g' *.inc
Thanks Jurgen,
your the shit!
0 new messages