0

I've added the following to my .vimrc in order to test how <count> works in a user defined command because I was noticing odd behaviour:

:command! -count=1 CountTest :exe "echo " . <count> . "'" 
noremap <space> :CountTest<cr>

Now, when I type :3CountTest I get what I would expect, i.e., the number 3 is echoed into the prompt. However, when I try using my custom mapping to <space> (i.e. typing 3<space>) I get 3 + the current line number.

I'm sure it's just a problem with the way I've assigned my custom mapping. Any ideas?

0

In :h map-examples, it is mentioned that

When you type a count before triggering a mapping, it's like the count was typed before the {lhs}.

For your mapping, this means that the count is added before :. This means the actual command that is getting executed is, for count of 2:

:.,.+1CountTest

and for count of 5:

.,.+4CountTest

And the result of what you see is just how range given to a command which should take a count is handled. To handle the count correctly in the mapping, you can use

:nnoremap <space> :<C-u>exec v:count1 "CountTest"<CR>
  • Ahhhh thank you. I knew it was something like this but I couldn't get the syntax right. Cheers mate. – AMackie Dec 8 '15 at 13:30

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.