Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am curious to know how I can find the next instance of a SpellLocal mistake (either by a motion key or via a search command) in vim.

share|improve this question
up vote 5 down vote accepted

What you can do is modify the ]s and [s motions to skip over SpellBad highlighted words. This is actually not very hard. With the spellbadword() function we can get information about the spell status of the current word:

Example outputs of :echo spellbadword():

  • test: ['', '']
  • behavior (with en-gb locale): ['behavior', 'local']
  • qwe: ['qwe', 'bad']

So what we need to make is a mapping which:

  1. Runs ]s.
  2. Checks if the word under the cursor has the status local.
  3. If so, stop.
  4. If the cursor position is unchanged, there are no more misspelled words and we stop.
  5. All else: go to the next word and back to 1.

And here is exactly such a function:

fun! SpellLocal() abort
    let l:start_pos = getpos('.')

    while 1
        " Store the cursor position
        let l:cursor = getpos('.')

        " Run ]s
        normal ]s

        " The cursor position hasn't changed, this means ]s didn't find
        " anything else.
        if getpos('.') == l:cursor
            " Restore cursor position to match default ]s behaviour
            call setpos('.', l:start_pos)
            break
        endif

        " We found a SpellLocal; stop.
        if spellbadword()[1] == 'local'
            break
        " Something else; continue to next word
        else
            normal w
        endif
    endwhile
endfun

nnoremap <silent> <Leader>]s :call SpellLocal()<CR>

You can expand this by passing an argument which specifies the direction (]s or [s). This is left as an exercise for the reader :-)

share|improve this answer
    
Thanks. This is exactly what I wanted... – Fabian Mar 4 at 22:00

You could try SrchRplcHiGrp.vim: this plugin creates a command SRSearch which allows to search for the occurrences of an highlight group. As hl-SpellLocal is an highlight group that should do the trick.

To use it you'll have to find a word highlighted as a SpellLocal error, put your cursor on this word and use SRChooseHiGrp to select the group you're looking for. Then you can use SRSearch to go to the next word of the group.

share|improve this answer
1  
Sounds like it should work, but it doesn't (at least for me). SRChooseHiGrp does not select the correct group. However, this can be overcome by SRChooseHiGrp 31. However, afterwards SRSearch finds nothing. You can check this (unexpected behavior) with SRChooseHiGrp 28 (searching for bad spelling mistakes). – Fabian Mar 4 at 15:36
    
@Fabian: Indeed as I don't usually have spelling enabled I tested the plugin with some other highlighting groups (which worked) but after further investigations this doesn't find words in SpellBad... I suspect a bug in the plugin, I'll try to look at it and edit the answer if I find something. – statox Mar 4 at 15:59

For navigating along the different spell mistakes, you can use the \[s and \]s motions, which move to the previous and next mistake.

See :h spell-quickstart for an introduction on how to use spell.

share|improve this answer
1  
I understand. However, I am interested to only find the SpellLocal mistakes. There is the motion key ]S which only cares about the SpellBad mistakes. How to do the analog for the SpellLocal mistakes? – Fabian Mar 4 at 14:19
    
I see, but from the reading I have of the documentation, I don't see anything related to it. – nobe4 Mar 4 at 14:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.