16

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?)

alexchenco
  • 53,565
  • 76
  • 241
  • 413

6 Answers6

15

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 
Rich
  • 7,348
  • 4
  • 34
  • 54
9

Per Jays comments this is the most elegant solution, I have a LOT of plugins and run it on multiple OSes and have just tested it.

autocmd BufWrite * mkview
autocmd BufRead * silent loadview

It does not break MRU and make you have to double query MRU It does not error when you :new into an empty buffer It also doesn't require you to create FileType patern for every file-type you may possibly use.

NOTE: using "loadview" on "BufNewFile" apears to be what confuses MRU, rather pointless trying to render folds on an empty buffer I would have thought??

user381689
  • 101
  • 1
  • 2
  • 3
    This balks during stuff like [less.vim](http://ubuntu-tutorials.com/2008/07/14/use-vim-as-a-syntax-highlighting-pager/) where there is no filename. Also if the file is write-protected (like the help pages!). – pieman72 Jul 22 '14 at 08:17
  • All folds are erased as soon as I save a file. – Isaac Pak Jun 29 '17 at 15:52
2

i had a similar problem. maybe you have to create the directory which holds the data.

mkdir -p ~/.vim/view
chmod 0750 ~/.vim ~/.vim/view
fin
  • 242
  • 3
  • 7
2

With Neovim in Arch Linux, I was getting error messages until I appended a ! after silent (silent!). Here is my ~/.vimrc entry,

autocmd BufWrite * mkview
autocmd BufRead * silent! loadview

Details here:

https://github.com/neovim/neovim/issues/7442#issuecomment-339752054

Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37
1

Add this to the top of your vimrc to make sure the viewdir is present

let &viewdir=expand("$HOME") . "/.bk/.vim/viewdir"
if !isdirectory(expand(&viewdir))|call mkdir(expand(&viewdir), "p", 451)|endif

Then this in your autocmds section:

autocmd BufWrite * mkview
autocmd BufNewFile,BufRead * silent loadview
AskApache Htaccess
  • 1,110
  • 10
  • 9
0

The view details get saved in view file in the vimfiles\view directory. A separate view file is created for every file you edit.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • Are the autocommands working as expected, otherwise? I have my `mkview` set on `BufWrite` instead of `BufWinLeave`, and my `loadview` on `BufNewFile,BufRead` instead of `BufWinEnter`. Not sure if one or the other way is "correct." – Jay Jan 26 '10 at 20:36
  • 2
    ...also, given that a view file is created for every file you modify, you might want to consider narrowing the extensions for which you're saving views. Typically, you don't gain much by saving the view for a file with a recognized syntax for automatic folding. – Jay Jan 26 '10 at 20:40