Vim Tips Wiki

Fold C-style comments

Redirected from VimTip874

1,624pages on
this wiki
Add New Page
Talk0 Share

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 874 Printable Monobook Previous Next

created February 11, 2005 · complexity intermediate · author David Vos · version 6.0

Do you want to make a 10-line /*C-style*/ comment disappear?

You can add folding capability to C-style comments using the command:

au BufNewFile,BufRead *.cpp,*.c,*.h,*.java syn region myCComment start="/\*" end="\*/" fold keepend transparent

This will work on C, .h, C++, and Java files.

The "keepend" and "transparent" commands are necessary to avoid overriding the default syntax highlighting of comments.

If you want to keep the "/*" beginning of the comment in the folded text, you can use the following function:

set foldtext=MyFoldText()
function MyFoldText()
  let line = getline(v:foldstart)
  let sub = substitute(line, '^[\t ]*', '', '')
  let nlines = v:foldend - v:foldstart + 1
  if strlen(nlines) == 1
    let nlines = " " . nlines
  elseif strlen(nlines) == 2
    let nlines = " " . nlines
  return "+-" . v:folddashes . nlines . ": " . sub

The resulting line should look about the same as the default, without removing the comments.


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki