Based on v:count help I define:

:map _x :<C-U>echo "the count is " . v:count . ' and count1 is ' . v:count1<CR>


the count is 0 and count1 is 1
the count is 0 and count1 is 1
the count is 1 and count1 is 1
the count is 3 and count1 is 3

My goal is detect when map do not get count at all, but v:count always get a 0 value.

Is there any chance to detect that?

  • It might be better at this stage to create a separate, new question asking specifically about the counts passed to user-defined ex commands. The existing answer is correct for the original question you asked (which gave the example of a normal-mode map), and some might consider it unsporting to change the parameters at this point. (Not meaning to imply that you did anything wrong originally: I understand it's not necessarily obvious that normal-mode and command-line mode counts would behave differently.)
    – Rich
    Mar 12 '18 at 16:38
  • 1
    I create a wrong use case then vimdoc.sourceforge.net/htmldoc/map.html (Search for special case). Let's accept the answer
    – albfan
    Mar 13 '18 at 12:22
  • Accepting the answer looks good to me! (In case it's not clear, that bit of the documentation is explaining how you can do something like nnoremap 0 dd but then still use a normal-mode command with a count such as 20j without triggering your mapping when you type the 0 in 20.)
    – Rich
    Mar 13 '18 at 12:31

0 is not a valid count. 0_x does not mean give a count of 0 to command _x, it means go to the first column of the line, then run command _x. You cannot give 0 as a count.

Hence, v:count == 0 means no count is provided. Some commands interpret count 0 as equivalent to 1. For this reason, a shortcut v:count1 is also provided which is the same as v:count except when no count is given, in which case v:count == 1. You cannot use v:count1 alone to determine when no count is provided.

  • 1
    It defaults to -count=0 because 0 is used as the "no count provided" value. Normal mode commands can never receive a count of 0.
    – tommcdo
    Mar 12 '18 at 14:23
  • 1
    @albfan I think there is some confusion between ex commands like :Search and normal mode commands like _x. They both have something called a "count" but they are not the same thing at all.
    – Mass
    Mar 12 '18 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.