Live data from Hacker News

Learn Emacs: Keyboard Macros

rawsyntax.com

1–10 of 27 posts

Re: Learn Emacs: Keyboard Macros

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

Re: Learn Emacs: Keyboard Macros

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

Re: Learn Emacs: Keyboard Macros

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

Re: Learn Emacs: Keyboard Macros

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

Ow, that hurts my brain more than usual. How would one leverage registers with text instead of incremental numbers? (I will have to RTFW later.)

Re: Learn Emacs: Keyboard Macros

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

Re: Learn Emacs: Keyboard Macros

#7
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 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 repla…

[deleted]

Re: Learn Emacs: Keyboard Macros

#8
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 the \# character, which is replaced with the number of times a replacement has been done. So,

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

Re: Learn Emacs: Keyboard Macros

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

Post reply on HN