F, t and <a-.> motion

When I used vim, I often used the f command and the multiline vim-sneak version of it. I actually found no use case for the Kakoune version of the f command regarding the selection, as I just need it for jumping (most of the time) inside a line. When I want to select a region, I can just use F. It would also be much more useful if the <a-.> command after t would jump to the next match.

So I ended up with the following commands. Maybe it is useful for someone of you too.

define-command fandt-f %{
    on-key %{ execute-keys "f" %val{key} ";" }
    map global normal "<a-.>" "<a-.>;"
}

define-command fandt-t %{
    on-key %{ execute-keys "t" %val{key} ";" }
    map global normal "<a-.>" "l<a-.>;"
}

define-command fandt-reverse-f %{
    on-key %{ execute-keys "<a-f>" %val{key} ";" }
    map global normal "<a-.>" "<a-.>;"
}

define-command fandt-reverse-t %{
    on-key %{ execute-keys "<a-t>" %val{key} ";" }
    map global normal "<a-.>" "h<a-.>;"
}

hook -group fandt global NormalKey <a-T> %{
    map global normal "<a-.>" "H<a-.>"
}

hook -group fandt global NormalKey T %{
    map global normal "<a-.>" "L<a-.>"
}

hook -group fandt global NormalKey <a-F>|F %{
    unmap global normal "<a-.>"
}

map global normal f ": fandt-f<ret>"
map global normal t ": fandt-t<ret>"
map global normal <a-f> ": fandt-reverse-f<ret>"
map global normal <a-t> ": fandt-reverse-t<ret>"
2 Likes

I made a plugin that tries to make the f and t commands in Kakoune a bit more useful.

https://gitlab.com/listentolist/kakoune-fandt
https://github.com/listentolist/kakoune-fandt

For scripting the normal f and t commands are good because they are predictable and do not try to be too clever. But for interactive use they are not half as powerful as the Vim f and t keys. One big thing that I am missing is the possibility to cycle through the matches. This plugin defines a fandt user mode that acts a bit like the clever-f plugin in vim. So you can just hit the f and t keys again to repeat the command.

2 Likes

Just tired it and it works very well for me !

I tried to implement this a few month ago but didn’t worked well, I’m so happy you did it :slight_smile:

1 Like