Vim Tips Wiki
(Added to C and Folding categories)
(not merged yet, foldtext may still be useful)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
+
{{Duplicate|108}}
  +
{{TipImported
{{Tip
 
 
|id=874
 
|id=874
  +
|previous=873
|title=Fold C-style Comments
 
  +
|next=875
|created=February 11, 2005 13:24
+
|created=February 11, 2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=David Vos
 
|author=David Vos
 
|version=6.0
 
|version=6.0
 
|rating=5/4
 
|rating=5/4
  +
|category1=C
|text=
 
  +
|category2=Folding
Do want to make the 10-line /*style*/ comment disappear?
 
 
}}
  +
{{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?
   
You can add folding capability to C-style comments using the command:
+
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
 
   
  +
<pre>
This will work on C, .h, C++, and Java files.
 
 
au BufNewFile,BufRead *.cpp,*.c,*.h,*.java syn region myCComment start="/\*" end="\*/" fold keepend transparent
  +
</pre>
   
 
This will work on C, .h, C++, and Java files.
The "keepend" and "transperent" commands are necessary to avoid overriding the default syntax highlighting of comments.
 
   
 
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
 
   
 
If you want to keep the "/*" beginning of the comment in the folded text, you can use the following function:
The resulting line should look about the same as the default, without removing the comments.
 
}}
 
   
  +
<pre>
== Comments ==
 
 
set foldtext=MyFoldText()
For displaying text in the folded column, there is also a script available, GetFDCText or something like that.
 
 
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
  +
</pre>
   
 
The resulting line should look about the same as the default, without removing the comments.
'''Anonymous'''
 
, February 13, 2005 12:03
 
----
 
How do I use it?<br/>
 
I want to see just code and not the function headers which are in comments.
 
   
 
==Comments==
itsmangesh--AT--gmail.com
 
, March 1, 2005 2:18
 
----
 
<!-- parsed by vimtips.py in 0.548909 seconds-->
 
[[Category:Folding]]
 
[[Category:C]]
 

Latest revision as of 04:45, 12 January 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.

Comments[]