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 669 times
2
-
Do you mean the same [window](https://www.gnu.org/software/emacs/manual/html_node/emacs/Basic-Window.html) instead of [buffer](https://www.gnu.org/software/emacs/manual/html_node/emacs/Buffers.html)? Reusing the same buffer would overwrite the search results. See also the answers to [this question](https://emacs.stackexchange.com/q/13583/15748) for further clarification. – Basil Jun 30 '17 at 05:34
-
good shout. Edited – Abraham P Jun 30 '17 at 09: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 Jun 30 '17 at 09:50
-
See related questions [here](https://stackoverflow.com/q/745694/3084001), [here](https://stackoverflow.com/q/8309769/3084001) and [here](https://emacs.stackexchange.com/q/19080/15748). – Basil Jun 30 '17 at 09: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 Jun 30 '17 at 10:01
-
@AbrahamP Note that the links are relevant since `grep-mode` is derived from `compilation-mode`. – Tobias Jun 30 '17 at 10:10
-
@AbrahamP A cursory glance reveals that the `compilation-mode` (as @Tobias usefully elaborated) family is pretty keen on opening a new window, but `ag` is special in that it provides the boolean user option `ag-reuse-window`. So an `ag`-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
1 Answers
6
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
- 5,917
- 1
- 24
- 37