I'm programming mostly in Python/Cython and use foldmethod=syntax (and foldclose=). Overall, this works fairly well, i.e. functions/classes/methods are folded reliably mostly. However, I have one problem: folds are often opend/closed while I'm typing. Specifically,

  • While working on an indented block (usually a long multiline argument list or a long list/dict/... initialization spanning multiple lines), that very block I'm in is folded when I hit return;
  • When starting a string at the end of a block, the block below is unfolded.

This is all very annoying and distracting.

Is there a way to completely disable any auto-folding while I type?

Functionalities I want to retain:

  • Fold/unfold blocks I'm in with a command/keystroke (I'm usually using z-a for both).
  • Fold the whole document with a single keystroke/command.
  • Open new buffers fully folded (though it's not a deal-breaker if I have to do this manually).

Since Python is an Indented language, The easier solution is to set foldmethod=indent. The crux of this solution is when you initialize list(or any code) in multiple lines, then they also get folded in a separate fold which is unwanted.

But vim does have syntax fold support for python out of the box, As suggested below in comment, running :help ft-python-indent gives:

Indent after an open paren:

   let g:pyindent_open_paren = '&sw * 2'      

Indent after a nested paren:

  let g:pyindent_nested_paren = '&sw'   

Indent for a continuation line:

   let g:pyindent_continue = '&sw * 2' 

For more details, check this answer or run :help fold-commands

  • 1
    vim does have out-of-the-box support for python.. see :help ft-python-indent – Mass Mar 13 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.