[Linux-disciples] vim: selecting lines matching a pattern

Stephen R Laniel steve at laniels.org
Thu Dec 22 15:38:35 EST 2005


On Thu, Dec 22, 2005 at 03:28:47PM -0500, Adam Rosi-Kessel wrote:
> It's certainly do-able in vim. It would just be a sort of hack, at least the
> way I would do it. Something like:
> 
> :map __1 /<p><cr>V/<\/p><cr>Jj__1
> :map _1 gg_1
> 
> This would cause _1 to map to the action of doing what you desire.

Here's how I did it in Perl, just now:

	#!/usr/bin/perl
	use strict;
	use warnings;
	
	my $start_re = qr/<p>/;
	my $end_re = qr{</p>};
	
	my $inText = '';
	while(<>) {
		if( m/$start_re/..m/$end_re/ ) {	# if we're in one of the matching blocks ...
			$_ =~ s/\n//g;
			$inText .= $_;
		}
		else {
			$inText .= $_;
		}
	}
	
	print $inText;

One obvious way to improve it would be to accept two regular
expressions as start_re and end_re. Then you could join
arbitrary bits.

-- 
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20051222/9d5bb250/attachment.pgp


More information about the Linux-disciples mailing list