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.

3 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!