Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #991 - Make views automatic

Created: September 13, 2005 8:52 Complexity: basic Author: manalive Version: 6.0 Karma: 9/7 Imported from: Tip#991

+


Hey folks,


As you know, you can use :mkview to save folds and such when you close a file--but you have to use :loadview next time you use the file.


With two lines in your .vimrc, you can make that automatic.


I got all excited to post this, then found a tip that already mentioned it, back in 2001:

VimTip122 "Skip blank lines when folding text." As he put it:


[snip]

And as an added bonus, for those new to text folding, add this to your .vimrc file too:


autocmd BufWinLeave *.* mkview

autocmd BufWinEnter *.* silent loadview


That way whatever folds you set won't get lost when you quit. I had that happen after spending 15 minutes folding up a 3000+ line file. Happy vimming!

[/snip]


There you go.

Comments

This really should be:

autocmd BufWinLeave * mkview autocmd BufWinEnter * silent loadview

heptite at yahoo dot com , September 13, 2005 14:25


`autocmd BufWinLeave *.* mkview! autocmd BufWinEnter *.* silent loadview

*.* is better for me than using just *, as when I load vim it defaults to [No File], which of course triggers the BufWinEnter, and since there is no file name, an error occurs as it tries to execute. 

The error does not appear when using *.*

thulsey--AT--yahoo.com , September 13, 2005 19:32


When I first wrote that I intended to only use folds in source code files, which almost always conform to the *.* pattern. Though it is true, as heptite points out that * would be more general purpose, it would work on files without the dotted notation, notably lots of shell scripts are like that. So use which ever is best for you.

BTW, since I made that tip several years ago, I have found that setting foldmethod to "marker" is more versatile. Though there are often reasons why not to use markers, it works better for me.

Nice to see people geting use from tips I wrote 4 years ago. :)

brailsmt--AT--yahoo.com , September 14, 2005 10:22


For me, this following lines work quite well for all files

autocmd BufWinLeave * if expand("%") != "" | mkview | endif 
autocmd BufWinEnter * if expand("%") != "" | loadview | endif 


irwan.djajadi at gmail.com , June 8, 2006 13:44


Advertisement