I can use display-buffer-alist to configure a command to open in the same window:

(add-to-list 'display-buffer-alist '("*grep*" display-buffer-same-window))

The problem in this case, however, is if I then open a result and subsequently run a new search without closing the original window, Emacs will open a second view of the search results in the current buffer. That gives me two redundant views of the same results, which is obviously not my intention.

I would like this to be a bit smarter, in that if the buffer is already visible in the current frame, it should merely reuse it (or change focus to it), and display in the current window otherwise.

How can I achieve this behavior?

1 Answer 1


display-buffer-alist actions can be multiple functions (a list of functions), the first one which returns non nil is used.

So you can use:

  (add-to-list 'display-buffer-alist '("*grep" (display-buffer-reuse-window

More info:
Emacs manual about ready availabe display-buffer actions

  • Perfect, thanks. I'd seen that page, but didn't realize it could take a list of functions, though I suppose the very name 'list' implies that.
    – Digicrat
    Feb 28, 2020 at 3:08

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.