Vim Tips Wiki
mNo edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
 
{{review}}
 
{{Tip
 
 
|id=639
 
|id=639
  +
|previous=638
|title=Comment highlight __HASH__ifdef DEBUG for code-read ease (C/C__PLUS____PLUS__)
 
  +
|next=640
|created=January 20, 2004 1:11
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Karthick Gururaj
 
|author=Karthick Gururaj
 
|version=6.0
 
|version=6.0
 
|rating=8/8
 
|rating=8/8
  +
|category1=C
|text=
 
  +
|category2=C++
Hi all,
 
 
}}
 
If your C/C++ code is scattered with statements like
  +
<pre>
 
#ifdef DEBUG
 
// Some code..
 
cout << "Debug output: blah" << endl;
 
#endif
  +
</pre>
   
 
and you would like to highlight these segments in a different colour (so that you can skip them visually), add the following code in your .vimrc (colouring follows that of comments)
   
  +
<pre>
 
syn region MySkip contained start="^\s*#\s*\(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*#\s*endif\>" contains=MySkip
   
 
let g:CommentDefines = ""
If your C/C++ code is scattered with statements like
 
   
 
hi link MyCommentOut2 MyCommentOut
&#35;ifdef DEBUG
 
 
hi link MySkip MyCommentOut
 
hi link MyCommentOut Comment
   
 
map <silent> ,a :call AddCommentDefine()<CR>
// Some code..
 
 
map <silent> ,x :call ClearCommentDefine()<CR>
   
 
function! AddCommentDefine()
cout &lt;&lt; "Debug output: blah" &lt;&lt; endl;
 
 
let g:CommentDefines = "\\(" . expand("<cword>") . "\\)"
 
syn clear MyCommentOut
 
syn clear MyCommentOut2
 
exe 'syn region MyCommentOut start="^\s*#\s*ifdef\s\+' . g:CommentDefines . '\>" end=".\|$" contains=MyCommentOut2'
 
exe 'syn region MyCommentOut2 contained start="' . g:CommentDefines . '" end="^\s*#\s*\(endif\>\|else\>\|elif\>\)" contains=MySkip'
 
endfunction
   
 
function! ClearCommentDefine()
&#35;endif
 
 
let g:ClearCommentDefine = ""
 
syn clear MyCommentOut
 
syn clear MyCommentOut2
 
endfunction
  +
</pre>
   
 
To see the effect, position the cursor on the word DEBUG in the C code snippet above and type <code>,a</code>
   
  +
==Related Plugins==
  +
* {{script|id=7|text=ifdef plugin}}
   
 
==Comments==
and you would like to highlight these segments in a different colour (so that
 
 
you can skip them visually), add the following code in your .vimrc (colouring
 
 
follows that of comments)
 
 
 
 
CODE STARTS
 
 
syn region MySkip contained start="^\s*&#35;\s*\(if\&gt;\|ifdef\&gt;\|ifndef\&gt;\)" skip="\\$" end="^\s*&#35;\s*endif\&gt;" contains=MySkip
 
 
 
 
let g:CommentDefines = ""
 
 
 
 
hi link MyCommentOut2 MyCommentOut
 
 
hi link MySkip MyCommentOut
 
 
hi link MyCommentOut Comment
 
 
 
 
map &lt;silent&gt; ,a :call AddCommentDefine()&lt;CR&gt;
 
 
map &lt;silent&gt; ,x :call ClearCommentDefine()&lt;CR&gt;
 
 
 
 
function! AddCommentDefine()
 
 
let g:CommentDefines = "\\(" . expand("&lt;cword&gt;") . "\\)"
 
 
syn clear MyCommentOut
 
 
syn clear MyCommentOut2
 
 
exe 'syn region MyCommentOut start="^\s*&#35;\s*ifdef\s\+' . g:CommentDefines . '\&gt;" end=".\|$" contains=MyCommentOut2'
 
 
exe 'syn region MyCommentOut2 contained start="' . g:CommentDefines . '" end="^\s*&#35;\s*\(endif\&gt;\|else\&gt;\|elif\&gt;\)" contains=MySkip'
 
 
endfunction
 
 
 
 
function! ClearCommentDefine()
 
 
let g:ClearCommentDefine = ""
 
 
syn clear MyCommentOut
 
 
syn clear MyCommentOut2
 
 
endfunction
 
 
 
 
CODE ENDS
 
 
 
 
To see the effect, position the cursor on the word DEBUG in the C code snippet above and type ,a
 
}}
 
 
== Comments ==
 
<!-- parsed by vimtips.py in 0.268181 seconds-->
 

Latest revision as of 05:40, 13 July 2012

Tip 639 Printable Monobook Previous Next

created 2004 · complexity basic · author Karthick Gururaj · version 6.0


If your C/C++ code is scattered with statements like

#ifdef DEBUG
 // Some code..
 cout << "Debug output: blah" << endl;
#endif

and you would like to highlight these segments in a different colour (so that you can skip them visually), add the following code in your .vimrc (colouring follows that of comments)

syn region MySkip contained start="^\s*#\s*\(if\>\|ifdef\>\|ifndef\>\)" skip="\\$" end="^\s*#\s*endif\>" contains=MySkip

let g:CommentDefines = ""

hi link MyCommentOut2 MyCommentOut
hi link MySkip MyCommentOut
hi link MyCommentOut Comment

map <silent> ,a :call AddCommentDefine()<CR>
map <silent> ,x :call ClearCommentDefine()<CR>

function! AddCommentDefine()
  let g:CommentDefines = "\\(" . expand("<cword>") . "\\)"
  syn clear MyCommentOut
  syn clear MyCommentOut2
  exe 'syn region MyCommentOut start="^\s*#\s*ifdef\s\+' . g:CommentDefines . '\>" end=".\|$" contains=MyCommentOut2'
  exe 'syn region MyCommentOut2 contained start="' . g:CommentDefines . '" end="^\s*#\s*\(endif\>\|else\>\|elif\>\)" contains=MySkip'
endfunction

function! ClearCommentDefine()
  let g:ClearCommentDefine = ""
  syn clear MyCommentOut
  syn clear MyCommentOut2
endfunction

To see the effect, position the cursor on the word DEBUG in the C code snippet above and type ,a

Related Plugins[]

Comments[]