我在vim中有两个缓冲区:

:ls
  1 %a   ".vimrc"                       line 1
  2 #h   "script.sh"                    line 1
Press ENTER or type command to continue


如上所示,我在第一个缓冲区中。现在,如果我执行:set softtabstop=16命令,那么我希望它会影响两个缓冲区。但是,情况并非如此-虽然第一个缓冲区的sts值确实发生了更改,并且Tab键插入了四个制表符(ts=4),然后对于第二个缓冲区,sts值仍然是4

:ls
  1 #h + ".vimrc"                       line 1
  2 %a + "script.sh"                    line 1
  softtabstop=4
Press ENTER or type command to continue


为什么会这样?我以为set影响所有缓冲区,而setlocal仅影响活动缓冲区。.

#1 楼

选项可以是全局的,窗口局部的或缓冲区局部的。

当您选择全局选项时,新值是全局的。

当您选择全局选项时,:set新值是本地。

当您对本地选项进行:setlocal时,新值就是本地。

:set是本地缓冲区选项,更改其值只会影响当前值。缓冲区。

选项的“范围”在softtabstop中表示:

'softtabstop' 'sts'    number    (default 0)
                       local to buffer
                       {not in Vi}
Number of spaces that a <Tab> counts[...]


:help option:help local-options:help :setlocal进一步说明了所有这些详细信息。

评论


还有一个:setglobal。

–佐藤桂
16-09-4在7:13

同样,某些选项是全局的,无论您如何设置它们。例如。 “编码”。

– Antony
16年4月4日在20:45

是否可以通过一些vimscript函数调用通过缓冲区ID和选项值设置局部函数?

–圣安东尼奥
19年12月2日在5:08

有一个函数setbufvar可以用来实现这一目标。最初没有找到它。

–圣安东尼奥
19/12/2在5:56

#2 楼

不是专家,@ Martin,但是通过阅读:help set,您需要“取消设置”每个缓冲区中的sts的本地值,以使该缓冲区接收任何新的全局值。 setlocal sts=(或对于布尔标志,setlocal flag<)。 Kinda很有道理,在打开时将本地设置初始化为当前的全局值,然后保留本地值,直到另行告知。如果没有本地设置在起作用,它应该选择任何新的全局值。

再次,不是专家,我可能会猜测第二个缓冲区在以后set。超过50/50的理解是错误的,因为我仍不清楚setsetglobal之间的区别是什么。

#3 楼

在我看来,setsetl都可以。它们全都取决于选项是setgglobal还是两者(local to buffer)。

相关的vim帮助文档:

                            *:se* *:set*
...
Handling of local options           *local-options*

Some of the options only apply to a window or buffer.  Each window or buffer
has its own copy of this option, thus each can have its own value.  This
allows you to set 'list' in one window but not in another.  And set
'shiftwidth' to 3 in one buffer and 4 in another.
...
When editing a new buffer, its local option values must be initialized.  Since
the local options of the current buffer might be specifically for that buffer,
these are not used.  Instead, for each buffer-local option there also is a
global value, which is used for new buffers.  With ":set" both the local and
global value is changed.  With "setlocal" only the local value is changed,
thus this value is not used when editing a new buffer


                            *:setl* *:setlocal*
:setl[ocal] ...     Like ":set" but set only the value local to the
            current buffer or window.  Not all options have a
            local value.  If the option does not have a local
            value the global value is set.


                            *:setg* *:setglobal*
:setg[lobal] ...    Like ":set" but set only the global value for a local
            option without changing the local value.

For buffer-local and window-local options:
    Command      global value       local value ~
      :set option=value      set        set
 :setlocal option=value       -         set
:setglobal option=value      set         -
      :set option?        -            display
 :setlocal option?        -            display
:setglobal option?      display      -


Global options with a local value           *global-local*
Options are global when you mostly use one value for all buffers and windows.
For some global options it's useful to sometimes have a different local value.
You can set the local value with ":setlocal".  That buffer or window will then
use the local value, while other buffers and windows continue using the global
value.
...
You can switch back to using the global value by making the local value empty: >
    :setlocal makeprg=
This only works for a string option.  For a number or boolean option you need
to use the "<" flag, like this: >
    :setlocal autoread<
Note that for non-boolean and non-number options using "<" copies the global
value to the local value, it doesn't switch back to using the global value
(that matters when the global value changes later).  You can also use: >
    :set path<
This will make the local value of 'path' empty, so that the global value is
used.  Thus it does the same as: >
    :setlocal path=


:setl[ocal] {option}<   Set the local value of {option} to its global value by
            copying the value.

:se[t] {option}<    For |global-local| options: Remove the local value of
            {option}, so that the global value will be used.


评论


不完全是。对于本地选项是setg和setl,对于全局选项是setg,对于全局本地对象是setg option = value和setl option =

–马特
20年6月7日在8:33