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

How do I set a Vim option for only the current buffer or window without affecting others?

Answer

:setlocal {option}={value}

Explanation

:setlocal sets an option only for the current buffer or window, leaving all other buffers and windows unaffected. This is essential when you want file-type-specific settings — like different textwidth for prose vs code, or per-project tabstop values — without polluting your global configuration.

How it works

  • :set {option} changes the option globally (affects all buffers/windows going forward)
  • :setlocal {option} restricts the change to the current buffer or window scope
  • Some options are global-only (e.g. history, hlsearch) and :setlocal has no effect on them
  • Options with local scope (like tabstop, wrap, textwidth, number) support :setlocal
  • Use :setlocal? to inspect the local value, :set? for the global value
:setlocal textwidth=80        " only this buffer wraps at 80 cols
:setlocal noexpandtab         " use real tabs here, spaces elsewhere
:setlocal spell               " enable spellcheck in this window only

Example

You have two files open: a Markdown document and a Go source file. You want soft-wrapping and a wide textwidth only for the Markdown file:

" in the Markdown buffer:
:setlocal textwidth=100
:setlocal wrap linebreak

" in the Go buffer — textwidth is still 0 (unlimited), wrap follows global setting

Tips

  • Put :setlocal calls in FileType autocmds (e.g. in ftplugin/markdown.vim) for permanent per-filetype settings
  • :setlocal {option}< resets a local option back to the global value
  • :set {option}^=value and :setlocal {option}^=value prepend to list options; += appends, -= removes items

Next

How do I refer to the matched text in a Vim substitution replacement?