Vim Word Processing

I’ve been using Vim for about a year now, and am pretty much addicted. Once I reached a certain level of proficiency, no other editor seems to be even close. The keys are very intuitive to me now, as is modal editing.

Of course, there is a problem when I have to type in other programs. For example, in OpenOffice, I routinely hit j a few times, and am surprised when that letter actually pops into the screen instead of moving the cursor around.

To overcome this, I’ve been messing around with ways to make Vim more friendly to general writing. Here’s what I’ve done or found:

Autocorrect

This section outlines my major actual contribution. The rest are just tips. Autocorrect is a key feature of almost any word processing program, and it’s tough to do by default in vim. When you type ‘teh’ and then have to go back and fix it, you are much less efficient. Vim has the concept of abbreviations, where you can map one word to another.

Example:

iabbrev teh the

Then you when you type in the whole word that matches, it will magically be replaced with the correction. Unfortunately, this takes quite a bit of time to build up automatically. Plus, while you catch ‘teh’ with this, ‘Teh’ doesn’t get changed. So it seems like there needs to be a more systematic approach.

I was looking around, and couldn’t find a nice native file that had corrections out there. Fortunately, Wikipedia has a machine-readable list of common misspellings! So this was a great start. There were about 4000 changes that were in there. Here’s a sample:

aggreement->agreement
aggregious->egregious
aggresive->aggressive
agian->again
agianst->against
agina->again, angina
agin->again
aginst->against
agravate->aggravate
agre->agree
agred->agreed
agreeement->agreement

There are a few like ‘agina’ that have multiple choices, so I picked the one that was most common. I don’t really type ‘angina’ all that often. Then again, the string distance is pretty huge there. So there are probably some that are a stretch, but it’s a great start.

Then I created a script to parse this file and do some important changes. Essentially, if a word to be corrected starts with a lowercase letter, it also should be changed when the mistake is capitalized.

Here is the script, generator, and product.

The only limitation that I found is that dashes and apostrophes in the word to be corrected don’t seem to work, and I couldn’t figure out how to escape them. And obviously sourcing thousands of lines of Vim commands takes a noticeable amount of time.

Spelling correction

Built into Vim nowadays. Just need to type :set spell. If it complains, you might need to set the spelling language.

Spelling suggestions z=

Previous misspelling [s

Next misspelling ]s

Add current word to dictionary zg

Undo adding current word to dictionary zug

Word Processing Mode

I commonly use a few things when doing word processing, so here’s a handy function that you can add to your .vimrc and modify as you see fit.

cabbr wp call Wp()
fun! Wp()
  set lbr
  source $VIM/vimfiles/autocorrect.vim
  set guifont=Consolas:h14
  nnoremap j gj
  nnoremap k gk
  nnoremap 0 g0
  nnoremap $ g$
  set nonumber
  set spell spelllang=en_us
endfu

You can add the preceding block to your .vimrc, and then invoke it with :wp.

Categories: main

« Review: Tribes Review: Bull Moves in Bear Markets »

Comments