Spell Checking in Vim and working with Languages
Vim is ready to handle any language – and I do mean any. It comes with a sensational spell checker. It's also great at writing any character from any alphabet.
1 The Vim Spell Checker
I cannot find the words to express that sense of glory I felt when I discovered that Vim also comes with a genius spell checker. Enable it with set spell
. It highlights your mistakes in red. Jump to the next (], s) or previous ([, s) mistake, list proposals for corrections with z, = or add the word to your list with z, g. That's just about how simple and helpful it is.
2 Working with Different Languages
Vim gives you the option of choosing your language with the 'spelllang'
option. If you set it to a language that isn't yet available in your system, Vim will offer to download it for you. Then again, it's very much possible that it's also available in a package from your system's distribution.
What's interesting about the 'spelllang'
option, is that it actually expects a comma-separated list of languages. If you work on a file which has several languages, just specify them all to stop Vim reporting spelling mistakes that aren't. This has the obvious downfall that it won't stop you accidentally spelling similar words in the wrong language.
3 Managing Your Word Lists
As I previously said, z, g adds a word to your word list. They are kept in files inside the ~/.vim/spell
directory and bear the .add
extension. The .add.spl
and .sug
files are binary files and you can't directly work with them. However, .add
files are plain text, and you can edit them with any editor. It's useful when you want to remove a word that you accidentally added with z, g. It's also interesting to add compound words or expressions, i.e. words separated with non-word characters such as spaces, hyphens or commas. It's only once you've saved the file and run this command that the changes will be taken into account by the spell checker:
:mkspell!%
The !
character forces overwriting. The %
character refers to the current file name. Annoyingly, you will need to restart Vim for it to see the changes. I've so far not been able to find a way to make these changes live.
4 Completion
Once 'spelllang'
is set, you can also use CTRLXK to use the dictionary to complete words. In particular, this can prove very useful if you added an expression which is annoying to type to your word list.
5 Writing Any Characters
I once discovered that the Polish keyboard layout on Linux is in fact very useful for typing any kind of characters. It's literally a US layout, but you can type an impressive variety of characters with the ALT GR key – and not just Polish ones. Add acutes to any character – even consonants – with ALT GR;, any key. Likewise, use a similar combination to type e.g. graves, circumflexes, diæreses, tildes respectively with the \, ', [, ] keys. Granted, the choice of keys may seem a bit misleading. But I got used to it in no time at all.
Other interesting characters such as × (to correctly typeset the multiplication sign instead of using the x letter), ¿ and ≈ can be produced respectively with ALT GRSHIFT,, ALT GRSHIFT2 and ALT GRSHIFT7. It's worth spending a minute or two trying all the combinations in turn.
But when it comes to writing even fancier characters such as arrows, math and console drawing characters, Vim can come to the rescue with digraphs. With highly intuitive combinations involving CTRLK, you can generate a rather fantastic variety of signs. For instance, → is made with CTRLK, -, >. The list of digraphs can be displayed with the :digraphs
command, in a rather unhelpful menu. Quite frankly, you're better off using :help digraphs
so you can browse and search digraphs more freely in a real Vim window.
I discovered that Vim and digraphs are quite exquisite to work with when it comes to writing languages with non-roman alphabets, such as Arabic. Mostly, it's a matter of typing CTRLK, consonant, SHIFT+. For instance, to write labas?, which means How are you?, you only need type:
CTRLK, l, SHIFT+
CTRLK, a, SHIFT+
CTRLK, b, SHIFT+
CTRLK, a, SHIFT+
CTRLK, s, SHIFT+
This will print لاباس. It's actually fascinating to watch Vim draw the ligatures as you type. Don't forget to :set rightleft
to typeset the word in the right direction when applicable.