Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Python folding in vim not visually ruin the whitespace?

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?

like image 905
Michael Gundlach Avatar asked Feb 28 '14 17:02

Michael Gundlach


People also ask

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 text in Vim?

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.

What is code folding in Vim?

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.

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.


1 Answers

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()
like image 164
Ingo Karkat Avatar answered Oct 12 '22 14:10

Ingo Karkat