I've turned on spellcheck with :set spell spelllang=en_us. Now there's a bunch of words highlighted in red, and I just want to correct them all to their closest matches.

How can I correct all misspelled words in the entire file?

1 Answer 1


Here's something I didn't know until a little while ago: Macros can be recursive! That is, you can call a macro from within the same macro.

So, here's the command:


That is,

gg  Move to the beginning of the file
qq  Start recording the "q" macro
]s  Find the next misspelled word
1z= Correct its spelling
@q  Call the "q" macro (we're still recording!)
q   Finish recording
@q  Call the macro for the first time

Since there's a @q at the end of the macro, it repeatedly calls itself until it reaches EOF. Therefore, it'll correct all the misspelled words in the entire document.

Make sure the q macro is empty before running this, though (if not, you can clear it with qqq).

  • Is there also a way to easily see which words Vim corrected? I ran this on a file, and it generated a lot of gobbledygook ... Feb 6, 2015 at 0:18
  • How terrifying. So this calls the macro while it is still recording that same macro? Feb 6, 2015 at 4:28
  • @Carpetsmoker You could save the file as a new file, and run a diff on them. But this isn't going to produce anywhere near reasonable results for source code and the like, of course.
    – Doorknob
    Feb 6, 2015 at 12:46
  • @Oxinabox Yes, and as long as the macro is empty before this, the finished macro will repeatedly call itself until reaching EOF.
    – Doorknob
    Feb 6, 2015 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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