[Cialug] vim global replace

John Lengeling John.Lengeling at radisys.com
Fri Sep 18 12:06:18 CDT 2009


% isn't vim-specific.  I have been using these since BSD 2.8/4.0.   You
can use the following for specifying line number ranges in vi/vim:

1 - first line
$ - last line
. - current line
N,M - Range: Line N through M
N+n - Relative: Line N + n lines
N-n - Relative: Line N - n lines
% - All lines

Their may be more... The man pages for vi/vim have the complete list.

-----Original Message-----
From: cialug-bounces at cialug.org [mailto:cialug-bounces at cialug.org] On
Behalf Of Barry Von Ahsen
Sent: Friday, September 18, 2009 11:01 AM
To: Central Iowa Linux Users Group
Subject: Re: [Cialug] vim global replace

I think % might be vim-specific

just for completeness, if you hit a standard vi, or you don't want to 
replace the whole file, you can do

:.,$ s/^/'/g

the .,$ being the range from this line (.) to EOF ($)

I also like /gic - global, insensitive, confirm - the confirm is nice, 
because I never trust my own regex-es (I can confirm 3 or 4, then 
replace all of the rest), or if you have 3 or 4 close matches that you 
don't want to replace, you can skip those, instead of having to 
super-size your regex to exclude those outliers


-barry




Jeff Chapin wrote:
> Try :%s/^/'/g
> 
> 
> I think the % is important to ed.
> 
> 
> Todd Walton wrote:
>> I'm using vim in cygwin.
>>
>> $ vi --version tells me:
>> VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Oct  9 2008 15:23:22)
>>
>> I've opened a text file and ran:
>>
>> :s/^/'/g
>>
>> It only puts a ' on the first line and then stops.  Pushing n tells
me
>> "E35: No previous regular expression".  Pushing & puts another ' on
>> the first line.
>>
>> Anybody know what gives?  Ultimately, my goal is to put a pair of '
>> around the contents of each line.  It's a list of serial numbers.
>>
>> --
>> Todd
>> _______________________________________________
>> Cialug mailing list
>> Cialug at cialug.org
>> http://cialug.org/mailman/listinfo/cialug
>>   
> 
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug

_______________________________________________
Cialug mailing list
Cialug at cialug.org
http://cialug.org/mailman/listinfo/cialug


More information about the Cialug mailing list