17

Let us say I have a text like so below:

AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)

Now I want to replace the nn with numbers like so

AC(0)
AC(1)
AC(2)
AC(3)
AC(4)
AC(5)
AC(6)
AC(7)
AC(8)
AC(9)
AC(10)

I used M-x replace-regexp nn RET \# RET to accomplish this.

Questions:

  1. I want to start the replacement number to start from 1 rather than from 0. Or rather start from a specified number say 25. How should I modify the above command?
  2. How to replace nn with digits like 001, 002 .... 998, 999etc - I mean with leading zeros
1
  • This isn't what you're asking for, but another way of doing this is to use a macro along with a register. Commented Jan 5, 2018 at 7:50

2 Answers 2

22

General technique

Your replacement string can contain arbitrary lisp code. From the documentation for replace-regexp:

In interactive calls, the replacement text may contain ‘\,’ followed by a Lisp expression used as part of the replacement text. Inside of that expression, ‘\&’ is a string denoting the whole match, ‘\N’ a partial match, ‘#&’ and ‘#N’ the respective numeric values from ‘string-to-number’, and ‘#’ itself for ‘replace-count’, the number of replacements occurred so far, starting from zero.

We can use this technique in a number of ways.

Starting from 1

Wat we want to do is replace nn with one more than the replace-count you used with \#.

Call #'replace-regexp with the argument \,(1+ \#):

C-M-% nn \,(1+ \#) will replace nn with 1 first, then 2, 3, etc.

Starting at 25

You can modify this by not just adding one, but (in your example) 25:

C-M-% nn \,(+ 25 \#)

Leading zeros

Or we can use format to add leading zeros. This will replace nn with 000, 001, 002, etc. You can combine other lisp code above to start at 001, 025, or whatever you want.

C-M-% nn \,(format "$03d" \#)

4
  • This is fantastic! Do you know if it's possible to do the same with incremental letter replacements? That is a, b, and so on. I suppose using elisp to convert \# would do, but Emacs doesn't seem to have a built-in number-to-ascii-character conversion function...
    – pglpm
    Commented Oct 10, 2023 at 10:43
  • Found out. In case others would like to replace with letters: the replacement string/elisp should be \,(message "%c" (+ 97 \#)). With 97 we start at a, because that's "a"`s ascii number; check any ascii table if you want to start at some other letter.
    – pglpm
    Commented Oct 10, 2023 at 10:50
  • 1
    @pglpm Emacs Lisp supports character literals as ?a. So instead of (+ 97 \#), you can use (+ ?a \#). It's much more readable. Note that Emacs doesn't have a character datatype; it simply has integers that it treats in some cases as characters.
    – zck
    Commented Oct 10, 2023 at 15:56
  • Fantastic again, thank you so much!
    – pglpm
    Commented Oct 10, 2023 at 18:03
5

You can also use cua-mode.

Select the rectangle région (all the nn) and then M-x cua-rectangle-mark-mode.

Next, M-n and accept the default values.

1
  • I wish I could accept this as an answer as well
    – Prasanna
    Commented Jan 5, 2018 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.