vim

Vim: How to keep folds on save?


In my current vim setup I have set foldmethod=syntax , however whenever I save my file it refolds anything I had opened. Any ideas?

FWIW this is my current vimrc


Solution

  • This behavior is normal. Vim's default is not to remember which code you had folded vs. unfolded from one session to the next. You can save your current folds; when you finish editing a file, before exiting vim, enter the command :mkview. When you next open the file, if you enter :loadview, it will restore your folds. If you want this to happen automatically, add this code to your vimrc

    augroup remember_folds
      autocmd!
      autocmd BufWinLeave * mkview
      autocmd BufWinEnter * silent! loadview
    augroup END
    

    If you want more features, this plugin does the same thing http://www.vim.org/scripts/script.php?script_id=4021.

    Update: sorry, my original code didn't work. It should work now.