Ever since Kakoune made the x key more predictable, I missed the easier way it provided to select new lines. However, I still prefer the new x behavior.
The solution was to either xJJJJ or JJJJx. This was good because it also worked upwards, by using K instead of J.
After some tweaking, I found a couple of mappings that are so good, I think they should even be hard-coded:
map -docstring 'extend lines downwards' global normal X %{<a-:>F<ret>x}
map -docstring 'extend lines upwards' global normal <a-X> %{<a-:><a-;><a-H>Kx}
I think this configuration makes a ton of sense and matches the behavior of other commands in Kakoune, such as / to search, ? to extend, and the alt- variants to do it backwards.
The right-hand-side of a mapping can use the %val{count} expansion to apply the active count to whichever parts of the mapping itās relevant for. Unfortunately it defaults to 0 rather than 1, but 0 is not a normal-mode command so if youāre just prefixing to a different normal-mode command it should be fine:
map -docstring 'cool j' global user j %{:exec "%val{count}j:echo cool!<lt>ret>"<ret>}
Test with 5<space>j.
Note: You need the :exec in the mapping so that %val{count} is not expanded until the mapping is executed, rather than when the mapping was defined.
This was useful after I upgraded Kakoune and my old configuration (which used the X command that seems to have disappeared) broke. In fact, I map "x" instead of "X" because I use this so much.
I was completely used to typing xx to select two lines and would have been lost without this.
map -docstring 'extend lines downwards' global normal X %{<a-:><a-L>Jx}
ā¦not just in code similarity but also in behaviour: in the original version, pressing X selects one line but <a-X> immediately selects 2 - now both versions start with 2 lines selected (and if you want just one, well thereās good olā x).
define-command select_whole_lines_or_extend_lines_down %{
try %{
# At least one selection is not full, so select whole lines.
execute-keys -draft '<a-K>\A^.*\n\z<ret>'
execute-keys '<a-:>x'
} catch %{
execute-keys '<a-:>Jx'
}
}
map -docstring 'select whole lines or extend lines down' global normal x ':select_whole_lines_or_extend_lines_down<ret>'
map -docstring 'select whole lines' global normal X x