Vim for LaTeX, part 2

Last week I wrote a post that offered an intro­duc­tion to the text edi­tor Vim, as well as a brief guide to learn­ing the odd­ity and power of Vim. Today I fol­low up with part two of my series of posts on Vim: cus­tomiz­ing Vim. This is the intro­duc­tory part. If you just want to see my .vimrc, look at part 3 in this series.

Vim is a bit of a throw­back com­puter pro­gram. If you are a com­puter pro­gram­mer (or if you are very com­fort­able using the com­mand line for some other rea­son), you might not notice this. But it is. In some ways, using Vim is like going back in time. The most obvi­ous dif­fer­ence between Vim and any ‘mod­ern’ text edi­tor in this regard is that all your con­fig­u­ra­tions and pref­er­ences for Vim are set using a text file. Even for your graph­i­cal MacVim and gVim.

With Vim there are two ways to make changes, one that makes the changes per­ma­nent (until you change them), one that makes the changes just for the cur­rent session.

Tem­po­rary changes in Vim configuration

When I am writ­ing a first draft, I don’t like for spell check to be set. I pre­fer to write with­out the dis­trac­tion of cor­rect­ing my mis­takes. But I want spell check to be on at some points in the writ­ing process. Since I want spell check more often than I don’t want it, I leave it on by default. And when I am draft­ing, I turn it off.

To turn off spell check­ing, in nor­mal mode (where you can’t type let­ters) just type :set nospell. The colon just tells Vim that you are about to enter a com­mand in nor­mal mode. Once you type it, you’ll see the colon appear in the last line of your Vim win­dow. The com­mand :set spell turns spell check­ing on; the com­mand :set nospell turns it off.

By default, Vim does not wrap long lines (where a line is any­thing between hard or car­riage returns). Each of your para­graphs is prob­a­bly a sin­gle line. Unless you write really short para­graphs, by default this means you will not see most of your para­graph, as it will carry off the win­dow to the right. To turn on line wrap­ping, you type :set wrap in nor­mal mode. To turn it off again, type :set nowrap. Though there may be some excep­tions to this rule, if you want to use a set­ting for the cur­rent win­dow for the cur­rent ses­sion, just type a colon, then ‘set’, then the set­ting you want to turn on or off. It will be set tem­porar­ily and you can try it out.

Note: in MacVim (I don’t know about gVim), if you click Edit in the menu bar, you’ll see options to change your Global Set­tings, File Set­tings, and Color Scheme. These should make the same kinds of tem­po­rary changes that :set command make.

Long term changes to your Vim configuration

You don’t want to have to turn on every con­fig­u­ra­tion option every time. Instead, you’ll want to set some options up to be turned on by default, when­ever you open Vim. To do that, you need to edit a text file.

The file in ques­tion is in your home direc­tory. When you open up the Finder on your Mac, you will likely be in your home direc­tory. If you open up a ter­mi­nal win­dow and type cd ~, you’ll go to your home direc­tory (you’ll prob­a­bly start in that direc­tory when you open up your ter­mi­nal win­dow). Your home direc­tory is the most basic direc­tory on your com­puter. There are more basic direc­to­ries on the com­puter, but any changes you make to those would apply to any user account on the com­puter. At work, you likely can’t access any­thing more basic than your home direc­tory. The tilde (~) is stan­dardly used to refer to your home direc­tory. Strictly, this is short­hand for /Users/username, where ‘user­name’ is your user name.

All your Vim con­fig­u­ra­tion details will go in your ~/.vimrc file. That is, they will go in a file called .vimrc in your home direc­tory. The dot there is impor­tant. And it is impor­tant that this file not have an exten­sion (like .tex or .txt). You likely won’t have such a file to begin with. To see if you do, open a ter­mi­nal win­dow, make sure you are in ~, and type ls -a. That will list all the files in that direc­tory, includ­ing the ones that start with a dot. On a Mac, those dot files are hid­den in the Finder. But even on Linux boxes I think they don’t show up in a stan­dard ls com­mand. If there is a file, you’ll want to open it. If there isn’t one, you’ll want to cre­ate one. A vim .vimrc com­mand from your home direc­tory will open or cre­ate the file, as needed.

If you want to turn spell check­ing on by default, just write set spell all by itself on a line in this file. If you want to set line wrap­ping by default, write set wrap on a line all by itself. Save the file, and next time you open up Vim, mis­spelled words will be high­lighted and long lines will be wrapped.

Vim will check this file every time it start up. But it only checks the file at startup. So if you want to change some­thing now, you’ll need to set it via the first method above.

Note: if you are using MacVim or gVim, you can access your .vimrc file with­out open­ing up a ter­mi­nal (or typ­ing :e ~/.vimrc, which will always open that file for edit­ing). In the menu bar, click Edit -> Startup Settings and your .vimrc will open. Make changes to that and save it, and the changes will take effect next time you start Vim.

More

I’m going to share the rel­e­vant parts of my .vimrc. But since this post is get­ting kind of long, I’m going to do it in a sep­a­rate post.

Posted Thursday, July 22nd, 2010 under LaTeX, text editors.

3 comments

  1. I switched spell check off at all and use free online spell check if it nec­es­sary: http://​www​.spellchecker​.net/​s​p​e​l​l​c​h​e​ck/ It also has gram­mar check and thesaurus.

Leave a Reply