17

I do a fair amount of transcription in which I type at 100+ wpm without looking at the screen. I think it would be useful if Vim could give me some sort of audible cue if the spell-checker has just marked the last word (likely indicating that I made a typo and should double-check what I wrote).

The spell-checker can definitely syntax-highlight words that are misspelled as soon as I hit the spacebar. How does this work? Is there a way to run some sort of command at the same time?

I always use Vim in the terminal; I don't have the terminal bell sound set up right now, but if that is the easiest way to do it, I can probably get that working.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Soren Bjornstad
  • 504
  • 2
  • 11
  • The question itself aside, do you really want to get your typing interrupted to fix a word? Wouldn't you be faster if you complete the writing first, then go through the errors, for example with a "jump to next misspelled word" command? – Shahbaz May 11 '15 at 08:32
  • My motivation goes more like this: the way I usually type, I can tell when I've clearly typed a word correctly, but sometimes I type a word and know that I fumbled it a little bit, but there's still a chance that I got it right. If it doesn't beep, then I know I got it right and I can go on without worrying about it; otherwise, I ^W over it and try again.

    This is a perfectly fair question, though – it's probably worth timing a few different approaches and seeing what happens.

    – Soren Bjornstad May 11 '15 at 14:28
  • 2
    Ok, the way you do it, it would probably be more efficient. If the beep "made you look", that would have probably slowed you down, but if you respond to a beep with an automatic ^W, without even looking at the screen, that should be as fast. Just be careful though, if your brain perceives the audible beep after you have started typing the next word, ^W would delete the wrong word. In such a case, you may even think of throwing away all inserted characters after a beep until ^W is pressed, so you can safely know that ^W deleted the misspelled word and you have to continue from there. – Shahbaz May 11 '15 at 14:37

1 Answers1

15

The last misspelled word is not directly accessible, nor can the highlighting be queried. You have to retrieve the word before the cursor yourself, and spell-check that one separately. Fortunately, there's such spellbadword() function. The following sets up a trigger on each inserted key that checks the last word:

autocmd CursorMovedI * if &spell && spellbadword(matchstr(strpart(getline('.'), 0, col('.') - 1), '\S\+\s$') )[1] ==# 'bad' | execute "normal! \<C-\>\<C-n>\<Esc>" | endif

Instead of beeping (via the :normal! <Esc>), you could also invoke an external tool via system() that plays a brief sound. That would also avoid problems with <Esc> stopping the cursor move, which I've seen happen sometimes.

Ingo Karkat
  • 17,819
  • 1
  • 45
  • 61
  • This is perfect! It does add a very slight delay on pressing the spacebar (when after a word), but it's not enough to be a big deal, and of course it only happens when spell-check is enabled. For the beep, I made a script that runs mplayer -really-quiet $SOUNDFILE >/dev/null 2>&1 & and put that in as the action. – Soren Bjornstad May 09 '15 at 19:39
  • 1
    Thanks! You could additional add if &spell && spellbadword(...) to disable even the word retrieval in case spell check is off. – Ingo Karkat May 09 '15 at 19:43