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)
Surely this is a solved problem.