3

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>

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kilojoules
  • 9,768
  • 18
  • 77
  • 149

1 Answers1

4

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.

lcd047
  • 5,731
  • 2
  • 28
  • 38
  • 1
    What is the signifigance of ``? I'm having trouble making this work. Do you see anything wrong with `noremap :call Myfunc(v:count)`? I have it set this way, but cannot access the function with ctrl+a.... – kilojoules Jul 13 '15 at 08:45
  • 2
    Yes, your map is wrong. `` is needed, it removes the count after `:`. `` is the key for stop signal, you won't be able to map it. `` adds the count to the character under the cursor. You probably could map it if you're really determined, but you shouldn't. There are preciously few `Ctrl` keys that don't already do something important. _shrug_ – lcd047 Jul 13 '15 at 08:56