Say I run ag or grep via M-x and display a window with search results. Say I then use my enter key to open a result. How can I force emacs to open this result in the same window as the search results are currently displaying, rather than a different window?

  • Do you mean the same window instead of buffer? Reusing the same buffer would overwrite the search results. See also the answers to this question for further clarification.
    – Basil
    Commented Jun 30, 2017 at 5:34
  • good shout. Edited
    – Abraham P
    Commented Jun 30, 2017 at 9:07
  • Do you want to be able to pick between using the current or a different window, or do you only ever want the current window to be reused?
    – Basil
    Commented Jun 30, 2017 at 9:50
  • See related questions here, here and here.
    – Basil
    Commented Jun 30, 2017 at 9:52
  • Ideally I am looking for the equivalent of pressing o vs Enter in Dired mode (e,g an easy way to pick one or the other) Thanks for the links, will look through them over lunch!
    – Abraham P
    Commented Jun 30, 2017 at 10:01

1 Answer 1


Something like this should work for all modes derived from compilation-mode:

(defun my-compile-goto-error-same-window ()
  (let ((display-buffer-overriding-action
           (inhibit-same-window . nil))))
    (call-interactively #'compile-goto-error)))

(defun my-compilation-mode-hook ()
  (local-set-key (kbd "o") #'my-compile-goto-error-same-window))

(add-hook 'compilation-mode-hook #'my-compilation-mode-hook)

With this snippet added to your init file, pressing o in a grep buffer (or any buffer derived from compilation-mode) will open the location in the same window.

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.