Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript advanced syntax highlights


May 24, 2021 Vim


Table of contents


At present, we have implemented simple keyword and function syntax highlighting for Potion files.

If you don't do the exercises in the last chapter, you need to go back and finish it. I'll assume you did the exercise.

In fact, you should go back and finish any exercises you skip. E ven if you don't think you need it, you have to finish it for better learning. Please believe me at this point.

Highlight the comment

Next we need to highlight an important part of Potion - annotations. The problem is that Potion's # with the # is not iskeyword

If you iskeyword you don't listen carefully. G o back and finish that damn exercise. I n writing each chapter, I will not throw meaningless heavy work to you. You really need to finish them to keep up with the book.

Because # not a keyword character, we need to use a regular expression to match it (and the comments that follow). W e will syntax match syntax keyword Add the following lines to your syntax file:

syntax match potionComment "\v#.*$"
highlight link potionComment Comment

I'm not going to nag where I'm going to put them in the file. You're already a program ape: by your own judgment.

Close and reopen factorial.pn Add a comment somewhere in the file and you'll see it highlighted as a comment.

The second line is simple: it tells The Vim highlight potionComment type group is Comment

There's something new in the first line. We use syntax match tell Vim to match with a regular expression instead of a keyword.

Note that the regular expression \v which means that the "very magic" pattern is used. I f you're not sure, reread the chapter on basic regular expressions. (Chapter 31)

In the current situation, "very magic" mode is not required. But in the future we might change this regular expression and meditate on why it doesn't work, so I suggest always using "very magic" to ensure consistency.

As for the functionality of regular expressions, it's simple: match # start with a s, including all the characters that start at the end of the line.

If you need to reawaken memories of regular expressions, you should look at Zed Shaw's Learn Regex the Hard Way.

Highlight the operator

Another part that requires regular expressions to highlight is the operator. Add the following to your syntax file:

syntax match potionOperator "\v\*"
syntax match potionOperator "\v/"
syntax match potionOperator "\v\+"
syntax match potionOperator "\v-"
syntax match potionOperator "\v\?"
syntax match potionOperator "\v\*\="
syntax match potionOperator "\v/\="
syntax match potionOperator "\v\+\="
syntax match potionOperator "\v-\="

highlight link potionOperator Operator

Close and reopen factorial.pn Notice that the step *= is now highlighted.

You may notice first that I group each regular expression into a separate line instead of as a keyword. This syntax match is not supported to put more than one group in a row.

You should also notice that each regular expression starts with \v even if it is not required. When I write Vimscript, I want to keep regular expressions consistent, even if I need to type a few more symbols to do so.

You might wonder why I "\v-\=?" T he regular expression of to match at the - and -= Y ou can do the same if you want. I t will work. I just - -= are different operators, so put them in different rows.

Putting each operator in a separate match simplifies regular expressions at the expense of entering additional characters. I like to do this, but you probably don't think so. You decide for yourself.

Nor do I = the s as an operator. We'll do it all the time, but I hope not for the time being so I can give you a question.

Because of the - regular -= and - I had to define - - the definition -=

If defined in reverse order and used in the Potion file -= Vim will match - (and, of course, highlight it), the rest - = the match. This means that syntax match group, each group of "consumed" text fragments cannot be matched after that.

It's too general, but I'm not going to get into the details for the time being. In summary, you should match a larger group after matching a smaller group, because after that, the group defined takes precedence over the group defined before.

Let's go ahead = operator. Now listen to the question:

syntax match potionOperator "\v\="

Take a minute to think about where you should put it in the syntax file. If you need a hint, reread the previous chapters.

Practice

Read: :help syn-match .

Read :help syn-priority .

In the example, we : as an operator. R ead the Potion documentation and carefully decide : treat : as an operator. If you decide to do so, add it to the syntax file.

Also consider . . and / .

Add a highlighted number to the syntax type potionNumber L ink it to highlight group Number D on't forget that 0xffaf 123.23 1e-2 1.9956e+2 2 Remember to balance the time spent dealing with marginal states with the number of times they occur.