1

I use vim for prose and and code, and when it comes to prose I use spelling a lot to help me out. I have it set up and already, and I'm already working with z=, zg, zw, [s , and ]s to work with spelling.

The thing is, I write in both Portuguese and English, and I want some words to be valid for both languages and still be able to add specific new words to just one language, and have spelling work right.

How should I go about setting this up?

Thanks

  • Do you write in both Portuguese and English in one buffer or will some buffers be English and others Portuguese? – Rich Apr 4 at 15:55
  • I use one language per buffer. – Paulo Phagula Apr 4 at 16:13
  • you can try to setup spellfile and use 1zg or 2zg to add good words for first or second file in spellfile. For my setup (I use Russian and English) I just add good/bad spelling words to a default spellfile and it works for both languages (with cyrillic and latin alphabets) – Maxim Kim Apr 4 at 16:25
0

Vim allows you to manipulate multiple spellfiles and languages in a single buffer, so it's very flexible. Your best bet is to read :help spell.txt and then come up with a system that works best for you.

Here's how I would do it. Probably your ideal solution will be slightly different.

You've said that you only use a single language per buffer, so I set a single value for 'spelllang', but I set two values for 'spellfile' to facilitate adding words to both languages easily: the zg command will then add to the current language's spellfile, and the 2zg command will add to the alternate language's spellfile.

Then I set up a :LanguageToggle command for switching languages, and a <leader>zg mapping for adding a word to both languages at once.

" Set default language
set spelllang=en_gb
set spellfile=$HOME/.vim/spell/en.utf-8.add,$HOME/.vim/spell/it.utf-8.add

function! LanguageToggle() abort
  if &l:spelllang == "en_gb"
    " Switch to Italian
    setlocal spelllang=it
    setlocal spellfile=$HOME/.vim/spell/it.utf-8.add,$HOME/.vim/spell/en.utf-8.add
    echom "Switching to Italian"
  else
    " Switch to English
    setlocal spelllang=en_gb
    setlocal spellfile=$HOME/.vim/spell/en.utf-8.add,$HOME/.vim/spell/it.utf-8.add
    echom "Switching to English"
  endif
endfunction

" Switch Languages
command! LanguageToggle call LanguageToggle()

" Add word to both spellfiles
nnoremap <leader>zg zg2zg

N.B. I'm using Italian, because I don't speak any Portuguese: you'll want to replace it with pt in the above.

0

One easy way to do this is to have different file name extension for different language, which makes it easy to set spellchecker's language based on its extension.

For example, if you write mostly in English, you can designate .txt to use English spelling, and .ptxt as Portuguese spelling. So, an English text file might be file1.txt and Portuguese text might be file2.ptxt. (I just made up the file extension .ptxt. It could be anything, like .pt or .pt_txt, etc)

Then in .vimrc, inside the autocmd, add the following to load different spell checker based on filename extension (checking for either .txt or .ptxt):

if has("autocmd")
  augroup vimrcEx
    au! 
    "... all the autocmd goes here
    autocmd BufNewFile,BufReadPost  *.txt setlocal spell spelllang=en_gb spellfile=~/.vim/spell/en.utf-8.add
    autocmd BufNewFile,BufReadPost  *.ptxt setlocal filetype=text spell spelllang=pt spellfile=~/.vim/spell/pt.utf-8.add
  augroup END
endif 

Or if you want to be able to switch to different spelling by manually pressing keys:

nnoremap <F10> :setlocal spelllang=en_gb spellfile=~/.vim/spell/en.utf-8.add<CR>
nnoremap <F11> :setlocal spelllang=pt spellfile=~/.vim/spell/pt.utf-8.add<CR>

On the other hand, if you're mixing both English and Portuguese in one text:

setlocal spelllang=pt,en_gb
0

Based on the comments above I went with this solution

" Spelling {{{
if has('spell')
  set nospell
  set dictionary=/user/share/dict/words
  set complete+=kspell
  " Manually Added Words Are Sent To `Names` Dictionary
  nnoremap zG 2zg
endif

function! s:ToggleSpellLanguage(lang)
  if a:lang == 'none'
    set spellfile=
    set spelllang=
    set nospell
    return
  else
    let &spellfile=expand('~/.dotfiles/vim/spell/' . a:lang . '.utf-8.add') . ',' . expand('~/.dotfiles/vim/spell/names.utf-8.add')
    if a:lang == 'en'
      set spelllang=en_us
    elseif a:lang == 'pt'
      set spelllang=pt_pt
    endif
    set spell
  endif
endfunction
command! -nargs=1 ToggleSpellLanguage call <sid>ToggleSpellLanguage(<q-args>)
" }}}

I use zg to add new words to the "language" dictionary. and zG to add words to the private/names dictionary.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.