Wikia

Vim Tips Wiki

Changes: Check for comments independent of filetype

Edit

Back to page

(Remove html character entities)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=217
 
|previous=217
 
|next=219
 
|next=219
|created=February 22, 2002
+
|created=2002
 
|complexity=advanced
 
|complexity=advanced
 
|author=Mark A. Hillebrand
 
|author=Mark A. Hillebrand
Line 16: Line 16:
 
:echo synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name")
 
:echo synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name")
   
echoes the group used for '''highlighting''' the character at the current cursor position, see {{help|synIDtrans()}}. It will usually be <tt>Comment</tt> if the cursor is inside of a comment, so
+
echoes the group used for '''highlighting''' the character at the current cursor position, see {{help|synIDtrans()}}. It will usually be <code>Comment</code> if the cursor is inside of a comment, so
 
synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") == "Comment"
 
synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") == "Comment"
   
detects, independent of the filetype (which have their own group <tt>names</tt> for comments), if the cursor is inside a comment or not.
+
detects, independent of the filetype (which have their own group <code>names</code> for comments), if the cursor is inside a comment or not.
   
 
The expression
 
The expression

Latest revision as of 05:19, July 13, 2012

Tip 218 Printable Monobook Previous Next

created 2002 · complexity advanced · author Mark A. Hillebrand · version 6.0


For some scripts it might be useful to detect, whether a specific position in a buffer is inside of a comment or not. Syntax highlighting can save us the work for parsing the comments ourselves. The command

:echo synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name")

echoes the group used for highlighting the character at the current cursor position, see :help synIDtrans(). It will usually be Comment if the cursor is inside of a comment, so

synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") == "Comment"

detects, independent of the filetype (which have their own group names for comments), if the cursor is inside a comment or not.

The expression

synIDattr(synIDtrans(synID(line("."), col("."), 0)), "name") =~ 'Comment\|Constant\|PreProc'

will detect additionally, if the cursor is inside of a string or some preprocessor statement.

CommentsEdit

Around Wikia's network

Random Wiki