Getting spelling right with AutoCorrect()
One of my favorite tools in vim is AutoCorrect. It works almost seamlessly with files that contain both code and writing, like Rmarkdown files, and is very good at fixing my more common typos (t-e-h, sep-e-rate and so on). Additionally, it is very satisfying to watch typos disappear as I write them.
However, there is one small issue that I could not get over - when AutoCorrect loads
Vim hangs for a second or two. Not severe, but still aggravating. In fact, just aggravating
enough that right on the github page it points out that calling it in the
is not the
best idea. It’s just enough time that you lose focus on what you were about to do.1
.vimrc
Losing focus
The good news is that Vim knows if it is in focus or not, i.e., it knows if you are the Vim window is the active window. This gives the chance to write a nice little bit of code:
the first part creates a global variable and sets it to 0. The function
calls 1
AutoAutoCorrect()
as long as the value
of that global variable is 0 and then changes the value of that
global variable to 1.
Finally, the 1
AutoCorrect()
command calls 1
au FocusLost
when focus is lost, meaning that the
first time focus is lost it will call 1
AutoAutoCorrect()
but there after it
will do nothing.1
AutoCorrect()