VIM-BASED SOLUTIONS
INTRODUCTION
LanguageTool is a blessing for the open-source community when it comes to spelling & grammar checking. As a bonus, it is a platform independent Java (.jar) application. Be warned, it is not perfect yet, but it is definitely usable and fills a gap that existed for a very long time. It is being actively developed and I sincerely hope for the day it will be the best tool available ever.
To begin with, any grammar-check solution will inevitably use LanguageTool. It is almost inescapable -- even paid commercial utilities will not give you the flexibility and the integration offered by LanguageTool. Now given this fact, the question simplifies to asking:
How can I use LanguageTool on platform X for documents of type Y?
I will now proceed to answering it for the VIM platform (and latex documents as just a special case). This is a canonical case as far as linux OS is concerned.
Basic Set-up
In summary, as of today in the Feb'17, I find three usable solutions as three plugins. I usually recommend Vundle plugin manager whose simple syntax to include in .vimrc is: (No harm in having all three plugins simultaneously. The subsequent setup of each individual plugin is mentioned in the rest of the write up.)
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'dpelle/vim-LanguageTool'
Plugin 'rhysd/vim-grammarous'
Plugin 'vim-syntastic/syntastic'
call vundle#end()
And then from within vim, issue a pair of commands once-for-all:
:so $MYVIMRC
:VundleInstall
You also need LanguageTool main java (.jar) application installed to ~/LanguageTool/
. You may use the following:
>> mkdir -p ~/LanguageTool
>> cd ~/LanguageTool
>> wget https://www.languagetool.org/download/LanguageTool-3.6.zip -O LT.zip
>> unzip LT.zip
The Main Solutions
As being the earliest LanguageTool plugin, this has some impressive performance except for some uncomfortable navigation.
There is one caveat to link the LanguageTool application on your desktop.
Include the following line in your .vimrc
and restart vim (you may :so $MYVIMRC
too).
let g:languagetool_jar='~/LanguageTool/languagetool-commandline.jar'
All set, you only have to start the spell/grammar-check-session using command :LanguageToolCheck
and end the session using :LanguageToolReset
. What you do in between is the key, and you use two commands :lnext
:lprevious
to navigate among errors. You may remap them in .vimrc
for a better comfort for e.g.
nmap gn :lnext<CR>
nmap gp :lprevious<CR>
Actually, you can be slightly smarter and try using those maps only when LanguageToolCheck
is active. And nunmap
them immediately on issuing LanguageToolReset
. (I found aliases LTC and LTR nice, issuing these additional nmap/nunmap'ing commands)
You are also allowed to change color of each type of error (for e.g. green for grammar and red for spelling) using vim commands such as: hi LanguageToolGrammarError guibg=peru guifg=wheat
. The full list of types of errors is found in :h LanguageTool
.
This is actually a nice alternative to the one above. It automatically installs languagetool java application for itself on its first invocation, hence a much quicker and easier installation to get yourself going.
So directly coming to the usage which is very similar to above except for a couple of more commands. Use :GrammarousCheck
to start and :GrammarousReset
to end the spell/grammar-check-session. The main tasks are in between and are performed using the small window that opens up explaining the errors in the file. This new window is special and using commands f
, n
, p
, and ?
you can navigate.
I personally prefer some of the remaps as below for a more comfortable usage.
let g:grammarous#hooks = {}
function! g:grammarous#hooks.on_check(errs) abort
nmap <buffer>gn <Plug>(grammarous-move-to-next-error)
nmap <buffer>gp <Plug>(grammarous-move-to-previous-error)
nmap <buffer>gr <Plug>(grammarous-move-to-info-window)r
nmap <buffer>gas f <Plug>(grammarous-move-to-info-window)f
nmap <buffer>gR <Plug>(grammarous-move-to-info-window)R
endfunction
function! g:grammarous#hooks.on_reset(errs) abort
nunmap <buffer>gn
nunmap <buffer>gp
nunmap <buffer>gr
nunmap <buffer>gf
nunmap <buffer>gR
endfunction
This above sample, when present in your .vimrc
will create five handy additional normal-mode commands namely gn
(next error), gp
(previous error), gr
(ignore the error), gf
(fix automatically with suggested correction), gR
(ignore all errors with same rule). These are available only to the current buffer and only when :GrammarousCheck
is active, and will disappear as soon as :GrammarousReset
is used.
3. Python wrappper: language-check in combination with vim's Syntastic plugin
Note: syntastic should have been installed already during above Basic Set-up step of three plugins.
This is an interesting and simplest solution of all, if you already have the infamous Syntastic plugin on vim and python installed on your linux. All that you have to do is install the python-wrapper language-check using the terminal-command:
>> pip install --upgrade language-check
and add the following two lines to .vimrc
.
let g:syntastic_text_checkers = ['language_check']
let g:syntastic_tex_checkers = ["lacheck", "chktex", "text/language_check"]
let g:syntastic_text_language_check_args = '--language=en-GB'
let g:syntastic_tex_language_check_args = '--language=en-GB'
Caveat: By default, this setup checks for spelling everytime a save/write command :w
is used and therefore slows it down. You may disable this check-on-write by using the following line in .vimrc
. But note that it means that you will check grammar yourself manually.
let g:syntastic_check_on_wq = 0
All set, you can simply use two commands :SyntasticCheck
and :SyntasticReset
to start and end the session respectively. You don't need any more commands here because of the special interface that already highlights and explains on status bar of what went wrong, as you move your cursor to the specific locations.
If anything troubling, you may first want to verify whether the filetype is properly detected to either tex
or text
by using the command:
:SyntasticInfo
latex2html
and then upload to grammarly.com. Note that this is a commercial product. A restricted trial period is available which permits free evaluation of the product for non-commercial purposes only for a limited period of time.