4

According to :h spell

Vim only checks words for spelling, there is no grammar check.

It does however alert with a green wobbly line if a sentence begins with a small letter. Is there a way to have vim alert in a similar way if a word is repeated? This is a mistake I make quite often.

It should be fairly easy to find with a regular expression, e.g. with \v( \w{2,})\1\W. But how to apply this to vim's spellchecking?

4

I don't know how to use a regex to spell checking, but you can highlight repeated words the way spell would:

syn match SpellBad /\<\(\w\+\)\s\+\1\>/

(Yes, I like \ a lot. :P)

enter image description here enter image description here

This won't bring up foo foo when you look for badly spelled words (via [s or ]s, for example), but hopefully the quick visual feedback you get from the highlighting should be helpful.

The the is a special case, added to Vim's default rules, from what I can make of the help.

  • Can you explain the regex like pattern /\<\(\w\+\)\s\+\1\>/ ? – Cbhihe Sep 15 '15 at 20:00
  • 2
    @Cbhihe I hope that this helps: \<\> - are anchor for match the whole word \(\) - defines a capture group \w - word character \+ - 1 or more of the preceding character \s - white space character \1 - represents the content of the first capture group – vappolinario Sep 15 '15 at 20:41
  • 1
    This works great. I modified the pattern a bit to /\c\v<(\w+)\s+\1>/. \v makes it magic, so you don't need all the backslashes, and \c makes it non-case sensitive, so that it captures dublicated words where the first is in the beginning of the sentence and capitalized. – Andreas Sep 15 '15 at 20:42
  • @Andreas I had both of those in my test matching, but elected to use non magic, case sensitive in the final version. – muru Sep 15 '15 at 21:34
  • 1
    @muru: True, there's no ready made way to mark syntax as good in relation to SpellBad highlighting the way you proposed it, although one could dream up some way based on pointer hovering over highlighted areas to actually validate syntax locally. Another clunky option is to apply yr proposed cmd, fix all that need fixing in terms of repeated word instances and then supress highlighting of false positive by :syntax clear SpellBad. Not ideal but doable. – Cbhihe Sep 16 '15 at 10:46

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.