In h: internal-variables
it mentions the "global" variable which is prefixed by g:
. What would be the difference between:
let g:var = "xyz"
And:
let var = "xyz"
Does the absence of a prefix mean g:
, or are they two different things?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityIn h: internal-variables
it mentions the "global" variable which is prefixed by g:
. What would be the difference between:
let g:var = "xyz"
And:
let var = "xyz"
Does the absence of a prefix mean g:
, or are they two different things?
In VimScript all variables have a scope. Or, better to say, they are organized into :h Dictionaries
with reserved names. So echo g:
is a perfectly valid command.
There are the following scopes (dictionaries) in no particular order:
g:
Globals:
Script-local (like "static" in C)l:
Function-locala:
Function argumentv:
Vim internalb:
Buffer localw:
Window localt:
Tab localIf scope is not given then v:
is searched. If there's no such variable in v:
then it's either s:
or l:
depending on the execution context. Also, while on the command line it's g:
, as, obviously, neither s:
nor l:
applies in interactive mode.
:let var = "new"
then that goes into the g:
dict because I haven't qualified its prefix/namespace?
echo g:var
or echo get(g:, 'var')
s:
... oO BTW, there is also a little issue with lambdas: we have to be explicit to use global variables, even interactively, while it's not the case in echo map(somelist, 'v:val > globalthreshold')
.
Jun 13, 2020 at 13:29
l:
, a:
and s:
as if executing in the outer scope. However, it's really strange that "global" lambda does not have g:
by default. Should it be called inconsistency or bug?