From: Peter Boettcher <boettcher@ll.mit.edu>
Subject: Re: Emacs regexp/incrementer question
Date: Thu, 16 Jan 2003 09:49:13 -0500 [thread overview]
Message-ID: <tr4r89176e.fsf@coyote.llan.ll.mit.edu> (raw)
In-Reply-To: b06466$gqq$1@s1.read.news.oleane.net
"Tim Morley \(remove vegetable for email address\)"
<tim@teamlog.turnip.com> writes:
> Hi all.
>
> Does anybody know of a way to include an incrementing number in a
> regexp search-and-replace in Emacs? What I want to do is search for
> a regexp, and the first time I find it, replace it with "case 1",
> the second time replace it with "case 2", etc.
>
> Even if the solution is a several-stager, I'd be more than happy,
> because I'm currently having to replace each occurrence with "case
> xxx" and then change each "xxx" to a number by hand afterwards --
> hardly ideal. I could use awk or something to change the xxx's to
> incrementing numbers, but I'd much prefer to do it all in Emacs if I
> can.
>
> Thanks in advance for your help.
I have a few counter functions bound to keys to use with keyboard
macros.
(defvar macro-counter 0)
(defun macro-counter-reset nil
(interactive)
(setq macro-counter 0))
(defun macro-counter-insert nil
(interactive)
(insert (number-to-string macro-counter)))
(defun macro-counter-increment-and-insert nil
(interactive)
(setq macro-counter (+ macro-counter 1))
(insert (number-to-string macro-counter)))
(global-set-key "\C-c=" 'macro-counter-insert)
(global-set-key "\C-c-" 'macro-counter-reset)
(global-set-key "\C-c+" 'macro-counter-increment-and-insert)
So then whenever I need to generate some code with increasing
numbers, I can do hacks like:
C-x ( ; start recording macro
C-s foo RET ; look for foo
M-d ; delete foo
c a s e SPC ; just type the constant part
C-c + ; increment and insert counter
C-n C-a ; next line, beginning of line
C-x ) ; end macro
Then
C-x e ; run macro once to be sure it does what I want
C-u C-u C-x e ; run it a bunch of times (16)
--
Peter Boettcher
MIT Lincoln Laboratory
boettcher@ll.mit.edu
prev parent reply other threads:[~2003-01-16 14:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-16 11:15 Emacs regexp/incrementer question Tim Morley (remove vegetable for email address)
2003-01-16 12:17 ` Friedrich Dominicus
2003-01-17 9:01 ` Tim Morley (remove vegetable for email address)
2003-01-17 12:59 ` Friedrich Dominicus
2003-01-17 13:44 ` Christopher J. White
2003-01-17 14:12 ` Tim Morley (remove vegetable for email address)
2003-01-16 14:49 ` Peter Boettcher [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tr4r89176e.fsf@coyote.llan.ll.mit.edu \
--to=boettcher@ll.mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).