Live data from Hacker News

Learn Emacs: Keyboard Macros

rawsyntax.com

11–20 of 27 posts

Re: Learn Emacs: Keyboard Macros

#11
post #2

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…

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.

Re: Learn Emacs: Keyboard Macros

#12
post #4
post #2

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…

You should check out the EmacsWiki macro tricks page: 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)

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...)

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))

Re: Learn Emacs: Keyboard Macros

#14

disclaimer: I'm the author Any suggestions for other emacs topics I should blog about? Thanks for your interest.

Something on code navigation & smart code completion.

I am going to echo a reply elsewhere on this thread and say you should contribute to the emacswiki if you can as well.

Re: Learn Emacs: Keyboard Macros

#15

disclaimer: I'm the author Any suggestions for other emacs topics I should blog about? Thanks for your interest.

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.

Re: Learn Emacs: Keyboard Macros

#16
post #2

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…

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].

[1] http://www.gnu.org/s/libtool/manual/emacs/Keyboard-Macro-Cou...

[2]http://stackoverflow.com/questions/1509688/emacs-macro-to-ge...

Re: Learn Emacs: Keyboard Macros

#17

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 do…

[deleted]

Re: Learn Emacs: Keyboard Macros

#18
post #2

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…

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
https://gist.github.com/1355548

Re: Learn Emacs: Keyboard Macros

#19
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.

Re: Learn Emacs: Keyboard Macros

#20
post #2

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…

The newest emacs keybindings should help sort this out. Use to make it easy. Might as well just go C-h k 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:

abcdefghijklm C-a C-SPC C-e C-w #define SPC C-y M-b C-f C-SPC C-e C-w SPC RET

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'.)

Post reply on HN