It's not a rectangle-based answer, so if you're not tied to that, you could do this using an Emacs macro and the macro counter.
Inserting numbers
So record a macro:
C-x ( C-e <DEL> C-x C-k C-i C-n C-x e
And continue running it until you hit the end of the area you want to change by pressing e
.
Let me break down the macro definition, because it's confusing:
C-x (
-- start the macro recording
C-e <DEL>
-- go to the end of the line, and delete the 1
there.
C-x C-k C-i
-- call #'kmacro-insert-counter
, which will start the macro counter and insert its number. This gives you the state tracking of "which number gets inserted next?"
C-n
-- go to the next line.
C-x e
-- end recording the macro, and run it again. You can now repeat the macro just by pressing e
.
Inserting characters
If you want to insert characters instead, we need to set the format string the macro uses to insert the counter. We can do that with kmacro-set-format
, which is bound to C-x C-k C-f
. So tell it to print the integer as a character:
C-x C-k C-f %c <RET>
Note that the macro counter format string is global; it's shared for other macros. So if you want to make another macro that inserts numbers, you have to reset it. You can do that again, with C-x C-k C-f <RET>
. This resets the value to the default, which is %d
. But if we instead set the format string inside the definition of the macro, it won't affect other macros. So we're left with this as the macro definition:
C-x ( C-x C-k C-f %c <RET> C-e <DEL> C-x C-k C-i C-n C-x e
start | set the macro's | the macro body
macro | format string | as above
So now we'll get something that inserts characters, but they're not the right ones; we'll get characters like ^@ ^A ^B
and so forth, because those are the characters corresponding to the ASCII codes starting at 0. We need to start the macro's counter not at 0, but at the code for the character a
.
Now, that character happens to be 97, so let's set that using kmacro-set-counter
:
C-x C-k C-c 97 <RET>
And let's put the whole thing together. There's nothing new here, so instead of fully annotating it, I've just noted what the parts are doing. You can do them separately. Here it is:
C-x C-k C-c 97 <RET> C-x ( C-x C-k C-f %c <RET> C-e <DEL> C-x C-k C-i C-n C-x e
set the macro's | start | set the macro's | the macro body
counter value | macro | format string | as above