Thursday, August 1, 2019

Re: setglobal, setlocal, set confusion

Am 01.08.2019 um 14:49 schrieb Kai Weber:
> I found out about 'setglobal' and read the documentation. It seems I am
> still confused and think that 'set' is more global than 'setglobal'
> despite the name suggests.
>
> I tried to set my spelllang with setglobal

First note that spelllang is a "buffer-local" option [1]:
The "local" value is the currently active value for the buffer.
The "global" value is used to initialize the "local" value of a newly created buffer.

> setglobal spelllang=en,de

=> This does not modify the active value.

> But I found that the spelling in my buffer did not checked for german
> spelling problems.
>
> :verbose set spellang
> spellang=en
>
> But
>
> :verbose setglobal spellang
> spelllang=en,de
> Last set from ~/.vim/plugin/settings.vim line 34
>
> So, I have to use 'set' instead of 'setglobal' to set spelllang
> globally? What is a reasonable use case to use 'setglobal'?

":set" sets both the "local" and the "global" value.

Now the question is, why ":setglobal", when used from a plugin file,
does not initialize the local 'spelllang' value of your file.

:h startup

[...]
2. Process the arguments
The options and file names from the command that start Vim are
inspected. Buffers are created for all files (but not loaded yet).
[...]
4. Load the plugin scripts. *load-plugins*
This does the same as the command: >
:runtime! plugin/**/*.vim
[...]

=> the option values are already initialized when the plugin file is loaded.


In the vimrc and plugin/settings.vim etc, you will want to use ":set".

Most often you will want to use ":setlocal" (interactively and in ftplugin
scripts).

Only in rare cases there is any need to use ":setglobal".
Eg in older Vims, ":setglobal noswapfile" in a BufReadPre autocmd prevents
creation of a swapfile (now there is :noswapfile) keeping the swapfile of
the current buffer. (and should later not forget to again :setg swf)


-----------------------------
[1] a few other options with a local value are "global-local", just to confuse
you a bit more ... A global-local option may or may not have a local value.
When a global-local option has no local value, then :setglobal also changes
the active value.

--
Andy

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5D42FF70.6020304%40yahoo.de.

No comments:

Post a Comment