reddit / emacs

rg: focus buffer after running it

bruchieOP · 2022-05-11 05:18
Hello, when I run a rg command from rg.el it shows the results in a buffer window below where I ran my command, so far so good, but is there any way to have it focus the window automatically instead of having to switch to it everytime. I may be able to do macros, or a small lisp function but I have a feeling there is a better way to do this (and i can't find it) anyone has an ideas?
paretoOptimalDev · 2022-05-11 05:34
I'm guessing you don't need to switch to it if it's like the compile buffer. https://rgel.readthedocs.io/en/2.2.1/usage.html#navigation Does M-n from your existing buffer (not the search buffer)? What about M-x next-error or C-x ` ?
bruchieOP · 2022-05-11 05:46
yes next-error would works, but I am just use to have it popup like the "* grep *" buffer and press directly n/p after launching a grep. is there any way to achieve this? thanks!
Phil-Hudson · 2022-05-11 14:10
(defun my/rg () (interactive) (call-interactively #'rg) (switch-to-buffer "*rg*"))
bruchieOP · 2022-05-11 16:08
sure this works i had the same thing but with pop-to-buffer, but i was wondering if display-buffer-alist can do the same things..
bruchieOP · 2022-05-11 20:38
Here we go, that works with display-buffer-alist magic : (add-to-list 'display-buffer-alist '("\\*rg\\*" (lambda (buffer alist) (select-window (display-buffer-pop-up-window buffer alist)))))
habershabery · 2022-05-12 18:39
If you use deadgrep.el instead, you're put into the ripgrep results buffer by default.
flyin1501 · 2022-05-15 17:48
You might be interested in a way to do this without specifying `display-buffer-pop-up-window`: (add-to-list 'display-buffer-alist '("\\*rg\\*" . (nil . ((body-function . select-window))))) This only works [after (& including) Emacs 28](https://github.com/emacs-mirror/emacs/commit/3273e2ace788a58bef77cef936021d151815ea94#diff-360d836ae0f8385d635c9d5a03857ab5666fe4e153a0bfd30cdd8cd26f80e732R7076) though. --- `display-buffer-alist` becomes much easier to understand when written with dot syntax. Cross-referencing with [`display-buffer-alist`'s docstring](https://github.com/emacs-mirror/emacs/blob/emacs-28/lisp/window.el#L7422): - `"\\*rg\\*"` is the CONDITION - `(nil . ((body-function . select-window)))` is the ACTION, which is (FUNCTIONS . ALIST); - FUNCTIONS is `nil` (display-buffer will look elsewhere for the action to use) - ALIST is ((body-function . select-window)) - The action alist is documented in [`display-buffer`'s docstring](https://github.com/emacs-mirror/emacs/blob/emacs-28/lisp/window.el#L7568): > […] > > `body-function` -- A function called with one argument - the displayed window. […] The function is supposed to fill the window body with some contents that might depend on dimensions of the displayed window. - So with this entry in `display-buffer-alist`, `display-buffer` will call `select-window` with the displayed window. --- Emacs documentation really could use some more examples.
bruchieOP · 2022-05-17 14:23
oh this is great, thank you so much... I didn't realise this could be used like that. hopefully this post would help other if they want to achieve this..