Use project or file specific spellfiles in vim

I just enabled spell check in vim. I like it and I know how to use it.

However, I would really like a project or even file specific spellfile as my list of accepted words. That way I can keep project (like thesis) specific words from being recognized in other projects.

When I open a Latex file in vim, I want vim to detect a particular spellfile in the Latex's file directory and load it. How can I achieve that?


One can use a code snippet executed after a file has been detected to be of type tex.

The idea is to check for the existence of the spellfile (properly named) in the working directory and then set the spellfile accordingly.

Assuming the filetype plugin is on (filetype plugin on), put this in ~/.vim/after/ftplugin/tex.vim:

let b:spellfile = expand('%:p:h').'/.vimspell.utf-8.add'
if filereadable(b:spellfile)
    let &l:spellfile = b:spellfile
    setlocal spell
    setlocal spelllang=en_us
else
   setlocal spellfile=
endif

This will look for a file .vimspell.utf-8.add in the Latex files's directory, enable spell checking and set the spellfile accordingly.

Inspired by http://vim.1045645.n5.nabble.com/Simple-way-to-add-local-spell-file-for-a-limited-set-of-filesystem-locations-td5709299.html.