[ale] vim search/replace syntax request

Byron Jeff byronjeff at mail.clayton.edu
Thu Mar 10 07:28:40 EST 2011


On Thu, Mar 10, 2011 at 02:04:15AM -0500, Dow Hurst wrote:

> I'd like to construct a vim based search and replace statement.  I want
> to search between a set of line numbers for the 85th character on a line
> and if it contains a "1" then turn it into a "0".  I just don't know how
> to set up that particular search pattern and replace pattern.

The following should work in vim. Not sure about raw vi:

:%s/^\(.\{84\}\)1\(.*\)$/\10\2/

Here is an expanded version with some backslashes eliminated for explanation:

:%s/^ ( .{84} ) 1 ( .* ) $ / \1 0 \2 /

The :%s/^ is the standard substitute all lines and mark the beginning of the line.

The parens in the expression are grouping operators. Needs backslashes to
mark as special operators. Each group captures and saves the match so that
it can be reinserted in the substitution. The groups are numbered starting
with 1.

So the first group captures .{84} which matches exactly 84 charaters.

The 1 matches the 1 in the 85th column.

The .* is the second group which captures the rest of the line after the
85th character. Probably could get away without it (*). The $ is the end of the
line.

Once a line is matched then the substitution is pretty simple:

The \1 is the first group which is the first 84 characters.
the 0 substitutes for the 1.
the \2 is the group for the rest of the line.
 
(*) This should also work:

:%s/^\(.\{84\}\)1/\10/

Good Luck.

BAJ

> 
> It's been a long time since I've posted to the list.  It is nice to see the ALE group is flourishing.  I miss going to the meetings...
> 
> Best wishes,
> Dow
> ________________________________________________
> Dow Hurst, Research Scientist
> 340 Sullivan Science Bldg., Dept. of Chem. and Biochem.
> University of North Carolina at Greensboro
> PO Box 26170 Greensboro, NC 27402-6170
> 
> 

Content-Description: ATT00001..txt
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo


-- 
Byron A. Jeff
Department Chair: IT/CS/CNET
College of Information and Mathematical Sciences
Clayton State University
http://cims.clayton.edu/bjeff


More information about the Ale mailing list