0

For a specific type of file, want to define customized comment with below requirements.

  1. Comment is start with -comment.
  2. Comment content is surround by curly brackets { }.
  3. Only show comment content with comment color; -comment and curly brackets are displayed with default color.

If modify $VIM/syntax/file.vim, add below commands, -comment { content } will all displayed with comment color.

syn region  myComment       start=/\s*-comment *{/ end="}"  
hi def link myComment       Comment 

1 Answer 1

0

I can think of two ways of doing it:

  1. rely on matchgroup clause of :h syn-region
  2. adjust start and end patterns to exclude part of the matched things.

matchgroup

As stated in help topic you can specify what highlight group should be used for the start and/or end:

syn region  myComment matchgroup=normal start=/\s*-comment *{/ end="}"  
hi def link myComment       Comment 

Set it to Normal highlight group (which is the regular text).

or adjust start and end patterns

syn region  myComment start=/\s*-comment *{\zs/ end="\ze}"  
hi link myComment       Comment 

Here I am using \zs and \ze to denote where is the actual start (\zs) or end (\ze) of the pattern is.

  • start=/\s*-comment *{\zs/ will match -comment { at the start of the line but \zs tells vim that the next char is the first char of the actual match.
  • same is with end=/\ze}/, where } is matched but the actual match is ended with the previous char.

Kind of, check :h \zs and :h \ze for a proper explanation.

enter image description here

3
  • For matchgroup method, '-comment' doesn't display with the same color as other -cmd; comment content is okay. For \zs and \ze, comment content doesn't show with comment color.
    – Fisher
    Commented Dec 27, 2023 at 9:55
  • what is the color of other `cmd? Use the same matchgroup=.
    – Maxim Kim
    Commented Dec 27, 2023 at 10:19
  • Now matchgroup method works fine when set matchgroup with the same color as other -cmd: syn region tclComment matchgroup=Special start=/\s*-comment *{/ end="}". Only curly brackets are also shown with Special color, instead of black. I'll just ignore this minor issue. Not sure why \zs and \ze method doesn't work.
    – Fisher
    Commented Dec 27, 2023 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.