7

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).
2

Sure, just disable folding in insert mode, and restore it when you leave:

augroup folds
  au!
  au InsertEnter * let w:oldfen = &l:foldenable \| set nofoldenable
  au InsertLeave *
        \ if exists('w:oldfen')
        \   let &l:foldenable = w:oldfen
        \   unlet w:oldfen
        \ endif
augroup END
  • This opens all the folds when you enter Insert mode, which seems like the sort of visual distraction that the question is trying to avoid. How about setting foldmethod=manual during Insert mode instead of nofoldenable? You could also add something like normal zv on InsertLeave to make sure your cursor doesn't get immediately folded into a new block. – djpohly Jan 7 at 21:36
  • I read « disable folding » as, well, disabling it – D. Ben Knoble Jan 7 at 21:37
  • 1
    Fair enough. I've made a separate answer in case anyone is looking for a solution to the alternate reading. :) – djpohly Jan 7 at 22:00
2

If you're looking to completely disable folding (i.e. open all folds) while typing, see D. Ben Knoble's answer.

If you'd just like to prevent Vim from opening and closing folds based on syntax while typing, leaving closed folds closed, check out Vim tip "Keep folds closed while inserting text" or the FastFold plugin. These use a variation on that theme with foldmethod=manual instead of nofoldenable during Insert mode:

augroup folds
  au!
  au InsertEnter * let w:oldfdm = &l:foldmethod | setlocal foldmethod=manual
  au InsertLeave *
        \ if exists('w:oldfdm') |
        \   let &l:foldmethod = w:oldfdm |
        \   unlet w:oldfdm |
        \ endif |
        \ normal! zv
augroup END

The normal! zv at the end serves to ensure that, if you typed something that would result in a new fold, your cursor doesn't get folded into it when you leave Insert mode. You could also experiment with zx, or just remove it if that doesn't matter to you.

  • While this works great for "standard" Vim usage, those of us who prefer evim (i.e., a real graphical editor) won't find much help with this. – Joe Sewell Jan 30 at 20:13
0

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

  • 2
    vim does have out-of-the-box support for python.. see :help ft-python-indent – Mass Mar 13 '19 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.