History
Article Edit this page Discussion

Check for comments independent of filetype

From Vim Tips Wiki

Jump to: navigation, search

Tip 218 Previous Next Created: February 22, 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.

[edit] Comments

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Check for comments independent of filetype"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation