7

Following this good piece of advice, I have added these lines to my .vimrc.

augroup AutoSaveFolds
  autocmd!
  autocmd BufWinLeave * mkview
  autocmd BufWinEnter * silent loadview
augroup END

It does work and my folds are saved. But I get this error now each time I open a unnamed buffer with;

:tabnew<cr>

or each time I navigate to help with

:tabhelp vim<cr>

with the following message:

Error detected while processing BufWinEnter Auto commands for "*":
E32: No file name

Why do I get this?
How to prevent it while still getting my folds automately saved?

1
  • I have no any idea why you needed to use tabs in vim, but i think that error arise when you create an unnamed buffer. Just try to open new tab like this ':tabnew ~/.vimrc' no any errors, if that file exactly exist. Check the doc vimdoc.sourceforge.net/htmldoc/starting.html#:mkview
    – Alex Kroll
    Oct 8 '17 at 21:29
21

Switching to ?* as the regex will no longer match empty file names. I actually recommend you switch to the following, which is a corresponding block of .vimrc that has been tweaked to suppress many of the errors you are likely to encounter when following the referenced advice.

augroup AutoSaveGroup
  autocmd!
  " view files are about 500 bytes
  " bufleave but not bufwinleave captures closing 2nd tab
  " nested is needed by bufwrite* (if triggered via other autocmd)
  " BufHidden for compatibility with `set hidden`
  autocmd BufWinLeave,BufLeave,BufWritePost,BufHidden,QuitPre ?* nested silent! mkview!
  autocmd BufWinEnter ?* silent! loadview
augroup end

The BufWritePost event (+ nested) can be left out if you prefer, though you will experience no penalty for leaving it in.

Furthermore, I use

set viewoptions=folds,cursor
set sessionoptions=folds
3
  • 2
    Works perfectly, with nice additional tweaking options and explanations. Cheers :)
    – iago-lito
    Oct 9 '17 at 12:31
  • This appears to work perfectly with tabs. This might be a nice addition to the vim wikia site.
    – Jay Dorsey
    Oct 31 '18 at 16:44
  • FWIW automatically restoring folds this way has caused some confusion for folks who are tweaking fold settings and not seeing any changes
    – D. Ben Knoble
    Jul 12 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.