I added the following code to my .vimrc:
" save and restore folds when a file is closed and re-opened
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
HTML and CSS documents save and restore their folds but code folding is not being saved in my .vimrc
Any suggestions?
EDIT:
The following code solves the problem:
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
but if I write it, the MRU files disappear from my list (and I have to open MRU twice in order to see my list of recent files why?)
The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.
Fold Methodmanual ( :h fold-manual ) — Folds are created manually. indent ( :h fold-indent ) — Lines with equal indent form a fold. expr ( :h fold-expr ) — foldexpr gives the fold level of a line. marker ( :h fold-marker ) — Markers are used to specify folds.
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.
With the following in your vimrc, you can toggle folds open/closed by pressing F9. In addition, if you have :set foldmethod=manual , you can visually select some lines, then press F9 to create a fold. Here is an alternative procedure: In normal mode, press Space to toggle the current fold open/closed.
The problem is that your original autocmd
lines are set to match the pattern *.*
, i.e. any filename which contains some characters, followed by a dot, followed by some more characters.
So the file test.html
or anothertest.css
will be matched, and your command will run, but .vimrc
, which has nothing prior to the dot, will not be matched.
The solution is to set up an autocmd which will match .vimrc
. Your guess of ?*
does match this (because it's looking for any character, followed by any number of other characters), but you say it somehow affects MRUs. I don't know what plugin you're using for your MRUs, but I'm guessing it's one which opens the MRU list in a temporary window with a name that matches the ?* pattern, and the subsequent loading of the view is somehow messing with your MRUs.
Therefore, the fix is to use something a bit more specific to match .vimrc:
autocmd BufWinLeave .vimrc mkview
autocmd BufWinEnter .vimrc silent loadview
It's possible that this will work, too, and is more general:
autocmd BufWinLeave .* mkview
autocmd BufWinEnter .* silent loadview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With