Map global normal 0 (any number key mapping) vs. repeat count param

A mapping like map global normal 0 Gh unfortunately conflicts with the “type number before command to do thing N times” system. Is it possible to somehow avoid the conflict and have 0 do my mapping when I haven’t typed any numbers, but ignore the mapping when I am typing numbers already and there’s the param= thing in the corner?

Hello

Yes it’s possible as described at the bottom of this wiki page : https://github.com/mawww/kakoune/wiki/Normal-mode-commands#suggestions

# if you press 0 alone, it will echo "foo".
# if you press 0 after a number to express a count, like 10, it will work as usual.
map global normal 0 ':zero "echo foo"<ret>'
define-command zero -params 1 %{ %sh{
  if [ $kak_count = 0 ]; then
    echo "$1"
  else
    echo "exec ${kak_count}0"
  fi
}}
2 Likes

define-command zero -params 1 %{ %sh{

Shouldn’t that be eval %sh{ ... } rather than just %sh{ ... }?

1 Like

Yes indeed. Before Kakoune 2018.09.04 %sh{ ... } blocks were reparsed automatically. The example probably dates back to before that version. I updated the wiki page to use evaluate-commands.

Why does it have to echo "exec ${kak_count}0"
and cannot simply echo "0"?

@schemar When the mapping is triggered in normal mode, it immediately uses the key ‘:’ to change to the prompt mode and to execute the command zero. Kakoune requires that bodies of commands, including the above zero, consist of sequences of commands as opposed to keys. This also holds for the argument of evaluate-commands, which here is the output of the %sh{ ... } expansion. Hence the shell commands must print kakoune commands as opposed to keys. Therefore the key 0 is not an appropriate choice, but the command exec ... is.

In this case, kakoune will be in normal mode at the time zero is executed and hence the keys executed via exec will be interpreted in normal mode. Moreover, it seems that the zero call consumes $kak_count and thus that needs to be repeated inside exec for the intended effect.

I hope some of this explanation helped!

2 Likes