1

I would like to create a quick Emacs macro (with C-x () which takes some user input from the minibuffer each time it's executed.

The usage scenario is this:

You have a complex text to change where the regexp query substitution just doesn't cut it, and you want to create a macro which:

  1. does some complex changes in the current line of text
  2. position the cursor at a specific point
  3. asks the user for some text in the minibuffer
  4. possibly executes some other action, then exits

I have tried using

M-x eval-expression

and

(insert-text (read-from-minibuffer ">"))

but after creation, the macro obviously (in retrospect... :) ) repeats the same keystrokes and therefore doesn't give me a chance to insert some new text.

Can this be done?

Thank you for the help!

Rick77
  • 3,121
  • 25
  • 43

1 Answers1

3

Use kbd-macro-query, normally bound to C-x q. Specifically, you should use C-u C-x q, which enters a recursive edit mode when you're defining the query and lets you enter whatever text you like to a minibuffer prompt, but note that the text you enter here will not be part of the keyboard macro. While you're defining the macro, you'll need to hit <enter> once to end the recursive edit and then hit it again to send the text to the minibuffer prompt so you can continue defining the macro. Once you finishing defining the macro, you can then execute it and it will stop at the point you called kbd-macro-query, prompt for the text, and then hitting <enter> will work as expected.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
  • Thank you for the answer! I'm delaying to accept the answer until I figure out how to make it work. I confess I'm not having much luck... I get until the `C-u C-x q` part and on next execution i will get prompted (C-r, C-l, Y, N, RET), but I can't figure neither out how to exit from recursive edit mode when defining the macro (Return doesn't seem to work, it just inserts a carriage return), nor how to enter the text in the minibuffer (should it appear magically? Should I use `eval-expr` and `read-from-minibuffer`?). I'm not giving up, and still doing my homework, though ;-) – Rick77 Feb 06 '15 at 17:24
  • I assume that during the macro creation you're pressing a key combination that results in a minibuffer prompt, then pressing `C-u C-x q`, entering some dummy text that will not be part of the macro but that will allow you to continue defining the macro, then pressing `enter` to exit the recursive edit, and then pressing `enter` again as part of the continued macro definition? – Steve Vinoski Feb 06 '15 at 21:00
  • Oh... Now I get it: thank you: I was triggering the recursive mode at the wrong time! After this explanation, I was able to create a simple interactive macro with almost no effort (as a reference for future readers, i used: `C-x ( test macro M-x eval-expression (insert-string (read-from-minibuffer ">")) C-u C-x q text end macro C-x )`. Now I just need to save the `(insert-string (read-from-minibuffer ">"))` code and key bind it, and I'm good to go (or is there something like that already defined? I would expect nothing less after seeing `C-x M-c M-butterfly`... :) ) – Rick77 Feb 06 '15 at 22:55