Regex Motions

I’m new to Kakoune, but am liking it a lot. In Emacs, I can write scripts to move the cursor forwards or backwards based on some regex. I see that Kakoune understands the concept of regex, but I can’t find information on how to build motions out of it.

I would like to make a map that can select or extend to the next ), }, or ] character from the cursor. It would search forwards until it reaches any of those characters. Is this trivially possible with Kakoune?

look the f default mapping entry in the help buffer obtained with typing :doc keys in Kakoune.

I know that command, but I don’t understand how to give it a regex. I tried:

map global user f ‘f[})]]’

And variations thereof, but this just selects to [. There are backslashes, but they don’t appear in Discuss.

You’re looking for the search forward key, /:

map global user f /\)}]<ret>

You need to backslash-escape ) otherwise Kakoune thinks it’s an unmatched grouping symbol.

There’s also extend forward (?), search backward (<a-/>) and extend backward (<a-?>).

1 Like

Thank you! That helped a lot. For anybody reading, I got the exact behavior I wanted with these lines:

map global normal <a-.> '/[\}\)\]]<ret>'
map global normal <a-,> '<a-/>[\[\(\{]<ret>'
map global normal <a-gt> '?[\}\)\]]<ret>'
map global normal <a-lt> '<a-?>[\[\(\{]<ret>'

It’s so cool how expressive Kakoune is.

1 Like

There’s also the m and M keys for selecting and extending to the next matching character.

1 Like

Hello EPonyA and welcome ! :slight_smile:

I believe most of our user comes from vim, I’ll be curious to know why - comming from emacs - you choose to try kakoune

Cheers!

1 Like

A belated comment (and tip) on regexes. As explained in :doc regex, Kakoune has special \Q and ‘\E’ regex delimiters that ease a lot of quoting pain: everything between \Q and \E (or the end of your regex) will be matched literally. This can be helpful when you want literal matching of opening and closing braces or brackets, for example.

1 Like

I believe most of our user comes from vim, I’ll be curious to know why - comming from emacs - you choose to try kakoune

Originally, the reason I used Emacs was because of a unique modal editing model named Xah Fly Keys. I think it’s a pretty good editing model overall, but I read about Kakoune and was intrigued, especially because of kakoune tv. I hacked together similar tools with VS Code’s modal-edit package (haven’t tried Dance), and tried making Evil work similarly in Doom Emacs, and I worked with both ryo-modal and kakoune.el, but these solutions were always a lot of friction so now I’m learning the real thing.

The general concept of Unix philosophy appeals to me, and it seems like Kakoune works very well with that kind of workflow due to shell expansions. I use Kitty for my terminal, so the great integration in Kakoune is super nice for me. I haven’t actually used (Neo)vim, but my understanding is that it’s a bit less Unix-y due to having its own window splits and scripting language.

Kakoune just seems like essentially the perfect editor for me right now.

1 Like