sponsor Vim development Vim logo Vim Book Ad

vim-DetectSpellLang : autodetect the spell-check language of the current buffer

 script karma  Rating 0/0, Downloaded by 741  Comments, bugs, improvements  Vim wiki

created by
Enno Nagel
script type
This Vim plug-in autodetects the tongue (= `&spelllang`) of the text in a
buffer (if it is spell checked, that is, `&spell` is set, and `&spelllang` has
not been explicitly set, say by a modeline :help modeline).

It uses either the spell checker aspell or hunspell (which are installed
in many Linux distributions, in Mac OS, and both available for Microsoft

In Microsoft Windows the path to the executable must

- be either added to the `%PATH%` environment variable (say by Rapidee from
  http://www.rapidee.com), or
- set by g:detectspelllang_program, say

    let g:detectspelllang_program = 'C:\Program Files (x86)\Aspell\bin\aspell.exe'


This plug-in only operates in buffers that are spell checked, that is,
&l:spell is set. To ensure that certain file types, for example 'text',
'markdown' and 'mail', are spell checked, add either the line

  autocmd FileType text,markdown,mail setlocal spell

or the line

  setlocal spell

to ~/.vim/ftplugin/text.vim (respectively markdown.vim and mail.vim).

The language is detected, depending on whether aspell respectively hunspell
is used, among those listed in g:detectspelllang_langs.aspell respectively
g:detectspelllang_langs.hunspell. It is *empty* by default and has to be
set by the user to a list of languages included in that of the output of the
command aspell dicts respectively hunspell -D!

For example, in case you use aspell,

  let g:detectspelllang_langs = {}
  let g:detectspelllang_langs.aspell = [ 'en_US', 'de_DE', 'es', 'it' ]

respectively, in case you use hunspell,

  let g:detectspelllang_langs.hunspell = [ 'en_US', 'de_DE', 'es_ES', 'it_IT' ]


- The value of 'g:detectspelllang_lines' defines how many lines are tested for
  spelling mistakes by hunspell to derive the suitable spellcheck-language.
  By default

  let g:detectspelllang_lines = 1000

- The values in the dictionary 'g:detectspelllang_ftoptions' define filetype-
  dependent options for aspell respectively hunspell (to specify the filter mode, for example).
  By default

  let g:detectspelllang_ftoptions.aspell = {
    \ 'tex'   : ['--mode=tex', '--dont-tex-check-comments'],
    \ 'html'  : ['--mode=html'],
    \ 'nroff' : ['--mode=nroff'],
    \ 'perl'  : ['--mode=perl'],
    \ 'c'     : ['--mode=ccpp'],
    \ 'cpp'   : ['--mode=ccpp'],
    \ 'sgml'  : ['--mode=sgml'],
    \ 'xml'   : ['--mode=sgml'],


  let g:detectspelllang_ftoptions.hunspell = {
    \ 'tex'   : ['-t'],
    \ 'html'  : ['-H'],
    \ 'nroff' : ['-n'],
    \ 'odt'   : ['-O'],
    \ 'xml'   : ['-X'],

  Note that on Windows the most recent official aspell binaries are from
  2002 and do not support all above file type options, for example, for 'tex'.

- The value of 'g:detectspelllang_threshold' defines the percentage of spelling
  mistakes among all words below which a spellcheck-language is recognized as
  correct. For example, if

  let g:detectspelllang_threshold = 20

  and g:detectspelllang_langs.aspell = [ 'en_US', 'de_DE' ] and if less than 20%
  of all words in the buffer are spelling mistakes for 'en_US', then
  'DetectSpellLang' does not verify if the percentage of spelling mistakes for
  'de_DE' is below that for 'en_US'.

- The autocommand event

  User DetectSpellLangUpdate

  provides by the buffer-local variables b:detectspelllang_old and
  b:detectspelllang_new the previous and current value of '&l:spelllang', the
  spell-check language. This way, a user command, for example for adding and
  removing abbreviations specific to a language, can hook in.


This plug-in vim-DetectSpellLang is developed at
https://github.com/konfekt/vim-DetectSpellLang where issues can be filed.
install details
Unzip vim-guesslang.zip into  ~/.vim on Linux (or MacOS) respectively %USERPROFILE%\vimfiles on Windows.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
vim-DetectSpellLang.zip 2.2 2020-01-13 7.0 Enno Nagel support hunspell as well as aspell and autodetect spelllang on enabling spell check
vim-DetectSpellLang.zip 1.2 2020-01-08 7.0 Enno Nagel restrict to lines likely to contain prose
vim-DetectSpellLang.zip 1.1 2019-07-10 7.0 Enno Nagel add configuration options and fix tidbits
vim-guesslang.zip 1.0 2016-11-27 7.4 Enno Nagel Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
OSDN.net Logo