I have created a ftplugin/html.vim file with a custom fold expression.

On my .vimrc file I have the following:

set foldmethod=syntax "all files use syntax fold method
autocmd Syntax html set foldmethod=expr foldexpr=HTMLFolds() "html files use custom fold method

However when opening an index.html and then another file, let's say app.js and then toggling between the files with bp and bn.

The fold method is never kept and somehow reset to manual

Is there any way to keep the foldmethod for each buffer when navigating files in vim so the foldmethod is kept always the one it should be?

behaviour example


  • Can you check all the times that the Syntax event is being called ? This might help you. You could have an insight of why the syntax is reset ... – nobe4 Aug 27 '15 at 12:15
  • strange, when using your the example file as my .vimrc look what happened when trying to edit index.html file. asciinema.org/a/ej8meo7k8bcgpyeiifbbhnv0k – zanona Aug 27 '15 at 21:15
  • never happened to me before, did you try just sourcing the file with your normal vimrc? – nobe4 Aug 27 '15 at 21:48
  • @Nobe4 weird, isn't it? I have also appended the code provided to my .vimrc file, but it had the same result I'm afraid. :( Perhaps my vim installation is broken? I simply installed on mac through brew install vim – zanona Aug 28 '15 at 8:32
  • This is indeed strange. Have you tried without your vimrc, and only with the debug script as your vimrc file ? vim -u log.vim -N – nobe4 Aug 28 '15 at 8:35

Since you already have ftplugin/html.vim you should simply add these lines to it and keep your vimrc clean:

setlocal foldmethod=expr
setlocal foldexpr=HTMLFolds()

Also, you should probably make HTMLFolds() local to your script:

function! s:HTMLFolds()

and call it like that:

setlocal foldexpr=s:HTMLFolds()
  • weird, if I add the s: flag to the method name and foldexpr call as suggested the folds are not created anymore? – zanona Aug 27 '15 at 21:06

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.