Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Code Folding in Vim for shell scripts working

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?

like image 683
hgiesel Avatar asked Jan 28 '16 14:01

hgiesel


People also ask

How do I create a fold in Vim?

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.

How do I save a fold in Vim?

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.

How do I unfold text in Vim?

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.

How do I fold a python code in Vim?

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.

How do I fold code in Vim?

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.

How to stop VIM from auto-folding when new file opens?

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:

How to fold multi-line comments in Vim?

The default syntax/c.vim provides folding definitions. Unless you define a c_no_comment_fold variable, this also folds multi-line comments.


1 Answers

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.

like image 91
xizdaqrian Avatar answered Oct 13 '22 22:10

xizdaqrian