4

I'm using manual folds. I issued the command :mkview before I exited a file just now, and found that, when I reopened it, the folds appear to be preserved as I left them.

But when I issue the command :saveas final.v2 or :w final.v2, and then reopen, the folds are all gone.

How do I preserve the folds when I save under a different filename?

Also, when I reopen the original file, the folds appear to be preserved as I left them, even though I didn't then issue the command :loadview to restore the folds to the newly reopened file. Why is this happening?

1 Answer 1

0

and then reopen, the folds are all gone

I'm guessing by "reopen", you meant opening the newly saved file. If so, you need to issue loadview from the new buffer's window. The workflow would look like this:

function! SaveAs(filename) abort
  let orig_filename = getbufinfo(0)[0].name
  exe 'f' a:filename
  exe 'w!'
  mkview
  exe 'f' orig_filename
endfunction
command! -nargs=1 -complete=file SaveAs call SaveAs(<q-args>)

You can now use SaveAs newfilename to save the current buffer in a new file with the fold information properly saved.


Also, when I reopen the original file, the folds appear to be preserved as I left them, even though I didn't then issue the command :loadview to restore the folds to the newly reopened file. Why is this happening?

You or some plugin have probably set an autocommand that issues :loadview on BufEnter. Try running with vim --clean and see if vim still loads the folds without you having to issue :loadview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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