Check the spelling and grammar of your texts with Emacs using flyspell-mode and langtool-mode.
Emacs comes with several options to make your writing better by avoiding mistakes, controlling typos and grammar, like the ones described in the Emacs manual: Checking and Correcting Spelling.
After getting familiar with them you will probably want to automatically enable them in text modes (like markdown), and even controlling your source code comments and strings.
This guide shows how to make it with two of the best packages for writing:
flyspell-mode: Flyspell enables on-the-fly spell checking, highlighting incorrect words as soon as they are completed.
LanguageTool is an Open Source proofreading program for English, Spanish, and more than 20 other languages. It finds many errors that a simple spell checker cannot detect and several grammar problems.
After having both packages installed and properly configured, we will activate flyspell-mode as soon as we load a text mode, and check for grammatical errors each time we save the file.
Activate flyspell-mode when loading text-modes
I find very helpful to activate
flyspell-mode as soon as I load any
Markdown file, as
markdown-mode is a derivate of
can choose what works better for you. In this case I will activate it
flyspell-mode when loading a file in any of the recognized
text-mode’s, we load it with:
(dolist (hook '(text-mode-hook)) (add-hook hook (lambda () (flyspell-mode 1))))
And we can also avoid loading them in any other specific
(dolist (hook '(change-log-mode-hook log-edit-mode-hook)) (add-hook hook (lambda () (flyspell-mode -1))))
To also use
flyspell-mode to check source code comments and strings there is a
special mode called
flyspell-prog-mode, for example we can enable
(add-hook 'python-mode-hook (lambda () (flyspell-prog-mode) ))
Check grammar after saving a text file with LanguageTools
To check a text buffer and light up errors with
langtool we use the
langtool-check function each time we save the buffer using
after-save-hook, in this case I will activate it only for
(add-hook 'markdown-mode-hook (lambda () (add-hook 'after-save-hook 'langtool-check nil 'make-it-local)))
In Emacs is easy to forget to check for errors manually, enabling them automatically is a great way to minimize errors that can be avoided easily and improving our documents legibility.
- flyspell-mode: https://www.emacswiki.org/emacs/FlySpell
- langtool: https://github.com/mhayashi1120/Emacs-langtool
- LanguageTool: http://languagetool.org/
- Trey Jackson answer to How to add a hook to only run in a particular mode?