According to the WIKI, scripts can be folded based on syntax. Does anyone have a solution to fold Python docstrings?
Basically, all I need is to fold
""" text between triple quotation marks """, like this:
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
There are a few funky factors at play that we should consider when playing with folds (they are complicated and not easy to understand without digging into the docs..)
syntaxwas my issue when I was fighting fold but lets take a look at the docs for more info..
A fold is defined by syntax items that have the "fold" argument. |:syn-fold|
The fold level is defined by nesting folds. The nesting of folds is limited with 'foldnestmax'.
Be careful to specify proper syntax syncing. If this is not done right, folds may differ from the displayed highlighting. This is especially relevant when using patterns that match more than one line. In case of doubt, try using brute-force syncing:
:syn sync fromstart
The main thing to note is the
sync fromstart this is a useful helper if you have regex that would match throughout the file and only want to catch the header. In your case you should be able to ignore this but just something to be aware of.
""" is fairly predictable we can simplify the
end to look something like this:
syn region foldImports start='"""' end='"""' fold keepend
For both the
end we are just trying to match on
'"""'(or regexp) for the script tags. I did not test the above code out as I'm not much of a python guy but I bet you can get it figured out from here.
Here is a better method which will preserve all highlighting inside triple-quoted strings:
syn region pythonString matchgroup=pythonTripleQuotes \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend fold \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
Add this to
This command is taken exactly from
/usr/share/vim/vim74/syntax/python.vim and only has the
fold keyword added. Why does this preserve highlighting when Dan's answer doesn't? The key is that this has:
Normally, Vim applies only one syntax region, but with
contains= you can tell it that a syntax region can contain other syntax regions.
It's also highlighted as a
String because it does:
HiLink pythonString String
Just defining a syntax region is not enough, you need to link this to a highlight group.