History
Article Edit this page Discussion

Make views automatic

From Vim Tips Wiki

(Redirected from VimTip991)
Jump to: navigation, search

Tip 991 Previous Next Created: September 13, 2005 Complexity: basic Author: manalive Version: 6.0


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:

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.

[edit] Comments

This really should be:

autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent loadview

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 *.*


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.


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

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

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto Wiki
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Make views automatic"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation