New plugin: kak-live-grep

asciicast

It has always bothered me that Kakoune does such a good job with visual feedback when editing, but that it does not extend to the grep integration. To use :grep, you have to type your regex, then see if it worked, then try again if it didn’t. I got fed up with it. I tried to integrate an external fuzzy finder like FZF, but I wanted something integrated into Kakoune itself.

Introducing kak-live-grep! The plugin is so simple that I can’t believe it wasn’t already made (unless my googling powers are failing me).

There isn’t much to explain, the main feature is fairly obvious. Call the live-grep command, and it will open the *grep* buffer with a prompt. As you type, the *grep* buffer will populate with the results. You can easily spot and fix mistakes with your regex as you go.

An extra feature can be enabled with the live_grep_select_matches option. If this option is enabled, the exact matches of your regex will be selected in the *grep* buffer as you type. Once you are finished typing the query and exit prompt mode, you can easily replace what you searched for with whatever you need. Combined with grep-write.kak, this enables a very smooth find/replace workflow! This feature has been changed, see below.

One note: the plugin is incredibly stupid, and calls grepcmd synchronously on every keystroke. I tested it on small and medium sized projects, but it may chug a bit in large projects.

5 Likes

After attempting to use this in a real workflow, I encountered a few usability issues and errors.

The live-selection of matches in the results buffer had many issues, and with incomplete regexes, could cause kakoune to hang for seconds at a time. This feature has been removed. Instead, I have added an optional LiveGrepMatch face that will live-highlight matches in the results buffer as you type. This solves the performance issues and still lets you see the matches if you wish.

The auto-selection of matches now happens when you complete the query. Using <ret> will place you in the grep buffer like normal, but will keep the matches highlighted with LiveGrepMatch. Completing the query with <s-ret> will remove the highlighter and select the matches as it did before. This is much more ergonomic than changing an option, and allows you to use either modes when you need them. I also fixed several escaping issues when selecting the matches.

Please let me know what you think!

I have moved back to GitHub, so the new plugin URL is https://github.com/raiguard/kak-live-grep. Sorry for the confusion!

It looks like the recording was archived! Could you make a new one?

Sorry for missing this, recording has been updated on the GitHub readme.

The plugin has also been updated with a few new customization options. live_grep_timeout will adjust the timeout when updating results (defaults to 300), and live_grep_results_limit adjust the maximum number of results that will be displayed (defaults to 10,000). Both of these are to fix performance issues I’ve experienced when using this plugin in large codebases.