I would like to use a default argument that is defined outside of the function, for example:

func Run(function_name=DEFAULT_FUNCTION_NAME)
    echom function_name

However, I get an error stating:

E121: Undefined variable: DEFAULT_FUNCTION_NAME

What would be the possible way to 'scope this' so that I can do something like the above?

1 Answer 1


Be explicit with the scope: declare it with a given scope

let s:var = ...

And then use s:var (or another scope). Note that s: in particular may not work since the default arguments are evaluated at call time!

Edit: recent vim supports default function arguments, but I’ll leave this for older versions.

There are a couple of ways I can think of to handle defaults:

  1. Pass a “special value” to represent not supplied, and fall back to the default
  2. (The approach I see more often) document the use of a particular global variable, and use let var = get(g:, 'globalvar', default) to get the value or default for use.
  • thanks for this. Though with my vim, I think it supports default args: github.com/vim/vim/commit/…. I'm just having trouble passing a variable for a default arg, if that's possible.
    – David542
    Jun 13, 2020 at 22:20
  • Whoa! I missed that!
    – D. Ben Knoble
    Jun 13, 2020 at 22:23
  • Latest Vim versions do support default args. But Neovim still doesn't. IMO, it's better to avoid them for now.
    – Matt
    Jun 14, 2020 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.