May 24, 2021 Vim
Now that you've removed the stumbling blocks along the way, it's time to start writing some useful code for our Potion plug-in. We'll start with some simple syntax highlights.
Create
syntax/potion.vim
Put the following code in your file:
if exists("b:current_syntax")
finish
endif
echom "Our syntax highlighting code will go here."
let b:current_syntax = "potion"
Close Vim and open your
factorial.pn
file. Y
ou may or may not be able to see the message, depending on whether you have other plug-ins that output the message after the plug-in.
If you
:message
you will see that the file is indeed loaded.
Note: Every time I tell you to open a Potion file, I want you to open it in a new Vim window or process, not in a split or tab. Opening a new Vim window causes Vim to reload all your plug-ins for this purpose, and opening a split does not.
The lines at the beginning and end of the code file are a common practice, and if the syntax highlight of this buffer has been started, there is no need to reload.
For the rest of this chapter, we'll ignore the if and let defense walls
if
and
let
of the file.
Don't remove those lines, it's just that the eyes are not clear.
Replace the placeholder echom in the file with the
echom
syntax keyword potionKeyword to times
highlight link potionKeyword Keyword
Close
factorial.pn
and reopen it.
to
and
times
are highlighted as keyword types in your color scheme!
These two lines show the basic syntax highlights in Vim. To highlight a syntax:
syntax keyword
or related commands, which we'll mention more than once.
This allows the plug-in author to decide on meaningful syntax types to group and then link to a common highlight group. This also allows the color scheme creator to decide on a common program structure without having to consider a separate language.
In addition to what we use in our toy program, Potion has other keywords, so let's modify the syntax files to highlight them together.
syntax keyword potionKeyword loop times to while
syntax keyword potionKeyword if elsif else
syntax keyword potionKeyword class return
highlight link potionKeyword Keyword
The first thing to say is that the last line has not been changed.
We still tell Vim that all
potionKeyword
should be highlighted as
Keyword
We've now added three new lines,
syntax keyword potionKeyword
T
his means that executing this command multiple times does not reset the syntax type grouping -- it does augment it!
This allows you to define groupings in a zero-based way.
How you define grouping depends on you:
Another highlight group for Vim is
Function
T
his brings some Potion's built-in functions to our highlighted files.
Change your syntax file to this:
syntax keyword potionKeyword loop times to while
syntax keyword potionKeyword if elsif else
syntax keyword potionKeyword class return
syntax keyword potionFunction print join string
highlight link potionKeyword Keyword
highlight link potionFunction Function
Close and reopen the
factorial.pn
see that the built-in Potion function is now highlighted.
It works just like keyword highlighting. We defined the new syntax type grouping and linked to different highlight groups.
Think about why if
if exists
at the beginning of the file and let at the
let
are useful. I
f you don't understand, don't worry.
I also asked Tim Pope about it.
Browse
:help syn-keyword
.
Notice the section
iskeyword
Read
:help iskeyword
.
Read
:help group-name
learn about some of the common highlight groups commonly used by color scheme authors.