Mike的分享空间
date: 2017.08.26; modification:2017.08.26
目录:
syn keyword {group} {keyword} ... " {keyword}不能包含"" 或 //
syn keyword xStatement n[ext] " 匹配n, ne, nex, next, 不匹配nee, net, nextt, nexx, ex
设置可以用作关键字匹配的字符:
:setlocal iskeyword+=-
syn keyword xStatement when-not
hi link xType Type
hi link xStatement Statement
syn match xIdentifier /\<\l\+\>/
syn region xString start=/"/ end=/"/
syn region xString start=/"/ skip=/\\"/ end=/"/
syn keyword xTodo TODO contained " 包含contained字段后不会单独起作用. 只有被包含时才起作用.
syn match xComment /%.*/ contains=xTodo
递归嵌套(Recursive nesting):
syn region xBlock start=/{/ end=/}/ contains=xBlock
Keepend:
嵌套时的结尾重合时, keepend可以使得外层结尾不会被跳过. 注意keepend写在外层.
syn region xComment start=/%/ end=/$/ contained
syn region xPreProc start=/#/ end=/$/ contains=xComment keepend
注意: start如果有重合的部分, 则会优先匹配外层, 则内层不会被匹配到.
Containing many items:
syn region xList start=/\[/ end=/\]/ contains=ALL " 包含所有
syn region xList start=/\[/ end=/\]/ contains=ALLBUT,xString " 包含所有除xString之外的匹配, 可用逗号列表.
syn region xList start=/\[/ end=/\]/ contains=CONTAINED " 包含带contained的匹配.
syn region xList start=/\[/ end=/\]/ contains=CONTAINED,{group-name},.. " 包含带contained的匹配, 除{group-name}之外.
syn region xList start=/\[/ end=/\]/ contains=TOP " 包含不带contained的匹配.
syn region xList start=/\[/ end=/\]/ contains=TOP,{group-name} " 包含不带contained的匹配, 除{group-name}之外.
syn match xIf /if/ nextgroup=xIfCondition skipwhite
syn match xIfCondition /([^)]*)/ contained nextgroup=xThen skipwhite
syn match xThen /then/ contained
不加matchgroup的话, 匹配到的start和end也会一起加入到语法高亮中, 但是加了之后, start和end匹配到的内容就会被当做是一个独立的语法高亮组, 可以单独的设置高亮.
syn region xInside matchgroup=xParen start=/(/ end=/)/
syn region xInside matchgroup=xParen start=/(/ matchgroup=xParenEnd end=/)/
例如:
syn region xInside matchgroup=xParen start=/(/ end=/)/
hi link xInside Statement
hi link xParen Special
这里匹配到的小括号和括号中的内容, 就会被高亮成不同的颜色.
syn region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/ contains=cCondNest
syn region cFor matchgroup=cFor start=/for\s*(/ end=/)/ contains=cCondNest
syn region cCondNest start=/(/ end=/)/ contained transparent
syntax region testOffset start=/mstart/ms=e+1 end=/mend/me=s-1
如果用来匹配:
mstarthello worldmend
则高亮'helloworld'. 具体意思是: ms代表'start'=/mstart/匹配到的内容, e+1表示从end之后的一个字符开始, 也就是hello开始.
syn region xIfThen start=/if/ end=/then/ oneline
说明: 加了oneline之后, 必须在一行之内才会匹配. 并且如果不在一行之内的话,
那么连start也不会匹配(如果没有oneline则会匹配到start).
匹配宏定义(带续行):
syn region xPreProc start=/#define/ end=/$/ contains=xLineContinue
syn match xLineContinue "\\\s*$" contained
当多个需要包含同样的一批匹配时, 可以用cluster来作为组群.
syn cluster xState contains=xNumber,xIdent
syn match xFor /^for.*/ contains=@xState
syn match xIf /^if.*/ contains=@xState
syn match xWhile /^while.*/ contains=@xState
这样就不用把一批写很多次了.
cluster中添加/删除子匹配:
syn cluster xState add=xString
syn cluster xState remove=xNumber
例如在c++中包含c:
:runtime! syntax/c.vim