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– Basil2017-06-30 05:34:24 +00:00Commented Jun 30, 2017 at 5:34
- good shout. EditedAbraham P– Abraham P2017-06-30 09:07:57 +00:00Commented 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– Basil2017-06-30 09:50:46 +00:00Commented Jun 30, 2017 at 9:50
- See related questions here, here and here.Basil– Basil2017-06-30 09:52:03 +00:00Commented 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– Abraham P2017-06-30 10:01:27 +00:00Commented Jun 30, 2017 at 10:01
| Show 2 more comments
1 Answer
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.
- Just wanted to say thanks for this, working great 8 years later :)Personman– Personman2025-01-02 01:30:07 +00:00Commented Jan 2 at 1:30