Mike Graf
Mike Graf

Reputation: 5317

Vim: How to keep folds on save?

In my current vim setup I have set foldmethod=syntax , however whenever I save my file it refolds anything I had opened. Any ideas?

FWIW this is my current vimrc

Upvotes: 36

Views: 22155

Answers (5)

dudozer_maks
dudozer_maks

Reputation: 83

For those who use NeoVim:

vim.api.nvim_create_autocmd({"BufWinLeave"}, {
  pattern = {"*.*"},
  desc = "save view (folds), when closing file",
  command = "mkview",
})
vim.api.nvim_create_autocmd({"BufWinEnter"}, {
  pattern = {"*.*"},
  desc = "load view (folds), when opening file",
  command = "silent! loadview"
})

First 5 lines are saying: When closing buffer (file or other buffers), do: if buffer name follows pattern ".", save view (all folds). Another 5 is doing the same job, but backwards: load view (all folds) when opening the file.

When you open file for the first time, you may get an error, because you have no views saved. To prevent this from happening, I inserted silent! in loading view autocmd

Upvotes: 6

josh anyan
josh anyan

Reputation: 381

augroup remember_folds
  autocmd!
  autocmd BufWinLeave *.* mkview
  autocmd BufWinEnter *.* silent! loadview
augroup END

if you're having issues getting the folding to work with something like Telescope or other plugins that create buffers (windows, dialogs, etc), the above seems to work for me so far.

It basically requires a period in the name of the buffer (foo.sh, script.py, server.js, etc) to trigger. The dynamic buffers from things like Telescope don't seem to match that pattern.

Upvotes: 5

sampi
sampi

Reputation: 872

Saving a file should definitely not cause Vim to reset folding. However, some autocmd on e.g. BufWritePost actions might trigger such behavior.

More specifically vim-go has an open bug which causes this issue with golang files. The bug's comments lists a couple of workarounds. Setting g:go_fmt_experimental = 1 works for me.

Upvotes: 5

Adam
Adam

Reputation: 3461

I am not a vim config ninja, but I hacked various solutions to achieve this, which works for me on nvim/Neo Vim without throwing errors.

augroup remember_folds
  autocmd!
  au BufWinLeave ?* mkview 1
  au BufWinEnter ?* silent! loadview 1
augroup END

Upvotes: 9

lwassink
lwassink

Reputation: 1691

This behavior is normal. Vim's default is not to remember which code you had folded vs. unfolded from one session to the next. You can save your current folds; when you finish editing a file, before exiting vim, enter the command :mkview. When you next open the file, if you enter :loadview, it will restore your folds. If you want this to happen automatically, add this code to your vimrc

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

If you want more features, this plugin does the same thing http://www.vim.org/scripts/script.php?script_id=4021.

Update: sorry, my original code didn't work. It should work now.

Upvotes: 64

Related Questions