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?
Vim uses the same movement commands to define folds. Folding also works in visual mode. If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines. Another option is to specify a range in command mode.
The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.
Use zR to open all folds. The command zm gives more folding by closing one more level of folds throughout the whole buffer. Use zM to close all folds.
It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.
I type z + a and z + c and z + o and nothing happens. Here is an article about folding: Code folding in Vim. Vim's default folding method is manual meaning that the folds are created manually; otherwise, there is no fold to be closed or opened using z a, z o, or z c as you described.
Now, everytime you open a file with vim, you can see the code is folded by the method you was set. Then you can use za, zc, zo. If anyone wants to change default fold method and stop auto-folding when new file opens, Add this code in your vim config file below:
The default syntax/c.vim provides folding definitions. Unless you define a c_no_comment_fold variable, this also folds multi-line comments.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With