Wikia

Vim Tips Wiki

Changes: Fold C-style comments

Edit

Back to page

(Merged with 108, Content is deprecated.)
(not merged yet, foldtext may still be useful)
 
Line 1: Line 1:
{{merged|108}}
+
{{Duplicate|108}}
{{Deprecated|Comment syntax folding is included by default in the c.vim syntax file.}}
 
 
{{TipImported
 
{{TipImported
 
|id=874
 
|id=874
Line 12: Line 12:
 
|category2=Folding
 
|category2=Folding
 
}}
 
}}
  +
{{Dodgy|Comment syntax folding is included by default in the c.vim syntax file, but the custom foldtext function may still be useful}}
 
Do you want to make a 10-line /*C-style*/ comment disappear?
 
Do you want to make a 10-line /*C-style*/ comment disappear?
   

Latest revision as of 04:45, January 12, 2011

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
  endif
  return "+-" . v:folddashes . nlines . ": " . sub
endfunction

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

CommentsEdit

Around Wikia's network

Random Wiki