Is there a way to make vim not close the current fold I'm in when I close a file? For example: I am typing inside a fold, I save and close the file (without presing zc to close the fold), and then when I reopen the file the fold I was in is now closed and I have to open it again and scroll to where I was. Is there any way to prevent this from happening? I would like to be able to open the file and have the fold just how I left it, open, with my cursor in the same place it was. Here is my vimrc for folds and keeping the cursor where is was on the last closing of the file:

 set foldmethod=manual                                                           
 set foldnestmax=10                                                              
 set foldlevel=2                                                                 

 "---- Auto save folding when closing file ----"                                 
 augroup AutoSaveFolds                                                           
   autocmd BufWinLeave * mkview                                                  
   autocmd BufWinEnter * silent loadview                                         
 augroup END  

"---- Jump to last cursor location upon opening file ----"                      

 if has("autocmd")                                                               
   au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")               
     \| exe "normal! g'\"" | endif                                               

3 Answers 3


Since your cursor should be where you left it last, you could try zv to open enough folds to see your cursor (bonus: it doesn’t move the cursor).


Due to some airheadedness combined with looking at different but related questions while answering, I wrote this as if OP didn't already use :mkview. I'd delete it but the thing is I tested it and it preserved folds just fine. So I'm going to leave this for now while I investigate a bit more.

You can use :mkview. This saves various information about the state of a single file, including fold status. The details...

  • You need to have "folds" in the value of 'viewoptions'. It's there by default. Otherwise, :set vop+=folds.
  • Before you close the file: :mkview.
  • After reopening the file: :loadview.

Per the documentation it restores...

manually created folds, opened/closed folds and local fold options

If you want to use this all the time you might consider creating an autocommand for a buffer/file close event and do :mkview there. Similarly for file open + :loadview.

:h :mkview
:h :loadview
:h 'viewoptions'

  • OP already uses mkview...?
    – D. Ben Knoble
    Oct 6, 2018 at 12:59
  • 1
    @D.BenKnoble Holy smokes! I think I was looking at a different but related question when answering. I guess I'll delete this....though I did test it and it preserves folds.
    – B Layer
    Oct 7, 2018 at 2:19
  • 1
    stuff happens! Leave it around, just in case?
    – D. Ben Knoble
    Oct 7, 2018 at 2:26
  • @D.BenKnoble I think I'll reword it to indicate that it should be a viable solution. Thanks.
    – B Layer
    Oct 7, 2018 at 2:29

autocmd BufWritePre * call feedkeys("ggVG") | foldclose | call feedkeys("vv") | mkview

  • 1
    Welcome to Vi and Vim! Your answer provides what appears to be code (use markdown to format the code appropriately), but provides no explanation of how it works or solves the problem (in fact I don’t think it does). Additionally I don’t thing feedkeys is necessary here
    – D. Ben Knoble
    Mar 22, 2022 at 13:51

Your Answer

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

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