LumpyGrads
LumpyGrads

Reputation: 132

Vim option scoping: global-local, local, and global

The Vim help documentation on |options| makes distinctions between options and option values; and between global, local, and global-local options. It is extremely confusing, especially given |local-options|:

"It's possible to set a local window option specifically for a type of buffer.... Vim keeps a global value of the local window options, which is used when editing another buffer. Each window has its own copy of these values. Thus these are local to the window, but global to all buffers in the window."

In the subsequent example, it shows that :setlocal list makes the local window option local to a buffer. But what about global or global-local window options?

Can anyone explain the reason for the difference in terminology between options and values, and between the three types of options?

Upvotes: 0

Views: 1948

Answers (1)

Matt
Matt

Reputation: 15186

Well, it's pretty easy, IMO. Just think of what an option is used for.

Say, confirm is global, as it makes sense per Vim instance. Hence, even if you execute setlocal confirm, that would silently change a global option, as it's the only one existing.

Next, expandtab is local per buffer (note: some options can be local per window, such as :h 'diff', for instance). However, there exists both global expandtab (which is essentially is "the current default value" of expandtab for newly created buffers) and local expandtab (one per each existing buffer). Also note that some "defaults" are pretty much useless (e.g. filetype will be set by ftdetect anyway, but, nonetheless, the global filetype exists).

Now, what's a global-local thing? That's simply an option which may (or may not) be created for every existing buffer. But, of course, there's always a global option of this kind.

How it's different from an ordinary local option? Say you have in your vimrc:

setglobal noexpandtab noautoread

Now you run your Vim and enter:

:setlocal expandtab? autoread?

And then you get in response:

noexpandtab
--autoread

That means you have a buffer-local copy of your global expandtab, but local autoread was not created at all in the current buffer, so Vim would use a global value whatever it'd be.

To summarize said above:

  1. "Global option" means that no local value exists at all. So global value is always used.

  2. "Local option" means that all buffers (or windows) always have a copy of their own, and so "global" value is only meaningful as the default value.

  3. "Global-local option" means that some buffers (or windows) may have their copy, but others may not. So Vim uses the current local option if set, otherwise it uses the global option.

Upvotes: 3

Related Questions