#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...Surely this is a solved problem.
M-x replace-regexp
Replace regexp: test\(.+\)
Replace regexp with test\#
You can actually drop arbitrary lisp in the middle of search/replace expressions. So, if you'd like to number from 1 instead of 0, M-x replace-regexp
Replace regexp: test\(.+\)
Replace regexp with test\,(1+ \#\)
I'm sure with more lisp tricks, you can get delimited lists: M-x replace-regexp
Replace regexp: test\(.+\)
Replace regexp with test\,(aref ["one", "two", "three"] (mod \# 3))
(Not sure that works, but you get the idea).Anyway, all of this was basically stolen from Steve Yegge. You should totally go read his article if you're interested: http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-...
http://www.emacswiki.org/emacs/KeyboardMacrosTricks
From there:
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)Here's my definitions:
(defvar *rolling-counter* 0)
(defun rolling-increment-number-at-point ()
(interactive)
(incf *rolling-counter*)
(skip-chars-backward "0123456789")
(or (looking-at "[0123456789]+")
(error "No number at point"))
(replace-match (number-to-string (+ *rolling-counter*
(string-to-number (match-string 0))))))
(defun clear-rolling-history ()
(interactive)
(setf *rolling-counter* 0))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
https://gist.github.com/1355548 M-: (loop for i from ?a
for j below 12
do (insert (format "#define %c %d\n" i j)))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.
[1] http://www.gnu.org/s/libtool/manual/emacs/Keyboard-Macro-Cou...
[2]http://stackoverflow.com/questions/1509688/emacs-macro-to-ge...
Here's what I'd do for your example, starting in an empty buffer:
abcdefghijklm C-a C-SPC C-e C-w <F3> #define SPC C-y M-b C-f C-SPC C-e C-w SPC <F3> RET <F4>
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'.)
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.
The documentation for the most convenient way to do it (there are others) is at:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Key...
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)
Any suggestions for other emacs topics I should blog about?
Thanks for your interest.
I am going to echo a reply elsewhere on this thread and say you should contribute to the emacswiki if you can as well.
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.
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.
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)
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.
Then I quickly remember I use the macro feature constantly and back I go to Emacs.