Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: How to keep folds on save?

Tags:

vim

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

like image 203
Mike Graf Avatar asked May 31 '16 18:05

Mike Graf


People also ask

How do I create a fold in Vim?

Vim uses the same movement commands to define folds. Folding also works in visual mode. If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines. Another option is to specify a range in command mode.

How do I expand in Vim?

To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold).

How do I fold a python code in Vim?

It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.


1 Answers

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.

like image 133
lwassink Avatar answered Oct 10 '22 17:10

lwassink