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

How do I control what state gets saved when I use :mkview to snapshot a window?

Answer

:set viewoptions=cursor,folds,slash,unix

Explanation

The viewoptions setting is a comma-separated list that determines exactly what :mkview (and the auto-save view pattern) stores in a view file. The default value includes options, which saves all local option overrides — a frequent source of confusing bugs when views go stale.

How it works

  • :mkview writes a script to viewdir capturing the items listed in viewoptions
  • :loadview re-applies that script to restore the saved state
  • The options item (included by default) saves every local option value, including foldmethod, filetype, and others set by your filetype plugin
  • If a view file is stale, it can silently re-apply an old foldmethod=manual, overriding the one your ftplugin sets

Recommended configuration

For most users, saving only cursor position and folds is sufficient:

set viewoptions=cursor,folds,slash,unix

Or, to surgically remove just the problematic item while keeping defaults:

set viewoptions-=options

Example

A common auto-save pattern:

autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent! loadview

Without set viewoptions-=options, this can silently override foldmethod and other settings your filetype plugin configures on every file open.

Tips

  • Inspect current value: :set viewoptions?
  • Available items: cursor, folds, slash, unix, options, localoptions, curdir
  • slash and unix are recommended for cross-platform compatibility with view files

Next

How do I enable matchit so % jumps between if/else/end style pairs?