8

Is there a way to make Vim fold functions based on the syntax of a function declaration?

For example having vim turn a function like this:

def foobar(foo,bar):
  if foo > bar:
    print "foo"
  elif foo < bar:
    print "bar"
  elif foo == bar:
    print "foobar"

Into this:

+----- 7 lines: def foobar(foo,bar): -------------------------------------------------------------

Is there some set of commands or a function I can put into my .vimrc file use to accomplish this?

12

Given that your example is in Python, which relies on correct indentation of code blocks it is enough to base folding on that:

:set foldmethod=indent

For more information on the varieties of folding available see :help foldmethod

7

Sure:

set foldmethod=syntax

You can then use zc to close a fold, zo to open one, or za to toggle.

Unfortunately, Vim doesn't include folding information for Python by default, which you appear to be using. You could use one of many external resources, however.

  • 3
    In order for this to work the syntax file you are using should make use of syn-fold. in my installation of vim74, only about 10% of the syntax files use it. – jalanb Feb 6 '15 at 0:15

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.