I'd like to have occur's prompt in the minibuffer do the same that isearch does: C-w inserts the word at point in the main buffer into the minibuffer prompt. That keybinding, when doing isearch, calls isearch-yank-word-or-char, so based on other questions, it seems like

(define-key minibuffer-local-map (kbd "C-w") 'isearch-yank-word-or-char)

would make the same thing work for occur / M-s o. But when I do the above and use C-w, I get [End of buffer] as an error message.

The remapping is working; if I do:

(defun myfunc ()
  (message "myfunc called!"))

(define-key minibuffer-local-map (kbd "C-w") 'myfunc)

it works as expected and echos "myfunc called!" in the minibuffer -- so the function call is working. Why would isearch-yank-word-or-char correctly find something and yank it when doing isearch, but somehow fail to find anything (as I guess "end of buffer" means) when I've activated the minibuffer prompt for an occur search?

2 Answers 2


Isearch grabs text at point in the buffer you're searching.

When you're in the minibuffer, that's the current buffer, so that's where isearch-yank-word-or-char tries to pick up a word or char. If you want to instead grab such text from the buffer that was current before you entered the minibuffer then you need to temporarily make that buffer current in the command you use to grab the text.

But to do that you need to have saved a reference to that buffer somewhere, before the minibuffer is entered. minibuffer-with-setup-hook is too late, as a place to do that: the minibuffer is already the current buffer when that macro is used.

If you happen to use Icicles then you can use M-. in the minibuffer to grab things at point from the buffer that was current before entering the minibuffer, and you can customize what that key/command does.

Also, with Icicles you have access to the buffer that was current before the minibuffer became active. That's save as the value of variable icicle-pre-minibuffer-buffer.


Related to @Drew's answer: my original intent was to ensure consistency by using the exact same function that isearch does. Then C-w would be guaranteed to work the same.

But it seems isearch is a bit special: as Drew pointed out in a comment elsewhere, it seems isearch doesn't use the minibuffer, or at least doesn't work the same as occur and such.

Instead, it seems much simpler to just use the approach from this question:

(defun my-minibuffer-yank-word ()
  "Yank word at point in the buffer when entering text into minibuffer."
  (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)

That has the advantage of being simple and understandable; it doesn't let the user hit C-w multiple times to yank more words but it's a good start.

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.