1

I'm asking about this code snippet:

:function TextwidthIsTooWide()
:  if &l:textwidth ># 80
:    return 1
:  endif
:endfunction

This is what vim-help says on the l (local) prefix:

Inside functions local variables are accessed without prepending anything. But you can also prepend "l:" if you like. However, without prepending "l:" you may run into reserved variable names. For example "count". By itself it refers to "v:count". Using "l:count" you can have a local variable with the same name.

Based on this, I would expect l:textwidth to be something like 0 or undefined, instead of taking the value of the global (?) textwidth option. (C.f. v:count vs l:count in the documentation.)

Question: Why does it take on the value of the option, even though it is local?

I do realize it has the & symbol prefixed, which refers to the option, so maybe it overrides local.

Question: In that case (i.e., if & overrides local), what is the point of making the variable local?

I tried the following change (only difference: remove l: prefix) and it seems to work too (even with setlocal -- of course I realize this is a different kind of "localness" compared to function-local variables)

:function! TextwidthIsTooWide()
:  if &textwidth ># 80
:    return 1
:  endif
:endfunction
:set textwidth=79
:echo TextwidthIsTooWide()
0


:set textwidth=81
:echo TextwidthIsTooWide()
1

:setlocal textwidth=79
:echo TextwidthIsTooWide()
0


:setlocal textwidth=81
:echo TextwidthIsTooWide()
1
2

This is all told under :h :let-option, but, to put it short, let &option="value" is the same as set option=value, let &l:option="value" is the same as setlocal option=value, and let &g:option="value" is setglobal option=value.

So your last point is actually about the difference between set/setglobal/setlocal commands. Again, putting it short, set option=value for a buffer-local option (such as :h 'textwidth') is the same as executing setglobal option=value and setlocal option=value at the same time.

Also, it should be noted that when reading from a buffer-local option using &-notation, "a local" value is returned, so if &tw > 80 and if &l:tw > 80 are basically the same thing.

| improve this answer | |
1

Side note: a global-local option may not have been overridden at the local level. This means we could work either with a global setting or one that has been overridden in a some buffers.

Usually users don't need much to bother with that. Even in most plugins, we can simply test &{optionname} that will return the most specialized value.

The troubles come when a plugin needs to momentarily change a global-local option and restore its value afterward. In that case it's better to determine whether it was overridden for the current buffer or not. And it's not that trivial to do... See my lh#option#is_set_locally()

BTW, as you could see, I never use l: when I have the choice. I don't see the point to use Hungarian notation in vimscript while I do not in other languages.

| improve this answer | |
  • It’s Hungarian, and I believe it to be misunderstood (but I’m not here to argue the point :) ) – D. Ben Knoble Aug 24 at 2:07
  • Oh indeed thanks. I didn't want to argue about its various forms either ^^' – Luc Hermitte Aug 24 at 11:06

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.