質問

I would like to have a quick way of spell-checking the word under cursor in Vim.

Doing it in native Vim requires me to:

(1) Activate spelling (2) Check the word (3) Deactivate the spelling

The reason for (1) and (3) is that I do not want spelling mode on all the time (e.g., I might be writing function/class documentation, and do not want the spelling highlighting non-natural language words in the code).

I thought something like this might work:

nnoremap <F1> :setlocal spell<CR>z=:setlocal nospell<CR>

But, of course, the last clause (:setlocal nospell) interferes with and cancels the spell check.

I also tried the following, but this does not work either:

function! s:spell_check_current()
  :setlocal spell
  :normal("z=")
  :setlocal nospell
endfunction
nnoremap <F1> :call <SID>spell_check_current()<CR>

Any suggestions?

Thanks.

役に立ちましたか?

解決

The problem with your function is that :normal("z=") is not the correct way to call the normal command. It should just be :normal z= because it is not a function. Second the leading : are not needed. So the function would be

function! s:spell_check_current()
  setlocal spell
  normal z=
  setlocal nospell
endfunction
nnoremap <F1> :call <SID>spell_check_current()<CR>

While this brings up the spell checking window it doesn't allow the user to input anything so this probably isn't going to work.


Instead you should just turn off the highlighting for spell checking since that seems to be what annoys you most.

Adding these after your color scheme is loaded should disable the colors.

highlight clear SpellRare 
highlight clear SpellBad 
highlight clear SpellCap 
highlight clear SpellLocal

This enables z= to work for spell checking without colors.

If you want the colors to be toggleable you could create some mappings to put the highlight rules in place.

他のヒント

Yes, because of the querying done by the z= command, this indeed is tricky. One cannot immediately turn off spell checking again. My SpellCheck plugin works around it via an :autocmd that is triggered soon after the spell correction. You can use the plugin's infrastructure to wrap the z= command. Put the following into your ~/.vimrc:

nnoremap <silent> <expr> z= SpellCheck#mappings#SpellSuggestWrapper('call SpellCheck#mappings#SpellRepeat()')
xnoremap <silent> <expr> z= SpellCheck#mappings#SpellSuggestWrapper('call SpellCheck#mappings#SpellRepeat()')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top