Wikia

Vim Tips Wiki

Changes: Highlight debug blocks in programs

Edit

Back to page

(Remove review tag; someone on vim.reddit.com tries this and it helped him; add link to ifdef plugin alternative.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=638
 
|previous=638
 
|next=640
 
|next=640
|created=January 20, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Karthick Gururaj
 
|author=Karthick Gururaj
Line 48: Line 48:
 
</pre>
 
</pre>
   
To see the effect, position the cursor on the word DEBUG in the C code snippet above and type <tt>,a</tt>
+
To see the effect, position the cursor on the word DEBUG in the C code snippet above and type <code>,a</code>
   
 
==Related Plugins==
 
==Related Plugins==

Latest revision as of 05:40, July 13, 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 PluginsEdit

CommentsEdit

Around Wikia's network

Random Wiki