Lone Learner
Lone Learner

Reputation: 20688

Why are Vim options documented to be "local to buffer" inherited by new buffers?

I start Vim as follows in an empty directory.

vim -u NONE foo.txt

Then I enter the following command in Vim.

:set ts=40

Now if I press tab, indeed the cursor moves to the 41st column.

Now I enter the following command in Vim.

:e! bar.txt

Now if I press tab and the cursor moves again to the 41st column. This surprised me. I was expecting the cursor to move to the 9th column.

In fact, :help 'ts shows the following.

                        *'tabstop'* *'ts'*
'tabstop' 'ts'      number  (default 8)
            local to buffer
    Number of spaces that a <Tab> in the file counts for.  Also see
    |:retab| command, and 'softtabstop' option.

The help says that the ts option is local to buffer. Why then did my ts=40 option set in one buffer is applied to another new buffer?

Upvotes: 2

Views: 1034

Answers (3)

Dan Lowe
Dan Lowe

Reputation: 56637

Here "local to buffer" means that it is possible to set different values per buffer, rather than only setting a value globally. Other options are labeled as "global", which indicates that there is only one value for the entire vim instance, and every buffer must necessarily share that single value. Still others are "local to window", which is like "local to buffer" but at the window scope.

From :help option-summary:

Most options are the same in all windows and buffers. There are a few that are specific to how the text is presented in a window. These can be set to a different value in each window. For example the list option can be set in one window and reset in another for the same text, giving both types of view at the same time. There are a few options that are specific to a certain file. These can have a different value for each file or buffer. For example the textwidth option can be 78 for a normal text file and 0 for a C program.

  • global: one option for all buffers and windows
  • local to window: each window has its own copy of this option
  • local to buffer: each buffer has its own copy of this option

When creating a new window the option values from the currently active window are used as a default value for the window-specific options. For the buffer-specific options this depends on the s and S flags in the cpoptions option. If s is included (which is the default) the values for buffer options are copied from the currently active buffer when a buffer is first entered. If S is present the options are copied each time the buffer is entered, this is almost like having global options. If s and S are not present, the options are copied from the currently active buffer when the buffer is created.

See also :help :setlocal.

Upvotes: 2

romainl
romainl

Reputation: 196781

"Local to buffer" means that the new value won't affect other existing buffers.

Suppose that tabstop is set to 4 in your vimrc and you start vim with two buffers:

$ vim a.txt b.txt

The initial value of tabstop is inherited by both buffers so a.txt and b.txt have tabstop set to 4.

If you do :set tabstop=7 in a.txt, b.txt will still have the previous value of 4. This is what "local to buffer" means.

But when you create a new buffer, that new buffer inherits the settings of the current buffer so you will get a ts=7 in the new buffer if the current buffer has 7, ts=4 if it is 4, and so on.

Then there's :setlocal which sets a local value that won't be inherited by new buffers.

Upvotes: 7

FDinoff
FDinoff

Reputation: 31439

set tabstop=40 set the default to 40. You want to set the local version you need to use setlocal tabstop=40. The local version overrides the global version if it is set.

Upvotes: 1

Related Questions