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 7590805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:29:46+00:00 2026-05-30T20:29:46+00:00

When setting folds manually, it would be handy if it were possible to set

  • 0

When setting folds manually, it would be handy if it were possible to set the commentstring in a context sensitive manner. Consider the case in which the language uses BCPL-style comment markers (i.e., comments begin with // and are terminated by a newline), the first line of the visual block contains a comment, and the last line does not. Currently, if commentstring is set to //%s, redundant // characters will be appended to the first line when zf is used to create a fold.

Is it possible to set commentstring so that the // characters are added only if they do not already appear on the line?

  • 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-30T20:29:48+00:00Added an answer on May 30, 2026 at 8:29 pm

    According to :help fold-create-marker, automatic folding marker insertion
    does not work properly when:

    • The line already contains a marker with a level number. Vim then doesn’t
      know what to do.
    • Folds nearby use a level number in their marker which gets in the way.
    • The line is inside a comment, commentstring isn’t empty and nested
      comments don’t work. For example with C: adding /* {{{ */ inside
      a comment will truncate the existing comment.

    Thus, it is not possible to change the default zf behavior by modifying the
    commentstring setting.

    However, it is possible to create a custom version of the zf command that
    would take into consideration that starting or ending (or both) lines of
    a fold could have comments. For example, consider the following mappings, one
    for marking a fold by visual selection, another for using with a motion
    command.

    nnoremap <silent> <leader>zf :set opfunc=CreateMarkerFold<cr>g@
    vnoremap <silent> <leader>zf :<c-u>call CreateMarkerFold(visualmode(), 1)<cr>
    function! CreateMarkerFold(vt, ...)
        let range = map(['[<', ']>'], 'line("''".v:val[a:0])')
        let mark = split(&foldmarker, ',')
        let pat = escape(&commentstring, '\')
        let pat = '\V' . substitute(pat, '\\\@<!%s', '\\zs\\ze\\.\\{-}', '')
        for i in [1, 0]
            let line = getline(range[i])
            if line =~ pat
                let line = substitute(line, pat, escape(mark[i], '\'), '')
            else
                let line .= printf(&commentstring, mark[i])
            endif
            call setline(range[i], line)
        endfor
    endfunction
    

    Both of the mappings follow the same routine. Before adding starting and
    ending folding markers, it checks separately whether the first and the last
    lines of the block to fold match the commentstring pattern. For each of the
    two that do match, it inserts the corresponding marker inside the first found
    comment, at the very beginning of its text. Otherwise, the marker is
    decorated according to the commentstring template and added at the end of
    the line.

    If in the latter case it is preferable to separate the marker on its own line,
    one can change the for loop as shown below.

        for i in [1, 0]
            let line = getline(range[i])
            if line =~ pat
                let line = substitute(line, pat, escape(mark[i], '\'), '')
                call setline(range[i], line)
            else
                call append(range[i] - !i, printf(&commentstring, mark[i]))
            endif
        endfor
    

    Unlike the previous version of the loop, the order of processing the two
    lines is important: The ending marker line should be added first, if
    necessary, because inserting a line for the beginning marker would shift the
    following lines changing their numbers.

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

Sidebar

Related Questions

Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
Setting the scene: My asp.net web application carries a version number which is incremented
In my InitWithFrame method of a view I'm setting a tracking area for which
Setting up an integration server, I’m in doubt about the best approach regarding using
Setting up a Windows based web application on Amazon's cloud has definitely been a
Setting up new git installations. On one Windows laptop, I'm running (under cygwin): git
Setting onchange event for CheckBoxList using the following code doesn't work. chkListUserGroup.Attributes.Add(onchange, document.forms[0].isRecordModified.value='true';); How
Setting a Magento store for a winery. They well bottles of wine, but you
Setting up the sandboxes for all these option is not feasible right now. So
Setting up environment variables for hundreds of tests get old very quick. Is there

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.