pythonvim

How to fold long docstrings in Python source code in Vim?


What is a method (or perhaps a plugin), that will automatically fold long docstrings in Python? I have docstrings in my code that span several pages, so it is troublesome to keep paging through them.

The other tricky part is that there is embedded Python testing code in the docstrings, so that might make parsing them difficult. I only need to automatically fold the entire docstring, regardless of what is in it.


Solution

  • This is a bit of a dirty hack, but you can go through the python syntax file (:sp $VIMRUNTIME/syntax/python.vim) and find all the syntax regions for triple-quoted strings (search for ''' and """) and add the fold keyword to the end of those statements. Then just set foldmethod=syntax for python files and the comments should be folded.