vimtricks.wiki Concise Vim tricks, one at a time.

How do I change an option's global default without affecting the current buffer's local value?

Answer

:setglobal

Explanation

Vim maintains two values for most options: a global default that applies to new windows and buffers, and a local copy that can be overridden per-buffer or per-window. The :setglobal command modifies only the global default, leaving the current buffer's local value untouched.

How it works

Vim has three flavors of :set:

Command Effect
:set {opt}={val} Sets both global default and local value
:setlocal {opt}={val} Sets only the local (buffer/window) value
:setglobal {opt}={val} Sets only the global default (new buffers/windows)

To query which value you're reading, append ?:

:setglobal tabstop?    " show the global default
:setlocal tabstop?     " show this buffer's local value

Example

You're in a Python file where a plugin has set tabstop=4 locally. You want future buffers to use tabstop=2 without disturbing your current Python file:

:setglobal tabstop=2

The current buffer still uses tabstop=4. New buffers opened afterward will inherit tabstop=2.

Tips

  • Particularly useful in ftplugin/ files: use :setlocal inside ftplugins so you don't silently change the global default for all other buffers
  • :set {opt}&g resets the global value to the compiled default while preserving any local override
  • Use :verbose set tabstop? to trace where the current effective value came from

Next

How do I highlight all occurrences of a yanked word without typing a search pattern?