Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Fold C-style comments
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Fold C-style comments
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{Duplicate|108}} {{TipImported |id=874 |previous=873 |next=875 |created=February 11, 2005 |complexity=intermediate |author=David Vos |version=6.0 |rating=5/4 |category1=C |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? You can add folding capability to C-style comments using the command: <pre> 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 "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: <pre> 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 </pre> The resulting line should look about the same as the default, without removing the comments. ==Comments==
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Dodgy
(
view source
)
Template:Duplicate
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab