(One more comment on The One Right Way to autoview :)) |
(autoview tweak to avoid :copen error) |
||
Line 65: | Line 65: | ||
au BufWritePost,BufLeave,WinLeave ?* mkview |
au BufWritePost,BufLeave,WinLeave ?* mkview |
||
au BufWinEnter ?* silent loadview |
au BufWinEnter ?* silent loadview |
||
+ | </pre> |
||
+ | ---- |
||
+ | I still get "E32: No file name" from "au BufWinEnter ?*" when I start vim fresh and do :copen. Triggering with "au BufReadPre" seems to work okay: |
||
+ | <pre> |
||
+ | au BufWritePost,BufLeave,WinLeave ?* mkview |
||
+ | au BufReadPre ?* silent loadview |
||
</pre> |
</pre> |
Revision as of 19:07, 10 July 2010
created 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 loadviewThat 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.
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
Here's the version I have - it matches all files and excludes when opening vim without a file:
au BufWinLeave ?* mkview au BufWinEnter ?* silent loadview
BufWinLeave gives me grief when I'm closing a tab and the next tab is nameless. As noted in the manual, when BufWinLeave triggers % may be different from the buffer being unloaded. This works for me without fail so far:
au BufWritePost,BufLeave,WinLeave ?* mkview au BufWinEnter ?* silent loadview
I still get "E32: No file name" from "au BufWinEnter ?*" when I start vim fresh and do :copen. Triggering with "au BufReadPre" seems to work okay:
au BufWritePost,BufLeave,WinLeave ?* mkview au BufReadPre ?* silent loadview