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?
Asked
Active
Viewed 727 times
2
1 Answers
5
Something like this should work for all modes derived from compilation-mode
:
(defun my-compile-goto-error-same-window ()
(interactive)
(let ((display-buffer-overriding-action
'((display-buffer-reuse-window
display-buffer-same-window)
(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.

François Févotte
- 6,007
- 1
- 25
- 38
Thanks for the links, will look through them over lunch!
– Abraham P Jun 30 '17 at 10:01grep-mode
is derived fromcompilation-mode
. – Tobias Jun 30 '17 at 10:10compilation-mode
(as @Tobias usefully elaborated) family is pretty keen on opening a new window, butag
is special in that it provides the boolean user optionag-reuse-window
. So anag
-specific solution would be to write a command revolving around(let ((ag-reuse-window t)) (call-interactively #'compile-goto-error))
. – Basil Jun 30 '17 at 10:16