When I fold Python code in vim, the fold text always starts in column zero. This is visually noisy since Python has significant whitespace -- it looks like top-level code when I glance through the file.
Is there a setting to tell the foldtext to indent itself to the level of the first line of code being folded, short of rewriting the foldtext() method?
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.
Open it in Vim, and place the cursor at the beginning of a paragraph. Make sure you're in normal mode, and type zf2j . After you press j , Vim will create a fold covering three lines — the line you started the fold on, and the next two lines.
Yanis. March 15, 2020. Folding is a way to hide a chunk of text, and thus unclutter the view. That comes handy when you need to focus on some other part of code, or if you want to get a quick overview of the whole file.
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.
The way to influence this is through the 'foldtext'
option.
Here's a simple example to get you started:
:setlocal foldtext=repeat('\ ',indent(v:foldstart)).foldtext()
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