🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.emacs
Article #2161

Re: regular expressions and operations other than replace

#2161
From: evp
Date: Sun, 19 Oct 1986 00:31
36 lines
2519 bytes
> Since I have switched from vi to EMACS, there is one thing that I missed
> more than anything else.  The ability to perform an operation on all
> the lines that met a particular criteria(specified by a regular expression).
> For instance in vi, I could type in "/[A-Z][a-z]*/d" to delete all lines
> that met the specified criteria or I could type in 
> "/\([A-Za-z][A-Za-z]*(\).*\()\)/s//\1\2".  How would I do similar operations
> in EMACS?

There are several answers to this question.  The easiest way to do
exactly what you want is to pipe the file through sed: set the mark at
the top, move to the bottom of the file, then use (in GNUmacs) the
"shell-command-on-region" command -- ^U ESC-|, passing the appropriate
agruments to sed.  The buffer will be replaced by the results of the sed.

However, this isn't really in the "spirit of EMACS".  When I have to do some
global operation, I usually use a keyboard macro.  Start the macro with ^X-(,
do a single instance of the operation, then ^X-) to terminate the macro
definition.  Repeat it a few times with ^X-e to make sure it works right,
then run it a bunch of times with ^U 999 ^X-e.  If you have a search at the
beginning of the macro, it will stop executing as soon as the search fails.

For more interesting problems, there's the "grep" command.  This allows you
do a search through many files for anything that grep can find. After
execution, calls to the "next-error" command (usually ^X-`) puts the cursor
on the next line containing the grep expression, in whatever file.  If
that line is one that needs to be changed, you can easily generate a
keyboard macro that does the operation then moves to the next instance.

Your last example can be performed almost verbatim with the GNUmacs 
"query-replace-regexp" command, which I bind to ESC-Q.

-- 
Ed Post   {hplabs,voder,pyramid}!lewey!evp
American Information Technology
10201 Torre Ave. Cupertino CA 95014
(408)252-8713
Technical Details
Message-ID: <403@lewey.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!husc6!mit-eddie!genrad!decvax!decwrl!nsc!voder!lewey!evp
References: <1168@peregrine.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!rutgers!husc6!mit-eddie!genrad!decvax!decwrl!nsc!voder!lewey!evp
From: evp@lewey.UUCP
Newsgroups: net.emacs
Subject: Re: regular expressions and operations other than replace
Message-ID: <403@lewey.UUCP>
Date: Sat, 18-Oct-86 20:31:26 EDT
Article-I.D.: lewey.403
Posted: Sat Oct 18 20:31:26 1986
Date-Received: Tue, 21-Oct-86 22:12:01 EDT
References: <1168@peregrine.UUCP>
Organization: American Information Technology, Cupertino, CA
Lines: 36