1

I have a mapping like this:

noremap <expr> zj FindNext()

FindNext will calculate which line to go, and return something like 10G.

This works without a count, but with a count the line number becomes incorrect. For example, if the count is 2, 10G will become 210G. So I'm looking for a way to erase the count.

It is easy with :<C-U>execute 'normal! ' . FindNext()<CR> mapping, but it doesn't work in visual mode.

1 Answer 1

1

I think you can make it work in Visual mode by using Normal mode but re-creating the visual selection first with the gv command.

:<C-u>execute 'normal! gv' . FindNext()<CR>

And then use the mapping specific to Visual and Select modes (:vnoremap , :xnoremap, :snoremap). See :help mapmode-x.


You didn't supply much information on what your function does, so I'm not sure it this helps. I tried this simple function and mappings

function! Bar()
    echo "the count was " . v:count1
endfunction

noremap foo :<C-u>call Bar()<cr>
xnoremap foo :<C-u>call Bar()<cr>

The result is correct both in Normal and Visual modes. For example 10foo results in "the count was 10" in either mode.

But if you use the mappings bellow (which execute 2 commands one after another) it doesn't work anymore. 2foo always results in "the count was 1".

noremap foo :<C-u>exec 'normal! j ' <bar> call Bar()<cr>
xnoremap foo :<C-u>exec 'normal! gv j ' <bar> call Bar()<cr>

My conclusion from this is that the first command resets v:count1.

So I tried

function! Bar()
    let x = v:count1
    normal! j
    echo "the count was " . x
endfunction

noremap foo :<C-u>call Bar()<cr>
xnoremap foo :<C-u>call Bar()<cr>

and it works again. Note that v:count1 has to be saved before the normal! is executed. Otherwise its value will be lost.

3
  • This is a nice idea, but do you know how to get the count in visual mode? v:count1 doesn't seem to work. Apr 6, 2015 at 12:26
  • @yyt16384, I edited my answer. Apr 6, 2015 at 21:17
  • I think I get the point: every normal mode command resets v:count1, so the count needs to be saved before :normal. Apr 7, 2015 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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