Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:15:19+00:00 2026-05-28T05:15:19+00:00

I want to fold function definitions in python code using Vim syntax based folding.

  • 0

I want to fold function definitions in python code using Vim syntax based folding. So I’ve added the following to my .vim/syntax/python.vim :

setlocal foldmethod=syntax
syn sync fromstart
syn region  pythonFunctionFostart="^\z(\s*\)\%(def\|class\) " skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

This works; it also swallows up empty lines between functions (unlike foldmethod=indent), which I want. However, I don’t want the “def function():” line to be folded. As far as I understood the docs I could do the following:

syn region  pythonFunctionFold  start="^\z(\s*\)\%(def\|class\) .*\n\zs." skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

But this causes vim to not create any fold at all. (I’ve tried the start regexp using plain search of course, and it works). Neither works the following:

syn region  pythonFunctionFold  start="^\z(\s*\)\%(def\|class\) .*\n."ms=e skip="^\s*$" end="^\ze\%(\z1\s\)\@!." fold transparent

nor does hs=e, rs=e

Everything I try either includes the def, or doesn’t work at all. ( I guess vim doesn’t like newlines inside my syntax regexps). Is there some simple way to make a syntax fold start the line after a regexp match?

Edit: I also tried the following:

syn match pythonFunctionFold "^\(\s*\)\%(def\|class\) .*\n\zs\(\s*\n\|\1\s\+.*\n\)\+" fold transparent

When I search for the pattern (using /) it matches exactly the regions I want to fold, and “:help syntax” claims support for multiline matches. But I still get no folding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:15:20+00:00Added an answer on May 28, 2026 at 5:15 am

    I fear this may not be possible. From syntax.txt, under :syn-multi-line:

    When using a start pattern with an offset, the start of the match is not
    allowed to start in a following line. The highlighting can start in a
    following line though. Using the “\zs” item also requires that the start of
    the match doesn’t move to another line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long code file with syntax folding in Vim. I know how
I want to push one button in Vim and fold all the code so
I usually edit RUBY files in VIM. I want the methods(def...end) to fold. Could
In the folder function of the Array.Fold operation, i want to look back arbitrary
I have the following in my vimrc: set foldmethod=syntax set foldnestmax=3 set nofoldenable When
I want to edit following code.(This code is come from [http://stackoverflow.com/a/9280414/945688] <!DOCTYPE html> <html>
I want to use some function such as zip , fold and map in
I want to fold a table so that some rows are hidden. I want
want to know why String behaves like value type while using ==. String s1
Want the function to sort the table by HP but if duplicate HPs then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.