Search code examples
vimkey-bindingsvim-plugin

Vim key binding to call function: control+c then 1


I am looking for a way to make a vim binding where I can enter control C (both the control and c keys at the same time), then another option (similar to yy or dd. For example, ctrl+c then 1 would be set so that a function I define, called my func, would be called like so: myfunc(1)

Here is my attempt so far: map <C-A> <F1>:call myfunc(1)<CR>


Solution

  • You can do it, with a few amendments:

    • Ctrl-c is used for interrupt signals; choose some other key combination, such as <Leader>c
    • It's a lot easier to do it with counts than arguments; that is, trigger the key combination with 3\c, rather than with \c3
    • User functions must have names beginning with upper case letters.

    With these notes, you might do it like this:

    nnoremap <silent> <Leader>c :<C-u>call MyFunc(v:count)<CR>
    

    v:count is a predefined variable that takes the value of the counter you passed to the last normal mode command, or 0 if there was no counter. There's also v:count1 that does the same thing, except it defaults to 1 if there was no counter.