7

Whatever I do, I don't seem to get syntax folding running for shell scripts.

So I have a file called abc.sh call

:let g:sh_fold_enabled=7
:let g:is_bash=1
:set foldmethod=syntax

But it still can't find any folds. Why is that?

hgiesel
  • 5,430
  • 2
  • 29
  • 56
  • 1
    With `foldmethod=syntax`, the folds are defined by syntax items with the "fold" argument. If your syntax file doesn't define them as such, you'll get no folds. – Michael Foukarakis Jan 28 '16 at 14:34
  • What version of vim? What does `:set ft?|filetype` output in vim with that file loaded? – Etan Reisner Jan 28 '16 at 15:46
  • `VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan 23 2016 14:27:29)`, `set ft` results in `sh`. I can't believe there is no builtin support for shell scripts, I mean there is even support for python or ruby, but not for shell scripts? – hgiesel Jan 28 '16 at 20:09

1 Answers1

10

This problem is solved in a discussion on Reddit.

The trick is to put those commands into your vimrc at the top.

set nocompatible

filetype plugin indent on

set foldenable

set foldmethod=marker

au FileType sh let g:sh_fold_enabled=5

au FileType sh let g:is_bash=1

au FileType sh set foldmethod=syntax

syntax enable

IMO fold-level 5 works better than 7, so that's what I put into the code above.

I put the above just after my plugins section.

This question is also discussed, with a slightly different solution, in the following post: Here.

xizdaqrian
  • 716
  • 6
  • 10
  • It seems a key detail of the above is to make sure `filetype plugin indent on` runs before `syntax enable`. Folding silently fails if reversing their order. A possible way to enforce that in more complex configurations might be to use something like `au FileType sh syntax enable` for every applicable filetype, rather than enabling syntax highlighing globally. – sampi Mar 23 '23 at 09:28