如何在vim中为初始字符定义语法区域?

时间:2017-03-22 03:15:37

标签: vim vim-syntax-highlighting

a - this is a
b - this is not c
* - it is *

如何分别突出显示a-和以下说明?我对vim中的语法定义知之甚少。这是我的尝试:

syn region MyMinus start="-" end="\s"
hi def link MyMinus String

但我没有突出显示最初的字符和描述部分。我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:1)

以下是一种使用contains=...contained关键字的方法:

" Add some highlight defs:
hi link MyLeftPart  SpecialKey
hi link MyMinus     Operator
hi link MyRightPart String

" Match the whole lines, and make the given highlight work only
" with lines with this format: "x - blah blah..." :
syn match MyLeftPart  /^. - .*/  contains=MyMinus

" Leave the very first char highlighted with MyLeftPart, and
" highlight the rest with MyMinus:
syn match MyMinus     /.\zs.*/   contains=MyRightPart contained

" Leave the minus highlighted with MyMinus, and highlight the rest
" with MyRightPart:
syn match MyRightPart / - \zs.*/ contained

当然,您可以修改三种给定的模式以更好地满足您的需求(例如,将-替换为\s\+-\s\+,或删除^以突出显示即使行向右移动也能正常工作。

您可能也更喜欢其他解决方案,例如使用matchadd()函数代替:syn match