I've found it's common to set opfunc in this manner:

nnoremap <Leader>r :set opfunc=<SID>my_function<CR>g@

With Vim8 supporting lambda functions, is it possible to now set opfunc to a lambda function? The following cursory try did not work:

:set opfunc={-> "test" }

The operatorfunc is a option. A option can only have the types boolean (on/off), number or string (see :help options). So you can't assign a lambda to an option.

Also the documentation help 'operatorfunc' says

This option specifies a function to be called by the g@ operator.

My interpretation of "specifies" is "names".

I had a look at the code (function op_function in normal.c). It calls a vim script function by name.

So the answer is: No

  • You're implying that lambdas are not functions? From the Vim manual: A lambda expression creates a new unnamed function ...
    – Roxy
    Feb 17 '19 at 23:39
  • @Roxy See update
    – Ralf
    Feb 18 '19 at 6:22
  • I suppose it's also not possible to use a funcref in this spot, even let Q=function("Bar'), set opfunc=Q though call Q() works.
    – Mass
    Feb 18 '19 at 18:21
  • @Mass Correct. Its a String option, so you can only set a name.
    – Ralf
    Feb 18 '19 at 18:39
  • don't ever do this!: let Q = {->execute('quit')} | let &opfunc=get(Q,'name') :)
    – Mass
    Feb 18 '19 at 20:45

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.