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

  • Home
  • SEARCH
  • 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 4615118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:47:38+00:00 2026-05-22T01:47:38+00:00

I’d like to set two methods for folding :set foldmethod=indent and retain all its

  • 0

I’d like to set two methods for folding

  1. :set foldmethod=indent and retain all its features
  2. hiding comments with

    :set foldmethod=marker

    :set foldmarker=/*,*/

I found out that this is not possible. Is it possible to achieve the desired folding and set this in .vimrc or to use some script or plugin for this?

  • 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-22T01:47:39+00:00Added an answer on May 22, 2026 at 1:47 am

    It’s not possible to have different foldmethod types in same buffer. How is Vim to know that there are some comments at same indent level as other text that you want to treat as being of a different (higher numbered) level?

    I’m sure you can achieve what you want by setting foldmethod to ‘expr’. This is most flexible way of doing folds in Vim but can get complicated (and/or slow) depending on what you want. I think it would work fairly easily for your use case, though.

    First, somewhere in your vimrc or vimscripts you need to make sure that foldexpr is getting defined for the filetype in question.

    set foldexpr=MyFoldLevel(v:lnum)
    set foldmethod=expr
    " and for last code example
    let b:previous_level = 0
    

    and you then have to flesh out your foldexpr function so that it assigns levels in a way that results in behavior you want. Something like the code below might come close to working in cases where each comment line has prefix symbol (i.e., not in your case), but I expect it needs some tweaks. h: fold-expr would be a good place to look for help:

    function! MyFoldLevel(linenum)
       " assign levels based on spaces indented and tabstop of 4
       let level = indent(a:linenum) / 4
       if getline(a:linenum) =~ [put line-based comment prefix pattern here]
           let level = 20
       endif
    endfunction
    

    Would need to be modified to assign higher level for lines between comment start and end markers the way you want:

    function! MyFoldLevel(linenum)
       let linetext = getline(a:linenum)
       if linetext =~ [put line-based comment prefix pattern here]
           let level = 20
       elseif linetext =~ '^\s*/\*'
           let level = 20
       elseif linetext =~ '^\s*\*/'
           let level = 21
       else
           if b:previous_level == 20
               let level = 20
           else
               "assuming code is space-indented with tabstop of 4
               let level = indent(a:linenum) / 4
           endif
       endif
    
       let b:previous_level = level
       return level
    
    endfunction
    

    I don’t expect the foldmethod functions I’ve written would work exactly as written. But they do point the way to something that would work.

    Note that use of level of ’20’ for comments is just arbitrary level that allows them to be folded while all (presumably lower-leveled) indented code could be visible. ’21’ for last line of comment section is just to differentiate it from the previous lines of comments that have level of 20, to know that next line should be treated as regular line of code.

    Also, key ops like ‘zc’ and ‘zo’ will not work quite right on comments when they’re set to level much higher than surrounding code. Would want to use direct command like :set foldlevel=21 to show all comment lines.

    Not pretty, and I expect it could be simplified a little, but something like this is what I think is required for what you want.

    Actually, thinking through this a little more, I think you would want the first line of any comment blocks to be at same level as if it were a non-comment line, only subsequent comment lines in same block would need to be of higher level to have them “fold” into the starting comment line. In the code I gave, if it works or comes close to working at all, I think vim would fold all the comment lines behind the preceding non-comment line, which isn’t what you want, but I unfortunately don’t have more time to devote to this little puzzle. . . I’ve done this sort of custom-folding quite a few times and generally always have a little bit of trial and error in getting exactly what I want.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but

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.