Today I learned how to fold/unfold in Vim; it's quite handy.

Unexpectedly, when I exit a file and reenter it, the folds I created are gone (unfolded).

In our company's large project there are quite a lot of sticky (legacy) code blocks which are kind of obsolete but indispensable.

I really need to fold them nearly permanently.

Is there a brief way to tell Vim to save my folding preferences for a file?

  • 1
    I don't know if it will solve your problem, but maybe the :mkview and :loadview commands could help. You could try adding these 2 lines inside your vimrc au BufWinLeave *.c mkview and au BufWinEnter *.c silent loadview and adapt them to your specific needs. These are autocommands that will be automatically executed whenever you leave a buffer for the first one, and when you enter a buffer for the second one, but only for C files. I've copied them from the help of the :mkview command (:help :mkview).
    – saginaw
    Commented Nov 16, 2015 at 10:01
  • 2
    According to the help, it should restore folds when using manual folding and the option 'viewoptions' contains folds. If you want the autocommands to be executed for other filetypes, you can change the autocommands. For example, if you want to add python files, you could change *.c to *.c,*.py.
    – saginaw
    Commented Nov 16, 2015 at 10:04
  • I'm sorry, I was wrong when I said that the autocommands would be executed whenever you enter / leave a buffer. The first one will be executed after a buffer is displayed in a window, the second one will be executed before a buffer is removed from a window.
    – saginaw
    Commented Nov 16, 2015 at 10:59

3 Answers 3


I do exactly this for one file I refer to a lot NOTES.otl I performed the following in my .vimrc:

" Useful for my Quick Notes feature in my tmuxrc
augroup QuickNotes
  au BufWrite,VimLeave NOTES.otl mkview
  au BufRead           NOTES.otl silent loadview
augroup END

What this means is that in your case you can handle manual folds using the :mkview and :loadview commands.


An easy to fold the legacy codes permanently is to use the foldmethod of "marker".

You can enclose the codes with the markers "{{{" and "}}}", and add a modeline to the file, like:

    /* vim: set foldmethod=marker : */
  • Great. Using 'foldmethod'=marker is an alternative approach to 'foldmethod'=manual. Note that the markers {{{, }}} will be included with the file.
    – husB
    Commented Jun 26, 2023 at 3:01

A more general approach (not just for QuickNotes but for all files) would be as follows:

" Save and restore manual folds when we exit a file
augroup SaveManualFolds
    au BufWinLeave, BufLeave ?* silent! mkview
    au BufWinEnter           ?* silent! loadview
augroup END

For more information, see the view-sessions, section with includes the mkview and loadview commands.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.