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

How do I keep :mkview files focused on folds and cursor position without restoring unrelated local options?

Answer

:set viewoptions=folds,cursor,slash,unix

Explanation

mkview and loadview are great for restoring editing context, but the default viewoptions can bring back more state than you actually want. In team settings, this often causes friction because local option changes leak into view files and get re-applied unexpectedly. Tightening viewoptions to only structural context keeps views predictable and portable.

How it works

  • viewoptions controls what :mkview writes and what :loadview restores
  • folds keeps your manual/expr fold state
  • cursor restores cursor line and column
  • slash preserves whether search uses / path separators in patterns
  • unix normalizes file format details in view handling, reducing cross-platform surprises

With this configuration, view files stay focused on navigation context instead of replaying many local settings that may have changed intentionally since the view was saved.

Example

Before tightening viewoptions, loading a view can unexpectedly reapply local state:

- fold state restored (wanted)
- cursor restored (wanted)
- assorted local options restored (often unwanted)

After:

:set viewoptions=folds,cursor,slash,unix
:mkview
' ...later...
:loadview

Result:

- folds come back
- cursor comes back
- fewer unrelated option side effects

Tips

  • Put this in your vimrc if you rely on views daily
  • Pair with :autocmd BufWinLeave ?* mkview and :autocmd BufWinEnter ?* silent! loadview only after confirming the option set fits your workflow
  • If you need additional state, add individual flags instead of restoring everything

Next

How do I run vimgrep across a project using whatever pattern is currently in the search register?