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

**URL:** <https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215>\
**Category:** Recipes & Guides\
**Tags:** grep\
**Created:** [August 17, 2020, 7:51am UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215 "2020-08-17T07:51:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [August 17, 2020, 7:51am UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215/1 "2020-08-17T07:51:19Z")

</div>

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:

```auto
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
	}}
}

```

---

<div class="post-metadata">

**Author:** ![robertmeta](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/robertmeta/32/833_2.png) [@robertmeta](https://discuss.kakoune.com/u/robertmeta)\
**Post date:** [August 17, 2020, 7:32pm UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215/2 "2020-08-17T19:32:01Z")

</div>

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.

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![bravekarma](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@bravekarma](https://discuss.kakoune.com/u/bravekarma)\
**Post date:** [August 17, 2020, 8:07pm UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215/3 "2020-08-17T20:07:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [August 18, 2020, 8:22pm UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215/4 "2020-08-18T20:22:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertmeta](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/robertmeta/32/833_2.png) [@robertmeta](https://discuss.kakoune.com/u/robertmeta)\
**Post date:** [August 19, 2020, 6:46pm UTC](https://discuss.kakoune.com/t/single-command-for-grep-next-match-in-similar-buffers-lsp-make-find/1215/5 "2020-08-19T18:46:37Z")

</div>

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!
