sponsor Vim development Vim logo Vim Book Ad

python_fold : Folding expression for python

 script karma  Rating 58/531, Downloaded by 19360  Comments, bugs, improvements  Vim wiki

created by
Jorrit Wiersma
script type
This script uses the expr fold-method to create folds for python source code.

Folds are created for:
- Class definitions
- def function definitions
- Blocks between {{{, }}} markers (but not yet {{{n, etc.)

The fold level for each fold is equal to the indentation divided by the value of the 'shiftwidth' option, so make sure that 'shiftwidth' is set to the right value for your code.

Class and function folds show a count of the number of lines in the fold and the documentation string when closed (contributed by Max Ischenko).

- The docstring is not shown when it is enclosed in single quotes (like '''comment'''); use double quotes instead (like """comment""")
- Newest version relies on a blank line following class or function definition if the next bit of code is not a new class or function definition.  If this doesn't suit your programming style, use the _nonblank version 2.2 (nb).  That version is buggier, however.  Do not install both files.

- It occasionally inserts a bogus fold at complicated structures like conditionals that are followed by a blank line.  A work-around is to remove the blank line or to replace it with a line containing a comment (a single '#' will do)
install details
Choose either python_fold.vim or python_fold_nonblank.vim (do not use both at the same time!).
Either save it in a file and source that file whenever you want to use the folding, or save it in your ftplugin directory (for example, ~/.vim/ftplugin) and it should load every time you edit python code.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
python_fold.vim 2.3 2005-07-14 6.0 Jorrit Wiersma Added line counts to the foldtext for closed folds.
python_fold.vim 2.2 2004-06-30 6.0 Jorrit Wiersma Fixed bug: fold text now shows "pass" for passed functions and classes.
python_fold_nonblank.vim 2.2 (nb) 2004-06-30 6.0 Jorrit Wiersma Fixed bug: fold text now shows "pass" for passed functions and classes.  This is the version for files without blank lines: see comment on version 2.1 (nb)
python_fold.vim 2.1 2003-10-06 6.0 Jorrit Wiersma Fixed spurious folds problem by relying (somewhat) on blank lines following class and function definitions.  Fix contributed by Tom Schumm.
python_fold_nonblank.vim 2.1 (nb) 2003-10-06 6.0 Jorrit Wiersma Small bugfixes.  The next version will rely somewhat on blank lines following class or function definitions.  Try that one first: it works much better.  If that one doesn't work for you because you use absolutely no blank lines in your code then you can try this one.
python_fold.vim 2.0 2003-05-08 6.0 Jorrit Wiersma Added foldtext contributed by Max Ischenko.  Closed folds now show the documentation string of the class or function if present.
python_fold.vim 1.2 2003-05-08 6.0 Jorrit Wiersma Fixed continuation line detection
python_fold.vim 1.1 2002-12-19 6.0 Jorrit Wiersma Fixed small bug (didn't look for whitespace after def or class)
python_fold.vim 1 2002-12-19 6.0 Jorrit Wiersma Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.