This seems like it should be the easiest thing to do, but I'm having trouble calling a vim function with a variable. For example, I have the following function:

func Run1()
    echom 'Running run1'

This works fine:

:call Run1()
" Running run1

Now, to add a function parameter:

func Run2(var)
    echom 'Running run2 with var ' . var

And calling it should be as simple as:

:call Run2('hello')
# E121: Undefined variable: var

But I get an error. How should I function be called then?


It’s the usage that’s wrong: all function-arguments must be prefixed with a: in the body of the function (but NOT in the function declaration line).

This is because the default is one of l:/s:/g:, so the argument doesn’t even get seen (it’s in a different dictionary).

  • @D Ben Knoble -- that worked, thank you. This vim syntax is quite odd I have to say!
    – David542
    Jun 13 '20 at 22:22
  • @David542 indeed, though there are some things I’ve grown to like. There is push for a newer, JS/python like syntax. Look for vim9 in the vim repo.
    – D. Ben Knoble
    Jun 13 '20 at 22:27

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.