I'm writing a vim syntax highlighting file for a specific language. For some keywords, specific characters are ignored (,
and -
and _
). In other words, these are exactly the same:
foo
f_oo
f-o,o
Is there a way I can ignore these characters when using eg. syn match
?
There's no special option for this; you have you make a pattern which optionnally allows the specific chars between EACH char; example with foo
:
f[,_-]*o[,_-]*o
Note that -
has to be placed at the end of the []
block (see :h /[]
).
As it is fastidious to write, you can create a function to make it for you:
func! CreatePattern(word)
let l:s = ''
let l:first = 1
for i in range(len(a:word))
if !l:first
let l:s .= '[,_-]*'
else
let l:first = 0
endif
let l:s .= a:word[i]
endfor
return l:s
endf
After this, we have:
:echo CreatePattern('bar')
b[,_-]*a[,_-]*r
Then, you can use :syn match
with the help of :execute
:
:exe 'syn match MyHighlightGroup /'.CreatePattern('foo').'/'
or using matchadd()
:
:call matchadd('MyHighlightGroup', CreatePattern('foo'))
Please note that the function above will only work with plain words, but it will break any pattern with contains special pattern chars. You may have to write a better function for such needs.