Operating System - Linux
1748256 Members
3722 Online
108760 Solutions
New Discussion юеВ

Re: Replacing All Instances of a pathname with another in Vi Editor

 
SOLVED
Go to solution
Andrew Kaplan
Super Advisor

Replacing All Instances of a pathname with another in Vi Editor

I have numerous instances of a pathname in a .cshrc file and would like to replace it with an alternate path. Initially I tried the following:

:1,$ s///g

But I would get either a NO PRECEDING EXPRESSION or a TRAILING CHARACTERS error

How can I do a blanket or all instances replacement within vi editor? Thanks.
A Journey In The Quest Of Knowledge
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Replacing All Instances of a pathname with another in Vi Editor

Try one of the following tricks:

1) Enclose your strings in single or double quotes.

2) Consider using sed to do the change, though you will still probably need to enclose your string in quotes.

Spaces are interpreted as separate statements by vi's change function or sed.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Stuart Browne
Honored Contributor
Solution

Re: Replacing All Instances of a pathname with another in Vi Editor

A space isn't considered a separate pattern in string replacement in vi or sed.

What your issue probably is, is that you aren't escaping the /'s in your path, i.e.:

:%s/\/path\/to\/replace/\/with\/new\/path/g

(Note: in 'vim', :%s is equivalent to :%1,$s).

Whilst it looks ugly, it's simpley escaping the /'s so they aren't interpreted as regex seperators.

The neater way is to use a different pattern separator, i.e.:

:%s#/path/to/replace#/with/new/path#g
One long-haired git at your service...
Andrew Kaplan
Super Advisor

Re: Replacing All Instances of a pathname with another in Vi Editor

Hi there --

I tried the using the single quote approach and got an EXTRA CHARACTERS AT THE END OF THE SUBSTITUTE COMMAND error. Here is the syntax that I used:

:1,$ s/'/scratch5/stella/'/'/usr/bali/robert/'/g

I'm not familar with the sed command and don't know the syntax to use there.

A Journey In The Quest Of Knowledge
Andrew Kaplan
Super Advisor

Re: Replacing All Instances of a pathname with another in Vi Editor

Hi there --

I tried using the expressions that employed the # symbol, and that worked without problem. Thanks again for the help.
A Journey In The Quest Of Knowledge