reddit / emacs

How do you use macros?

HumanBrainMapper · 2022-08-27 16:22
How and when do you use macros? I have been using emacs for 5 years and everything I do that needs repeating is captured in a script. I also don't read much about people using macros, so that got me wondering. How and when do you use macros? Moreover, why do you use macros over regular lisp / shell-command-on-region / ... ?
Alan_Shutko · 2022-08-27 16:38
I use macros for a lot of one-off uses. For example, I have a log file with a few data elements I need to extract from it. I could use regexps but sometimes that's not easy (things with nested expressions, for instance). So I'll whip up something like this: * C-s SOMETHING RET to find the next line that matters * C-s to find the start of the piece of data I need to grab. * Set mark * Movement commands (forward-sexp or C-s or something) to find the end of the data * C-x r x to copy the info into a register * Repeat for anything else I need in the log entry * C-x o to flip to the other buffer in the frame, which is where I want my output * Type in the output format I want, using C-x r i to insert the text from registers where I need it. Maybe I'm making a bunch of CSV lines, or fitting it to a json format, or doing some text for an email When I've made the macro, I go to the start of my log file buffer and do C-u 100000 C-x e. It'll find all the requisite lines and the isearch-forward will fail when it can't find any more lines, stopping the macro execution.
jsled · 2022-08-27 17:26
Copy paste some list out of excel or jira or confluence and the formatting is just a /little/ off from org-down, so `C-x ( [edit, regexp-replace, move edit hack] C-x ) C-e e e e e e e […]`.
mickeyp · 2022-08-27 17:56
Keyboard macros work well for a large range of tasks, and not just text editing. You can use them to set up your favourite window layout, and much more. If you don't know elisp, keyboard macros make it possible to do much of what an elisp hacker can also do. I wrote about it a while back here: https://www.masteringemacs.org/article/keyboard-macros-are-misunderstood
agumonkey · 2022-08-27 17:59
people let's share our kmacros tricks :)
justinhj · 2022-08-27 18:20
Very often and for any repetitive task that can be easily captured in a macro. Usually it’s a one off
RoninTarget · 2022-08-27 18:56
I used to put `M-b M-u` in a macro if I had a lot of full word capitalizing to do. Few other tasks that had to be simplified as well, but those are rare one offs.
xtifr · 2022-08-27 19:14
I assume you're referring to keyboard macros, as opposed to elisp macros. Keyboard macros are definitely a quick-and-dirty tool, but sometimes, that's what you want. Their main advantage is that you can see the results of what you're doing as you do it, unlike with elisp, where you have to reason it all out separately. Basically, I use macros when they feel like the easiest solution to the problem in front of me. Of course, whatever I do with a macro, I could also do with lisp, since macros just call lisp code indirectly through keybindings, but in many cases, that would require more effort. Macros fill a nice intermediate area between the stuff that's simple enough for a quick regex to achieve, and the stuff that's complex enough or done often enough to justify writing and debugging some lisp.
mickeyp · 2022-08-27 19:15
You can prefix with a negative argument to change the directionality of many commands. Like `M-- M-u`.
Jak_from_Venice · 2022-08-27 20:04
GUI programming. No jokes. I had a bunch of structs that needed to be edited in a Qt window. Graphic fields where named after the name of the field on the struct. What I did was to use a keyboard macro to: - find the semicolon (;) in the current line - set a mark - move one word to the left - copy Now I have the field name. Then: - switch to buffer - go to the end of the buffer - write the code for a QLabel - set the text wuth CTRL-Yank - add a QTextField and name it with CTRL-yank - switch to previous buffer - go down one line
bondaly · 2022-08-27 20:06
Keyboard macros are great, and I use them often for odd jobs. But I recently found out about C-x r t for editing rectangles. It replaced some of my used of keyboard macros and regex replacements. I highly recommend taking a look at it too.
permetz · 2022-08-27 20:13
I use the things practically every day. I routinely mass modify files of data with them and I often use them to mechanically refactor code. The fact that you can set up and increment a counter in the things is a way underused feature too.
tangonov · 2022-08-27 21:28
You may just use `C-u 0 C-x e` to go until you hit the bottom of your buffer
cerka · 2022-08-27 21:58
Also `C-v` in Evil normal state which I use a lot
HM0880 · 2022-08-27 22:51
Yep, I use keyboard macros to move text in complicated ways like this. If there's a need to do lots of "next <punctuation character>, skip N chars/words/lines, copy text, switch buffers, yank text, back to previous buffer, next line," I use a keyboard macro and `repeat` the macro until I'm done. I bind `repeat` [1] to `M-o` for quick use; very handy even outside of keyboard macros. [1] https://www.masteringemacs.org/article/repeating-commands-emacs
bagtowneast · 2022-08-28 02:16
The biggest trick is finding that break even point where the minimal, but real, time spent making the macro is worth it versus hammering through the last few instances of a given action. I'd like to get to the point of thinking of the keyboard macro as part of any action, so that I can make the switch earlier in the process. Having typed that out, though... I wonder if there is a way to build a keyboard macro from the key stroke history... View the history, set the mark, work back through the steps and then grab the region and immediately make a macro out of it. I bet this already exists.
_viz_ · 2022-08-28 03:41
Checkout kmacro-edit-lossage
bagtowneast · 2022-08-28 04:36
Brilliant, thank you.
fragbot2 · 2022-08-29 15:03
I used Emacs for years (including writing my own elisp on occasion to automate things) prior to using keyboard macros. As an infrequent user, I'll only really use them when I'm working on a file where I need to do the same edit over and over. My most common example is a giant ledger file that I insert generated content into periodically that requires identical input per entry.
cpleasant · 2022-09-01 20:41
Have a lot of woes. Great pics!