简体   繁体   English

Vim中光标下方的拼写检查单词

[英]Spell-check word under cursor in Vim

I would like to have a quick way of spell-checking the word under cursor in Vim. 我想有一种快速的方法来对Vim中光标下的单词进行拼写检查。

Doing it in native Vim requires me to: 在本地Vim中执行此操作需要我:

(1) Activate spelling (2) Check the word (3) Deactivate the spelling (1)激活拼写(2)核对单词(3)停用拼写

The reason for (1) and (3) is that I do not want spelling mode on all the time (eg, I might be writing function/class documentation, and do not want the spelling highlighting non-natural language words in the code). (1)和(3)的原因是我一直都不需要拼写模式(例如,我可能正在编写函数/类文档,并且不想让拼写突出显示代码中的非自然语言单词) 。

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. 但是,当然,最后一个子句( :setlocal nospell )会干扰并取消拼写检查。

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. 您的函数的问题在于:normal("z=")不是调用普通命令的正确方法。 It should just be :normal z= because it is not a function. 它应该只是:normal z=因为它不是一个函数。 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. 这使z=可以用于没有颜色的拼写检查。

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. 是的,由于使用z=命令进行查询,这确实很棘手。 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. 我的SpellCheck插件通过:autocmd该问题,该拼写更正后立即触发该插件 You can use the plugin's infrastructure to wrap the z= command. 您可以使用插件的基础结构来包装z=命令。 Put the following into your ~/.vimrc : 将以下内容放入您的~/.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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM