How to highlight words starting with @ in Vim syntax?

I have a very simple Vim syntax file for personal notes. I would like to highlight the name of the people, and I chose a syntax like Twitter @jonathan .

I tried:

 syntax match notesPerson "\<@\S\+" 

In the meaning: words starting with @ and having at least one character without spaces. The problem is that @ appears to be a special character in Vim regular expressions.

I tried to exit \@ and bracket the [@] regular tricks, but that didn't work. I could try something like (^|\s) (beginning of line or space), but it is precisely the problem that the word boundary is trying to solve.

Highlighting works on simplified regular expressions, so it's more about finding the right regular expression than anything else. What am I missing?

+7
source share
1 answer

@ is a special character only if you enable the "very magical" mode by placing \v in the pattern before @ . You have one more problem: @ does not start a new word. \< is not just a "word boundary" like perl / PCRE \b , but a "left word boundary" (in the help: "beginning of a word"), meaning that a keyword symbol should follow after \< . Since @ usually not a keyword symbol pattern \<@ , it will never match (and even if it looked like \b , it would match constructs like abc@def , which is definitely not what you want: by for the reason that I explained earlier).

Instead, you should use \k\@< !@ \k\S* : \k\@<! ensures that @ does not precede any character of the keyword, \k\S* ensures that the first character of the name is the keyword (maybe you can also use @\<\S\+ ).

There is another solution: include @ in 'iskeyword' and leave the regex as it is:

 setlocal iskeyword+=@- @ 

(see :h 'isfname' for an explanation of why I use @ -@ here. 'iskeyword' has exactly the same syntax and redirects you there for an explanation.)

+13
source

All Articles