Live data from Hacker News

Learn Emacs: Keyboard Macros

rawsyntax.com

21–27 of 27 posts

Re: Learn Emacs: Keyboard Macros

#21
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 #defi…

Or just learn a little bit of Elisp, and do this:

    M-: (loop for i from ?a 
              for j below 12 
              do (insert (format "#define %c %d\n" i j)))

Re: Learn Emacs: Keyboard Macros

#22

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.

Good idea. I'll be digging into this topic, and making a post on it soon

Re: Learn Emacs: Keyboard Macros

#23
post #21

Earlier quoted context omitted.

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

Or just learn a little bit of Elisp, and do this: M-: (loop for i from ?a for j below 12 do (insert (format "#define %c %d\n" i j)))

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.

Re: Learn Emacs: Keyboard Macros

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

Re: Learn Emacs: Keyboard Macros

#25

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.

Re: Learn Emacs: Keyboard Macros

#26

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…

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.

Re: Learn Emacs: Keyboard Macros

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

Emacs has this feature [for all values of 'this feature' :)]

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

Post reply on HN