In Neovim, when you add a new word to your spellchecking dictionary with the command “zg”, the word will be added to a “.add” file in your config directory in the subdirectory “spell”. For example, “spell/en.utf-8.add”, depending on your locale. That’s a textual file you might want to share among different machines, e.g., part of the Git repository hosting your Neovim configuration. However, the used file is “.add.spl”, e.g., “spell/en.utf-8.add.spl”. That’s a binary file you don’t want to store in the Git repository, so you typically add it to “.gitignore”.
The “.spl” file is automatically updated (through the Neovim command “mkspell”) when you add a word with “zg”. But what if you modify the “.add” file from outside Neovim? For example, when you pull the Git repository of the Neovim configuration changes on another machine. You’d have to remember to run the “mkspell” inside Neovime (more details can be found here https://neovim.io/doc/user/spell.html).
Let’s instead make everything automatic by adding an “autocmd”, e.g., in LazyVim, by adding this to the “lua/config/autocmds.lua” file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-- Ensure that the binary spl file is up-to-date with the source add file vim.api.nvim_create_autocmd("FocusGained", { pattern = "*", callback = function() local config_path = vim.fn.stdpath("config") -- Get Neovim's config path local add_file = config_path .. "/spell/en.utf-8.add" local spl_file = config_path .. "/spell/en.utf-8.add.spl" if vim.fn.filereadable(add_file) == 1 then local add_mtime = vim.fn.getftime(add_file) -- Get modification time of .add file local spl_mtime = vim.fn.getftime(spl_file) -- Get modification time of .add.spl file -- Run mkspell! only if .add is newer than .add.spl or .add.spl doesn't exist if add_mtime > spl_mtime or spl_mtime == -1 then vim.cmd("silent! mkspell! " .. spl_file .. " " .. add_file) end end end, }) |
And that’s all!
If the “.add” file is changed from outside Neovim, the corresponding “.spl” file will be automatically regenerated.
Hope you find this post useful! 🙂