0

From the docs, local variables are defined as:

  • script-variable s: Local to a :source'ed Vim script.
  • function-argument a: Function argument (only inside a function).
  • local-variable l: Local to a function.

It seems that a is the simplest and most narrow -- this can never be user-defined within a function and is only used to reference the variables that were passed to a given function. In other words, I cannot do something like a:name = "Tom", and its the only variable type that cannot be defined on the left-hand side.

What would be the difference then between l and s? As far as I understand, l:var can only be done inside a function and is illegal to use outside of a function, and once the function is done the l:var variable is discarded. And then s:var I'm not too clear on at all (for example, why can't I call this from the command-line within the function its being used?).

  • 2
    As @Matt said here: script local variables are as static variables in C. In vimscript language, ther name is quite explicit: their scope is restricted to the script they belong to. They cannot be referenced outside. And as usual with programming with every other programming language that I know, local variables cannot be used outside the scope of their function. – Luc Hermitte Jun 13 at 23:00
  • @LucHermitte -- I see, the only other thing I thought odd was when I define a variable in, for example, .vimrc it adds that variable into g: rather than s:. Why does a variable defined outside a function in a script belong to the g: scoope? – David542 Jun 13 at 23:14
  • It's like how it works in C. Variables defined at file (translation unit actually) level that aren't explicitly declared static are global variables. Vim follows the same rule. – Luc Hermitte Jun 13 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.