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
viewoptionscontrols what:mkviewwrites and what:loadviewrestoresfoldskeeps your manual/expr fold statecursorrestores cursor line and columnslashpreserves whether search uses/path separators in patternsunixnormalizes 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 ?* mkviewand:autocmd BufWinEnter ?* silent! loadviewonly after confirming the option set fits your workflow - If you need additional state, add individual flags instead of restoring everything