Python’s string-literal juxtaposition makes multi-line strings much easier and prettier to write, but when I’m four or five indents deep and want to use the entire row (leading whitespace does not matter), Vim’s foldmethod=indent breaks down.
For example:
def getQuotation():
print "Fetching quotation from the absolutely useless function."
return ("Four score and seven years ago our fathers brought forth, "
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
should be folded as this:
def getQuotation():
+-- 4 lines: print "Fetching quotation from the absolutely useless function."--
but instead I get this:
def getQuotation():
+-- 2 lines: print "Fetching quotation from the absolutely useless function."--
"upon this continent, a new nation, conceived in liberty, and dedicated "
"to the proposition that \"all men are created equal\"")
I tried setting foldignore=\", but to no avail. Vim’s help foldignore offers this to say on the subject:
Used only when ‘foldmethod’ is “indent”. Lines starting with
characters in ‘foldignore’ will get their fold level from surrounding
lines. White space is skipped before checking for this character.
Is there something obvious that I’m missing, or will I have to resort to foldmethod=expr, base the foldlevel on the indent, and except the corner cases myself?
EDIT: I’ve made at least some headway; it turns out that if I add a non-empty line after the strings and “refresh” the indent with set fdm=indent, then the block folds as it’s supposed to. Even an empty comment (#) is enough.
Short answer: you can’t do this with
foldmethod=indent, but I found something you can use withfoldmethod=expr, so no need to reinvent the wheel. See the long answer.Long answer
Just a brief review of how
foldmethod=indentworks…shiftwidthwith whitespace from the edge of the pagefoldlevelSince the text you have is justified at the edge of the screen, anything involving
shiftwidthis eventually broken unless you hack it up as you did.I looked at several different
.vimrcconfigurations before I found something that would work. For a cleaner solution thanfoldmethod=indent, usefoldmethod=exprwith the~/.vimrcI have below. I found it in jneb’s bitbucket python-fold repoAs a test, I built a few more cases into your example…
Using python-fold at the bottom of my
~/.vimrcyields:And when I hit
zRto unfold:FYI, I use Dmitry Vasiliev’s
python.vimin~/.vim/syntax/python.vimfor python syntax highlights.I copied jneb’s vim script below in case the bitbucket repo disappears…