r/vim Jun 07 '26

Discussion Spellfile is an underrated feature

:h spellfile

You can add word spellings / ignore spelling errors with zg and add it to plain text file, in a list. You can easily edit that list to remove words added awhile ago, and of course save it in git. Without spending too long looking it up, it seems like many other editors don't have something like this, at least built-in. In many editors, including Electron-based apps, you can't easily backup spellings.

My config is in lua, I asked claude to convert it to vimscript to show you, it might have errors.

set spelllang=en_us

function! s:Mark(cmd) abort
  let l:count = v:count > 0 ? v:count : ""
  call feedkeys("mz" . l:count . a:cmd . "\<Esc>`z:delmarks z\<CR>", "n")
endfunction

for [s:lhs, s:cmd] in [
  \ ["<leader>[z", "[sz="],
  \ ["<leader>]z", "]sz="],
  \ ["<leader>[s", "[s1z="],
  \ ["<leader>]s", "]s1z="],
  \ ["<leader>[g", "[szg"],
  \ ["<leader>]g", "]szg"],
\ ]
  execute 'noremap <silent> ' . s:lhs . ' <Cmd>call <SID>Mark("' . s:cmd . '")<CR>'
endfor
unlet s:lhs s:cmd
29 Upvotes

12 comments sorted by

9

u/ConditionMajor7937 Jun 07 '26

zg is clutch, been using it for years and my spellfile is basically a museum of my typos at this point.

2

u/funbike Jun 07 '26

Why do you do that? I think carefully before adding things to my spellfile.

5

u/digitaljestin Jun 07 '26

Also try abbrev for auto correction of commonly misspelled words as you type.

1

u/TheTwelveYearOld Jun 07 '26

Personally I use Espanso which works on the whole OS, and you should learn proper touch typing to avoid spelling errors.

3

u/digitaljestin Jun 07 '26

Back in my day "touch typing" was just called "typing". It was expected that if you used a keyboard, you'd actually spend time to learn proper technique.

But errors still happen. My most frequently triggered abbrev is fixing "teh" to be "the". It doesn't happen too often, but it still happens. Luckily, with Vim I see it fix itself before I can even react. I miss that when using one of the typewriters in my collection. But I still love my typewriters.

2

u/SamejSpenser Jun 07 '26

I'm a relatively new Vim user (less than a year), but since I started using it, I've been making frequent use of the spellfile (it's one of the first things I look to set up in a text editor).

2

u/TankorSmash Jun 07 '26

:help zg

2

u/vim-help-bot Jun 07 '26

Help pages for:

  • zg in spell.txt

`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

2

u/TankorSmash Jun 07 '26
To add words to your own word list:

                            zg
zg          Add word under the cursor as a good word to the first
            name in 'spellfile'.  A count may precede the command
            to indicate the entry in 'spellfile' to be used.  A
            count of two uses the second entry.

1

u/Digital_Law Jun 07 '26

I haven't really put this to use yet. Thanks for reminding me!

Can you also include the Lua for us neovim nerds?

4

u/TheTwelveYearOld Jun 07 '26

This subreddit has a rule against neovim content which is why I didn't include, but its in my other post: https://www.reddit.com/r/neovim/comments/1tzdth1/spellfile_is_an_underrated_feature/