1

the question is as the title says. I have code that works but not as I expect.

enter image description here

For example, I have a fold here. And another one as well.

enter image description here

These two folds are from the same file. When I save and quit then come back. Only the fold in the first image remains closed. While the fold in the second image still exists, but I have to go over to it and run zc to close it manually.

This is the code I borrowed from solutions here and modified.

local folds_augroup = vim.api.nvim_create_augroup("Folds", { clear=true })
vim.api.nvim_create_autocmd("BufWritePost", {
    group = folds_augroup,
    command = "mkview | filetype detect | set foldmethod=manual"
})

vim.api.nvim_create_autocmd("QuitPre", {
    group = folds_augroup,
    command = "mkview | filetype detect | set foldmethod=manual"
})

vim.api.nvim_create_autocmd("BufWinEnter", {
    group = folds_augroup,
    command = "silent! loadview | filetype detect | set foldmethod=manual | silent! foldclose!"
})

The expected outcome is that

  1. When I make folds they are remembered.
  2. All folds remained closed even if I have them opened from a previous session.

Right now my code only does 1, not 2.

3
  • I strongly recommend against autocommands that make and restore views—one day, you’ll be confused by behavior from an automatically restored view. I’ve seen it dozens of times.
    – D. Ben Knoble
    Feb 11 at 15:10
  • @D.BenKnoble what do you suggest then?
    – YJH16120
    Feb 12 at 3:16
  • Using non-manual folds, typically, or creating and restoring views manually so that it's always and only when you want.
    – D. Ben Knoble
    Feb 12 at 13:13

1 Answer 1

0

I would use the zM command:

local folds_augroup = vim.api.nvim_create_augroup("Folds", { clear=true })
vim.api.nvim_create_autocmd("BufWritePost", {
    group = folds_augroup,
    command = "mkview | filetype detect | set foldmethod=manual"
})

vim.api.nvim_create_autocmd("QuitPre", {
    group = folds_augroup,
    command = "mkview | filetype detect | set foldmethod=manual"
})

vim.api.nvim_create_autocmd("BufWinEnter", {
    group = folds_augroup,
    command = "silent! loadview | filetype detect | set foldmethod=manual | normal! zM"
})
4
  • 1
    I tried the solution but it has the same effect. If there are two folds, only one remains closed when re-opening that file.
    – YJH16120
    Feb 12 at 2:34
  • Thanks for the feedback. I'll investigate further :-/ Feb 12 at 5:45
  • Unfortunately I can't reproduce your problem. The code provided seems to work fine on my machine. Maybe could you give us more precise information about how you reproduce the problem (a step by step approach: What you do, what you get and what you expect)? Feb 15 at 15:59
  • Hmm, that's alright. I'll dig more into and see what I can get. Thanks :-)
    – YJH16120
    Feb 15 at 23:57

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.