setglobal, setlocal, set confusion

488 views
Skip to first unread message

Kai Weber

unread,
Aug 1, 2019, 8:49:51 AM8/1/19
to vim...@googlegroups.com
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

setglobal spelllang=en,de

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'?

Kai

Tony Mechelynck

unread,
Aug 1, 2019, 9:33:22 AM8/1/19
to vim_use
- ":setlocal option=value" sets only the local value of an option (for
the current buffer or window) without messing with the global default
- ":setglobal option=value" sets only the global default without
messing with any local value, not even the one in the current buffer
or window
- ":set option=value" sets both.

When displaying the existing setting, ":setglobal option?" displays
the global default, ":set option?" and ":setlocal option?" _both_
display the actual setting in the current buffer or window.

However, for global settings, :set, :setlocal and :setglobal all set
(or display) the global (and only) setting, with no error.

One setting I use in my vimrc is "setglobal fenc=utf-8 bomb" to set
'fileencoding' and 'bomb' to UTF-8 with BOM for new files. My new
files are usually HTML files and that setting suits me, it might be
unsuitable for some other people, for whom "setglobal fenc=utf-8
nobomb" might be better. The idea of using :setglobal is in order to
set the option only for future files for which the same options are
not set by some other mechanism (e.g., on existing files, the
'fileencodings' [plural] heuristic sets 'fileencoding' [singular] to
what it thinks is the file's charset, and it sets 'bomb' or 'nobomb'
for Unicode files depending on what was found at the very beginning of
the file, bypassing any global default). :setglobal does nothing to
already open files and that is what I want.

Best regards,
Tony.

Andy Wokula

unread,
Aug 1, 2019, 11:04:39 AM8/1/19
to vim...@googlegroups.com
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

Kai Weber

unread,
Aug 5, 2019, 8:33:27 AM8/5/19
to 'Andy Wokula' via vim_use
* 'Andy Wokula' via vim_use <vim...@googlegroups.com>:

> Only in rare cases there is any need to use ":setglobal".
> ...

Thanks a lot. Your explanation clears some of my misconceptions. I am
now back to using 'set' in my $MYVIMRC and 'setlocal' in my
ftplugin files.

Kai
Reply all
Reply to author
Forward
0 new messages