13

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 Nov 16 '15 at 10:01
  • 1
    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 Nov 16 '15 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 Nov 16 '15 at 10:59
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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