3

Does Emacs have a key binding to yank the word/character under point into the minibuffer? I know isearch has "C-w" (isearch-yank-word-or-char) and "C-M-y" (isearch-yank-char). I often wish I could use something similar in other uses of the minibuffer.

A common use-case is when I'm reading some elisp and see a variable whose value I've set in my init file, but can't remember what I set it to. I'll run eval-expression and type in its name. But if the cursor is on that variable, I'd like to run eval-expression and hit some key to yank it into the minibuffer.

1
  • For variable info, just run describe-variable while your cursor on the variable. It is bound to C-h v. For help on functions, do the same with C-h f. It should pop the name of the variable as the default option into the minibuffer.
    – Arktik
    Commented Mar 27, 2023 at 20:06

2 Answers 2

4

Vanilla Emacs doesn't, but Icicles does. See Inserting Text from Cursor.

The key sequence M-. grabs one or more text things at point (the cursor position) in the current buffer and inserts them at point in the minibuffer. This is available whenever the minibuffer is active.

You can customize the behavior of M-., and when you use it you can control what it does.

When you use M-. repeatedly it can either:

  • Grab something different each time, (removing the last one inserted), giving you a way to use M-. to cycle to a particular thing at point.

or

  • Grab another thing of the same kind, giving you a way to insert successive things of a given type (e.g. word) in the minibuffer.

Which it does by default is controlled by an option, but you can use a prefix argument to flip the behavior temporarily.

It is really simple to use, even if it is also very configurable and you can change its behavior on the fly (i.e., as you use it).

[And yes, vanilla Emacs should have something similar - at least some way to grab something at point and insert it into the minibuffer. Suggestions for that have gone nowhere, alas. Such a feature is available for incremental search (Isearch), at least, and has been for decades.]

2

Instead of typing its name manually, you can also copy-and-paste if it is easier. Though implementing isearch's completely means lots of work, you can make a very simple one which might suit 80% of your needs

(defun my-minibuffer-yank-word ()
  "Yank word at point in the buffer when entering text into minibuffer."
  (interactive)
  (with-selected-window (minibuffer-selected-window)
    (when-let ((word (current-word)))
      (with-selected-window (active-minibuffer-window)
        (insert word)))))

(define-key minibuffer-local-map "\C-w" #'my-minibuffer-yank-word)

If you use Helm, (C-w) already does what you want. Helm provides the similar feature on the same keybinding (C-w) like isearch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.