hgiesel
hgiesel

Reputation: 5658

Vim: Make function with count only operate once

I have this function:

function Test()
    echom "call " . v:count
endfunction
nnoremap a :call Test()<cr>

If I type, let's say 4a, it will print out

call 4
call 4
call 4
call 4

However, I want it only to be executed once, when I use a count. How can I achieve that?

Upvotes: 0

Views: 45

Answers (1)

ronakg
ronakg

Reputation: 4212

You need <C-U> before calling the function.

function Test()
   echom "call " . v:count
endfunction
nnoremap a :<C-U>call Test()<cr>

<C-U> removes all the characters before the cursor on command line. From help:

                                                        c_CTRL-U
CTRL-U          Remove all characters between the cursor position and
                the beginning of the line.  Previous versions of vim
                deleted all characters on the line.  If that is the
                preferred behavior, add the following to your .vimrc:
                        :cnoremap <C-U> <C-E><C-U> 

Upvotes: 2

Related Questions