Single command for grep-next-match in similar buffers (lsp/make/find)

In Vim many things go into the same error list whereas Kakoune tends to use different buffers with filetype=grep. This is nice because I can browse them independently.
However, when pressing my shortcut for grep-{next,previous}-match I usually only care about the last accessed grep-like buffer. Here is the command I use:

declare-option -hidden str my_grep_buffer
hook -group my global WinDisplay \
	\*(?:grep|find|make|references|diagnostics|implementations|symbols|cargo)\* %{
	set-option global my_grep_buffer %val{bufname}
}
define-command -override my-grep-next-match \
	-docstring 'Jump to the next match in a grep-like buffer' %{
	evaluate-commands -try-client %opt{jumpclient} %{
		buffer %opt{my_grep_buffer}
		execute-keys "<a-l> /^[^:\n]+:\d+:<ret>"
		grep-jump
	}
	try %{ evaluate-commands -client %opt{toolsclient} %{
		buffer %opt{my_grep_buffer}
		execute-keys gg %opt{grep_current_line}g
	}}
}
define-command -override my-grep-previous-match \
	-docstring 'Jump to the previous match in a grep-like buffer' %{
	evaluate-commands -try-client %opt{jumpclient} %{
		buffer %opt{my_grep_buffer}
		execute-keys "g<a-h> <a-/>^[^:\n]+:\d+:<ret>"
		grep-jump
	}
	try %{ evaluate-commands -client %opt{toolsclient} %{
		buffer %opt{my_grep_buffer}
		execute-keys gg %opt{grep_current_line}g
	}}
}
3 Likes

this is – amazing – I automatically generate LOTS of grep buffers, and I never took the time to fix this for myself! Thank you so much.

map global grep g %{<A-i>w"gy<esc>: grep <C-r>g<ret>: try %{delete-buffer *grep*:<C-r>g}<ret> : try %{rename-buffer *grep*:<C-r>g}<ret> : try %{mark-pattern set <C-r>g}<ret>} -docstring "Grep for word under cursor, persist results"

Would this work with *lint-output* buffer too? AFAICT it doesn’t use the grep_current_line option so updating the current line in the buffer window might not work.

Yep, it works the same for *lint-output* - any buffer with lines starting like <file>:<line>:, in fact.
For *make* I still use make-next-error because that’s more picky.

I haven’t figured out a good way to have this functionality out-of-the-box.
grep-next-match could take an optional buffer argument I guess.

I just ended up replacing the grep-next and grep-previous and setting the custom buffer as a part of my little ,gg – works great! Thanks!