0

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!                                                                      
   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                                               
 endif
1

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

1

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 '18 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 '18 at 2:19
  • 1
    stuff happens! Leave it around, just in case? – D. Ben Knoble Oct 7 '18 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 '18 at 2:29

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.