There have been various times in my programming career that I've wanted an emacs macro with arguments, so that it would do things like concatenate a number in each line. Things like this in C:
#define a 0
#define b 1
...
#define l 10
#define m 11
Usually I take that as a sign that I need to write some other program to generate the lines of the C program, but for some tasks that's overkill and I want an automated way to create a list of sequential things so I don't have to type 1 down 1 down 1 down 1 down 2 down 2 down 2 down...
If you want to produce this list:
test1
test2
test3
...
Do like this:
C-x r n q (store the number 1 in register q)
C-x ( (start macro recording)
test C-x r i q (insert contents of register q)
C-x r + q (increment register q by 1)
RET (new line)
C-x ) (stop macro recording)
The easy way I do it is to make however many copies of "test0" I want, then make a trivial throwaway macro where I move my cursor to the number I want to increment, and hit M-x rolling-increment-number-at-point. (Of course with autocomplete...)
I don't think it's quite what you're talking about, but I have an Emacs function that runs the current region through Python and replaces it with the stdout from running it.
For your example, I write this:
vars = list('abcdefghijklm')
for v,i in zip(vars, range(len(vars))):
print("#define {} {}".format(v, i))
Select it and press Ctrl+Alt+p and have it replaced by:
#define a 0
#define b 1
#define c 2
#define d 3
#define e 4
#define f 5
#define g 6
#define h 7
#define i 8
#define j 9
#define k 10
#define l 11
#define m 12
Yeah, I considered that, but the situation comes up so infrequently I'd never remember the syntax. I use Python everyday anyway, so it almost comes naturally.
I know exactly what you mean -- but perhaps it doesn't even have to be a macro, but a more powerful search-and-replace, with changing replacements.
My lisp-fu isn't good enough to implement this today, but I would find it really easy to:
* copy `#define a 0` with C-k, M-w, C-y, RET, C-y, RET, C-y, RET, ... (so copy the line and paste N times)
* begin interactive search-and-replace, with usual keybindings (Y for replace, N for skip, . for replace and stop)
* but with additional keybinding, e.g. + for change replacement and apply
Alternatively, the replace argument in M-% would be a [somehow-delimited] list of replacements, and + or capital Y would pop off the next replacement and begin using that.
There is a keyboard macro counter, which I've been able to use in situations like this. It's described in the manual[1] and on this Stackoverflow question[2].
The newest emacs keybindings should help sort this out. Use <F3> to make it easy. Might as well just go C-h k <F3> to see what it does, but the function name `kmacro-start-macro-or-insert-counter' might give you a clue.
Here's what I'd do for your example, starting in an empty buffer:
Then press F4 until you reach `m'. (You may find that the sequence ends with `m' being 12.)
(NOTE you should totally use `enum' for this; your debugger may on occasion permit you to use the names when you are entering an expression, and you'll get sensible error messages in the event of a conflict. Unlikely in this case I suppose since nobody uses variables called `i' or `j'.)
In the past, I've used 2 buffers to solve this problem.
I'll fill one buffer with the text I'm editing, and the other buffer with any 'arguments'. Then I'll just write the macro to switch between buffers and grab arguments as necessary.
I don’t know Emacs, but I know a way to do that in Vim, and Emacs might support a similar method. In Vim, Ctrl-A and Ctrl-X increment and decrement the number the cursor is over. You can generate a list by recording a macro that duplicates the current line and then increments its number (qqVyp ^a q), and running that macro repeatedly (10@q). Emacs might also have a function for incrementing characters, not just numbers.
I find that copy rectangle works wonders for lots of problems like this. It's simple to type
1
2
3
4
5
6
7
8
9
0
and then using copy rectangle I can quickly assemble long lists of numbers.
I do use keyboard macros quite frequently too, but mostly for more complex operations (e.g. start of line, search for char, set point, search for next char, kill-ring-save, move to end, set point, yank, uppercase region)
Long-time Emacs user here. (When I search for a string, read, send or reply to an email, or set a timer to tell me when my eggs are ready, I use an Emacs command I wrote.)
I just want to say that if you plan to learn how to write commands in Emacs Lisp, you do not need to learn how to define or use keyboard macros. In my experience, it is always easier just to write some lisp.
Ditto. I've had emacs in my fingertips for 17 years now, but virtually never use macros. Repetitive editing stuff in my world is generally easier in perl.
Or more generally: repetitive structure in any text file (especially a program) is generally a sign of bad design, and should be factored out in whatever way is appropriate.
On the tangent, this may be the 10th article of this kind i've seen, the 1st time it was nice to discover macro shortcuts, now I feel something is wrong.
Emacs documentation is large and well written, yet we discover features with google, it's not even centralized, let's avoid duplication of efforts and reinforce emacswiki. Maybe integrating emacswiki with the standard emacs distribution in some way ? (if it's not done already.. which would render this little rant finally funny)
I'm sure you know this already, but Emacs actually contains its manual in a convenient form: C-h r opens it. There are other help commands that provide plenty of information as well.
The issue isn't a lack of information built-in--Emacs is self-documenting, after all--but that its difficult to prioritize features. The real value in blog posts like this is when they describe features the author really likes coupled with his actual use cases for it.
Ultimately, the author is just a curator in a special Emacs exhibit--he brings out the real gems (from his point of view) and gives us some background on them. I think something like that could be integrated into Emacs--but should it?--by having a "tip of the week" or something like that.
I have a suggestion for this specific article. I like that it's short and sweet, but I think it could have used a mention of the C-x e e e e e e pattern. I find myself using this all the time to do medium difficulty refactoring on a section of code. Rarely, if ever, do I use C-u 0 C-x e.
Surely this is a solved problem.