Entering Vim script
Starting
I didn’t know the Vim language.
But I’m writing a blog, and I wanted to enable spell checking. I found on the Internet this:
Nice, but it is a long command, let’s create a key binding:
Writing this in .vimrc
and reloading the editor (or running :source ~/.vimrc
to reload the config) we should be able to toggle the spell checking pressing
SPACE s
.
Notes:
- the ! after
spell
means toggle and inverts the state of the parameter <CR>
is carriage return (enter)
One step further
Now why not print a message when the check is enabled or disabled? To run multiple instruction we can use a function:
Notes:
-
variables with name starting with
s:
are visible only in the current script file, the ones starting withg:
are globals and withl:
are locals (thel:
can be omitted, but it’s useful to avoid conflict with reserved variable names) -
<silent>
prevent the printing of called command, we want to display stuff from the function
More languages
We can also write a function to loop over different languages:
And support the “none” language too:
Make a plugin
Everything can be packet in a plugin putting the code in a .vim file inside
~/.vim/plugin/
To change the languages list from .vimrc
we have to define s:spell_list
as
global with the g:
and check if it is already defined before setting the
default value
The full plugin can be downloaded from GitHub here