0

I often write code, and sometimes I check comments with z= when I am not sure of a spelling of some word. Vim then presents a list of suggestions. However, when I got the spelling right and the word is correct, then the word is not in the suggest list, so I do not know if I spelled it correctly or not. For me, as the word is correctly spelled, the same word has the "best" match for spellchecking suggestion, so the same word should be the best suggestion.

[screenshot of completion of 'word'

Above is a screenshot after running z= on Word. The best suggestion for changing a correct word Word is Ward. Would it be possible for vim to suggest the same word when the word is correct? I usually replace the word for anything, and then re-run suggestion and find the same word in suggestion list, just to make sure it is correct.

enter image description here

I tried reading :h spell spellsuggest spelloptions etc. but I did not find such option. The behavior stays the same in neovim and vim, with and without (-U NONE) plugins. The behavior most probably depends on spellsuggest() that has a description that:

                                                        spellsuggest()                                                                  
spellsuggest({word} [, {max} [, {capital}]])
                ...
                {word} may also be a good word.  Similar words will then be
                returned.  {word} itself is not included in the suggestions,
                although it may appear capitalized.

Is my only option to add spellsuggest^=expr:MySuggest() where in pseudocode function MySuggest() if v:val is correctly spelled; then return [v:val, 0] endfunction? So I did just that:

function Kc_spell_suggest_correct_word()
    if len(spellbadword(v:val)[0]) == 0
        " return [['is correct '.v:val, 1]
        return [[v:val, 1]
    endif
    return []
endfunction
set spellsuggest^=expr:Kc_spell_suggest_correct_word()

But the word still does not show up in the suggest list and the list stays the same as in the first screenshot. If however the 'is correct ' part is uncommented, it does show up in the result list! Based on that, the function works as it should and there's some else place that filters the word. No errors show up in set verbose=1:

enter image description here

What can I do to make it show up the same word in completion list if the word is good?

1
  • 1
    If you :set spell and your colorscheme supports it, you should have highlighting when a word is misspelled, so you needn't guess in this way. There's also ]s and [s to navigate between misspelled words.
    – D. Ben Knoble
    Oct 4 at 23:08
0

I ended up doing the following:

" Include correct word in spellsuggestiong if it is correct. See :h spellsuggest()
function Kc_spell_suggest_correct_word()
    if len(spellbadword(v:val)[0])
        return []
    endif
    if v:lang == 'pl_PL.UTF-8'
        echo 'Słowo "'.v:val.'" jest poprawne.'
    else
        echo 'The word "'.v:val.'" is correct.'
    endif
    return [[v:val.'_is_correct', 1]]
endfunction
set spellsuggest^=expr:Kc_spell_suggest_correct_word()

What is does, it displays the best correction with _is_correct appended when the word is correct. When I correct the word (z=1<CR>) I see _is_correct and then just type u to undo it and I know the word is correct. It also displayed The word <word> is correct. on top of the spellsuggest window, there's an empty line and it's displayed there.

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.