Create an Emacs semi-interactive macro - emacs

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:
does some complex changes in the current line of text
position the cursor at a specific point
asks the user for some text in the minibuffer
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!

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.

Related

How can I use M-x in minibuffer?

Say I want to create a file. Then I was asked to enter the name. I want to use the date as the name of file. I want to use M-x insert date (I define myself) . How can I do that?
Set (or bind, depending on the context) variable enable-recursive-minibuffers to non-nil (e.g. t), before the first entry into the minibuffer.
That lets you use actions, such as M-x which themselves activate the minibuffer, from within a minibuffer. The second activation takes place in a recursive minibuffer. When it ends (e.g. you hit RET after replying to the M-x prompt), things continue as they were, with the initial minibuffer still active.

Emacs I want to call execute-kbd-macro from a lisp function

I want to call a keyboard macro from a Lisp function. I hope to layer in some custom error handling.
mykey is a keyboard macro stored in a text file in (fset ...) format.
I loaded it with load-file and it works fine when called with M-x mykey.
When I execute this function and plug in mykey I get only the name of the key displayed in current buffer, not it's execution. Is there a step I'm missing?
(defun gn-batch-search (key-name)
"Execute a keyboard macro that has already been loaded."
(interactive "sName of macro key:")
(execute-kbd-macro key-name))
Try
(defun gn-batch-search (key-name)
"Execute a keyboard macro that has already been loaded."
(interactive "sName of macro key:")
(execute-kbd-macro (intern key-name)))
The issue you bumped into is that the "sName of macro key:" interactive spec prompts the user and returns a string, and you want to run the command whose name is described by this string. That explains why it didn't do what you wanted and why you need intern.
As for why it did what it did: a keyboard macro is represented as a vector of events, where events can be things like mouse clicks or key presses. And as it turns out, a string is considered as a kind of vector (a vector characters) and a character is also an event (it represents the event that is sent when you press that character on your keyboard), so the string "hi" is a valid keyboard macro which represents the act of pressing h followed by pressing i, so when you run this macro, it will (usually) end up inserting "itself" in the current buffer (except in special buffers like dired, *Help*, ... where h and i are bound to other commands).
Also, rather than execute-kbd-macro you can use command-execute which will work with "any" command, whether it's defined as a keyboard macro or a normal function.

In Emacs, how do I select the completion list with the keyboard?

When I tab-complete in a minibuffer and Emacs displays a completion list in a new buffer, how do I switch to that buffer without using the mouse?
I tried C-x o, but that just switched to the first buffer, out of which I entered the minibuffer.
I also tried C-x b, but that gives me command attempted to use minibuffer while in minibuffer.
Lastly I tried C-x <C-right>, which gives me cannot switch buffers in minibuffer window.
EDIT: I spoke about minibuffer completion in my example, but being able to access the completion list (using the keyboard) from within a regular buffer is also important to me, and not working. The M-v shortcut was suggested, but it only seems to work inside the minibuffer accessed by M-x, in every other buffer I've tried, M-v is bound to scroll down command and does not switch to the completion list. I doesn't even seem to work in other minibuffers. For example, it doesn't work in the shell command minibuffer invoked by M-! either.
You can use M-v (documented here) which switches to the completion buffer and puts the cursor on the first completion:
Typing M-v, while in the minibuffer, selects the window showing the
completion list (switch-to-completions). This paves the way for using
the commands below. <PageUp> or <prior> does the same. You can also
select the window in other ways...
EDIT: It looks like based on your edit to the original question that what you're asking for is a way to switch to the completions buffer more globally. There is a function switch-to-completions that selects the completions list - you might consider binding that function to a key of your choosing, e.g.:
(define-key global-map (kbd "C-x t") 'switch-to-completions)
For example, such a binding allows me to switch to completions from "Shell command: " invoked by M-!, and places the cursor on the first possible completion.
You can use C-xo twice, or use M--C-xo, which switches to the previous buffer instead of the next one.

How to bind a key to kill repl and yank in a buffer on emacs

You can assume that I'm in repl using the slime mode.
How can I make a function key (for example, f4), to do this:
kill the last history item (the ones that you get with C-up or C-down);
move to the upper buffer;
yank, Save buffer to file;
move back to the repl.
Please, make it a step by step guide, because I'm a complete beginner to Emacs and Lisp.
The easiest way to make what you ask would be using emacs macros.
Why?
Because you have just said exactly what you want to do.
And macros save the sequence of keys you typed.
You can do it in emacs for one time, and save the sequence of pressed keys.
So, start recording a macro (when you are in the repl buffer) using F3 or C-x (, then make something like M-p C-a C-k C-u - C-x o C-y C-x o(i just translated your request to key sequence), then type F4 or C-x ). To execute macro, press F4 again, or C-x e.
You can interrupt recording a macro if you made a mess with C-g. The reverse is applied, if you made a mess and error message is send, your macro recording(sometimes frustrating) or evaluating(and this is feature, since you can make macro that will work good by just holding F4) would be interrupted.
If you want to use this macro later, you can name it with M-x name-last-kbd-macro. This will allow you to use as a command, typing M-x <your macro name> (<your macro name> - name of your macro). This will not save it for future sessions, just name it.
To save your named macro, use M-x insert-kbd-macro when you are in your .emacs file. This will insert elisp code at current point, executing which you will get your macro binded to your command name(and it will be executed every time you start emacs).
To bind it to some key, rather start it every time from M-x, insert this in your .emacs file: (global-set-key [f12] '<your-macro-name>). You can read more about setting comands to keys there and there.
The bad thing about macro is that you will undo every step, not the whole macro in one time(but someone may bring solution here, if he have one). If you want to make something more serious, using conditions or iterations, you have to forward your path to elisp. Just C-h k everything around. Help keys like C-h f, C-h a, C-h b will also come in use.

What is the key binding to enter input string in C-s or C-r in Emacs?

In finding string in the current buffer, I know that we can use C-s or C-r, then you'll have the minibuffer prompt of what the string to be search. I know that we can use M-p or M-n to go to that prompt, but unfortunately emacs will display the previous search query.
I don't like using backspace to delete it, is there any key in which the prompt can be cleared from previous searches?
Thanks
Are you saying you want to change your search term while you are already in the process of searching?
A couple of simple options are:
Break out of the search first. I usually just do something which moves point, such as C-a. Then when you C-s again, the prompt will be blank (unless you type it twice, in which case it will search for the previous pattern again).
RET is isearch-exit, but that has a different effect with no pattern, and I prefer the consistency. You could also use C-g (isearch-abort), but you may need to type that repeatedly, depending on what happened up to that point.
ESCESCESC runs isearch-cancel which will reliably "Terminate the search and go back to the starting point", which may sometimes be preferable to C-a which will leave you on the line where you typed it.
You can edit the pattern on the fly with M-e, and then delete the whole thing with normal editing commands. After editing, type RET to continue searching for your newly-edited pattern.
Type C-sC-hC-h for the built-in help.
Try binding a key to the M-x search-forward command.
(global-set-key (kbd "C-s") 'search-forward)
This will automatically focus on the minibuffer and clear previous input.