Vim Tips Wiki
No edit summary
 
(Added to C and Folding categories)
Line 10: Line 10:
 
|text=
 
|text=
 
Do want to make the 10-line /*style*/ comment disappear?
 
Do want to make the 10-line /*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
 
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.
 
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 "transperent" 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:
 
 
If you want to keep the "/*" beginning of the comment in the folded text, you can
 
 
use the following function:
 
 
 
 
 
set foldtext=MyFoldText()
 
set foldtext=MyFoldText()
 
 
function MyFoldText()
 
function MyFoldText()
 
let line = getline(v:foldstart)
 
 
let sub = substitute(line, '^[\t ]*', '', '')
let line = getline(v:foldstart)
 
 
let nlines = v:foldend - v:foldstart + 1
 
 
if strlen(nlines) == 1
let sub = substitute(line, '^[\t ]*', '', '')
 
 
let nlines = " " . nlines
 
 
elseif strlen(nlines) == 2
let nlines = v:foldend - v:foldstart + 1
 
 
let nlines = " " . nlines
 
 
endif
if strlen(nlines) == 1
 
 
return "+-" . v:folddashes . nlines . ": " . sub
 
let nlines = " " . nlines
 
 
elseif strlen(nlines) == 2
 
 
let nlines = " " . nlines
 
 
endif
 
 
return "+-" . v:folddashes . nlines . ": " . sub
 
 
 
endfunction
 
endfunction
 
 
   
 
The resulting line should look about the same as the default, without removing the comments.
 
The resulting line should look about the same as the default, without removing the comments.
Line 63: Line 36:
   
 
== Comments ==
 
== Comments ==
For displaying text in the folded column, there is also a script
+
For displaying text in the folded column, there is also a script available, GetFDCText or something like that.
available, GetFDCText or something like that.
 
   
 
'''Anonymous'''
 
'''Anonymous'''
 
, February 13, 2005 12:03
 
, February 13, 2005 12:03
 
----
 
----
How do I use it.
+
How do I use it?<br/>
 
I want to see just code and not the function headers which are in comments.
 
I want to see just code and not the function headers which are in comments.
 
   
 
itsmangesh--AT--gmail.com
 
itsmangesh--AT--gmail.com
Line 77: Line 48:
 
----
 
----
 
<!-- parsed by vimtips.py in 0.548909 seconds-->
 
<!-- parsed by vimtips.py in 0.548909 seconds-->
  +
[[Category:Folding]]
  +
[[Category:C]]

Revision as of 12:40, 27 July 2007

Previous TipNext Tip

Tip: #874 - Fold C-style comments

Created: February 11, 2005 13:24 Complexity: intermediate Author: David Vos Version: 6.0 Karma: 5/4 Imported from: Tip#874

Do want to make the 10-line /*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 "transperent" 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

For displaying text in the folded column, there is also a script available, GetFDCText or something like that.

Anonymous , February 13, 2005 12:03


How do I use it?
I want to see just code and not the function headers which are in comments.

itsmangesh--AT--gmail.com , March 1, 2005 2:18