reddit / emacs
Keyboard Macros are Misunderstood
mickeyp · 2022-07-12 15:10
mickeyp · 2022-07-12 15:13
It's all too easy to discount keyboard macros as a blunt instrument of text editing. It's a really effective tool for automating diverse tasks, like window configuration, as that's easily one of the more frustrating things for new users. Ditto combining disparate activities into a single 'action'.
So I'd love to hear from people who use keyboard macros for more than "basic" text editing.
00-11 · 2022-07-12 15:43
Mickey rightfully used the best terms for those new to Emacs: ***record*** and ***play back***. Everyone understands such behavior, and how, when, and why it's useful.
And it's important to realize that this is about pretty much *all* user interactions, not just *"keyboard"* use.
The name *"keyboard macro"* is fine from an Emacs point of view (even mouse actions are handled in Emacs with "key bindings" in "keymaps"). But for discovery and introduction, Emacs should put **record** and **play back** front & center.
I can't emphasize this enough, I think. Imagine if the other record-and-play-back experiences in your life had names like "keyboard macro"? Ridiculous, right?
mickeyp · 2022-07-12 15:54
Yeah, the nomenclature's not great. But I suppose it's just human nature to underestimate the power of recording user actions. Most implementations suck, so it's easy to see why people don't bother exploring.
The only other macro recorder I can think of that doesn't suck is Microsoft Excel's macro recorder. It's pretty amazing, and it generates quite sane VBA that people can then edit and build on for their own scripts.
LordOfSwines · 2022-07-12 16:08
Something I do quite often when writing a elisp macro is record a kb macro to jump to some place where I'm using the macro and macroexpand it and then have another macro to undo that expansion and jump back to where I'm defining said macro. Saves me from having to do that manually on every iteration of the macro.
columbcille · 2022-07-12 16:41
I’m a newb, and just discovered macros. I use one to layout my windows for my morning review—basically to open my org agenda to a certain view and open up the day’s journal. It’s a simple macro, but has opened my eyes to a whole new dimension of things.
[deleted] · 2022-07-12 16:52
[deleted]
Imaltont · 2022-07-12 16:55
I would say vim's macros are pretty nice as well. You record them to the wanted register, shared with the "clipboard". It persists between sessions. Since it shares the registers with the clipboard, you can also paste in the macro or copy it from somewhere, including special characters for F-keys, escape etc.
mickeyp · 2022-07-12 16:59
You may want to give `pp-macroexpand-last-sexp` a try for stuff like this.
[deleted] · 2022-07-12 17:17
[deleted]
timmymayes · 2022-07-12 17:31
The bit where you can insert the elisp from a macro is fantastic. Love your book btw!
LordOfSwines · 2022-07-12 17:41
Yeah that's pretty useful :) Still need to navigate to the expression and do the expansion though, still very handy.
F4il3d · 2022-07-12 17:55
I usually write elisp code based on strategic significance. The task that the macro is intended to address is of a regular nature so I will use the function again. I used keyboard macros for "one-off" repetitive tasks. They are especially useful when using multiple buffers and when used in conjunction with register commands. There is a dazzling amount of complexity you can achieve. I like and have used keyboard macros for about 30 years.
hmelman · 2022-07-12 18:07
I’ll setup a (typically dried) buffer with a specific list of files I want to make bulk changes to and then use a macro to: (1) open the file under point in another window (2) switch to that window (3) make some change (4) save and kill the buffer (5) return to my file list (6) move to the next line. Depending on which files I’m working on or what the changes are, it is much more interactive than writing a one-off shell script.
agumonkey · 2022-07-12 19:22
I still want to make a macro to elisp thingy, with variable inference and other things
7890yuiop · 2022-07-12 19:58
The day I realised keyboard macros were awesome was the day when I was looking at a buffer of grep results which effectively formed a to-do list for some non-trivial-but-similar changes in each of those files (not just at the line in the grep results), and thought to myself "I wonder if..." and the answer was "yes; yes you can".
More generally, using some collection of data in one buffer to process things elsewhere in other buffers, is really powerful.
I also think keyboard macros really highlight the advantages of a keyboard-centric everything-is-text application.
arthurno1 · 2022-07-12 23:50
There is a 3D animation anf fx package called Maya, by former Alias|Wawefront and now Autodesk. I think they have even nicer alternative to macro recorder. They use a sxripting language based on TCL but with some extras tossed in which they call MEL, which is used pretty similar to Lisp in Emacs. Everything is scriptable similar as in Emacs, and every action is a command, similar to interactive functions in elisp. What they do is to echo/record every command to a built-in (simple) text editor. "Recording" is always on so to say. One can then just select with mouse and grab a bunch of commands from the editor and drag the text selection into a "shelf" (toolbar) and it automatically becomes a clickable button ready to be used. It is not as same as start/stop recording paradigm, but it is maybe even simpler as paradigm.
eadmund · 2022-07-13 02:41
You’re not wrong, _but_ it does make sense from the general Lisp perspective of having reader macros, compiler macros, normal macros &c.
whompyjaw · 2022-07-13 13:47
Can you elaborate on how you do it? I have not messed with macros really yet.
whompyjaw · 2022-07-13 13:49
May I ask exactly what you did? I’m not too knowledgeable in macros yet.
whompyjaw · 2022-07-13 14:13
I am wondering if someone could give me some direction for creating 2 macros for 2 things I like to do while coding:
1. Insert a TODO commented out with a time stamp in my code. I tried to create a function, but I’m not good at that either (yet)
Syntax would be: “# TODO <time> (user) text”
I know org-mode can insert time stamps, but can prog-modes do that?
2. Insert import pdb set trace at cursor
“import pdb; pdb.set_trace()”
Bonus: then maybe remove all set_traces in my buffer (or all buffers).
The second one seems pretty easy, so I might take a crack at that later today :)
Thanks for the post Mickey! I’m about 1/3rd or halfway through your book and it’s been great. Just enough explanation and action :)
columbcille · 2022-07-13 15:16
I recorded the macro (just a few keystrokes to open the journal and open an agenda view), then inserted the macro code into my .emacs file with a name that I can invoke with M-x.
I’m away from the computer now but will share a good website or two that helped walk me through this.
mickeyp · 2022-07-13 15:54
1. You can use `org-time-stamp` just fine in other buffers. So I'd record and write out the text (without a comment symbol) on an empty line, and then invoke `C-x C-;` and stop recording. Emacs will pick the right comment symbol for your major mode this way.
2. That's just a case of recording some text. I recommend you add something at the end to indicate it's a 'special' line, like `MYDEBUG` or something. Then you can record another macro that uses `C-s` to jump to every line that matches `MYDEBUG` and delete the line.
whompyjaw · 2022-07-13 16:54
Great thanks so much!
00-11 · 2022-07-13 17:26
Yes; as I said:
> The name "keyboard macro" is fine from an Emacs point of view
whompyjaw · 2022-07-13 17:36
1. Works like a charm ty!
2. Like, “MYDEBUG import pdb; pdb.set_trace()”? I think python interpreter would miss that. But I think I could regex search for pdb or even just “pdb” but you’re thinking in the future for all breakpoints?
mickeyp · 2022-07-13 18:00
2. No, as a comment after it. You can search for pdb also of course.
7890yuiop · 2022-07-13 23:56
Well the grep results allow you jump to the buffer and position for that result, so it's a matter of recording a macro which jumps to the next grep result and then performs the edits in that buffer in such a way that the same recording will perform the equivalent edits for the other results as well. Because switching buffers (and indeed pretty much everything else in Emacs) uses a key sequence, your macro recordings can move around between buffers as easily as anything else.
One of the keys to editing text with macros is to use editing commands which will be agnostic to the *differences* between the various different pieces of text you'll be replaying the macro against. Searching for specific things tends to be a good way to navigate. Moving/killing by sexp rather than word tends to be good. You learn by watching things fail in various ways, though, so it's all just a matter of practice.
northjayd · 2022-07-14 05:32
Unfortunately any time I hit escape or for other unknown reasons it cancels the macro. Or when there is a message in the mini buffer. Hard for me to make macros for window and buffer setups when the macro quits out so easily.
whompyjaw · 2022-07-14 19:14
I gotcha. Thanks very much!
Mamonimoni · 2022-07-15 13:59
I tried this but things didnt really worked. I created a macro opening the agenda and filtering but then when I run the macro it doesnt work. It seems I have to add a "wait" command because the file is still being loaded. Is there a way to do this?