Vim for LaTeX, Part 1

Kevin wrote an exe­ce­lent post on text edi­tors for LaTeX recently. He did a nice job cov­er­ing a host of edi­tors there. As I’ve said before, I’m a big fan of Vim. I don’t have any­thing against other text edi­tors, but I think Vim has a lot of advan­tages and is really good for writ­ing LaTeX and other plain text doc­u­ments. But Vim isn’t exactly easy to learn. (As Kevin notes, LaTeX isn’t easy to learn either, so don’t let the learn­ing curve dis­suade you from try­ing Vim out.) Though I can’t make Vim easy to learn, I think I can serve as a bit of a guide, and I can hope­fully offer some advice and sug­ges­tions to make get­ting started with Vim and LaTeX eas­ier. So this will be the first in a series of posts that explains how I use Vim for phi­los­o­phy papers.

Vim is odd

The first thing you need to know about Vim, as Kevin and I have both men­tioned in the past, is that Vim is odd. It is ‘modal’. That is, there are mul­ti­ple modes in which you oper­ate. When you open up Vim, you are in NORMAL mode. In nor­mal mode, no key means what you thought it would! Type a ‘D’ in nor­mal mode and you will not get a ‘D’ under the cursor—you will have deleted the rest of the line from the cur­sor onward. Whoops.

If you want to write any­thing in Vim, you need to hit ‘i’. That puts you in INSERT mode. In insert mode you can insert text. When you are writing—putting words on the page—you are in insert mode. In insert mode, Vim is no dif­fer­ent than any other text edi­tor. To get back into nor­mal mode, you hit the escape (or Esc) key.

As Kevin men­tioned, Vim is an exten­sion of the old vi text edi­tor. The only modal edi­tors that I know of are the descen­dants of vi. Using Vim is unlike writ­ing with any text edi­tor you’ve ever used.

Vim’s strength

Why use a modal edi­tor? If you have two modes, then every key can have twice as many mean­ings. Instead of the ‘i’ key hav­ing two mean­ings (the low­er­case and upper­case let­ter ‘I’), with a modal edi­tor it can have four (the two ’I’s, plus the ‘insert a let­ter in front of the cur­sor’ and the ‘insert a let­ter at the start of the line’ com­mands). This means that the pos­si­ble key­board short­cuts are greatly increased. And most of the short­cuts are really easy to use. Instead of hold­ing Shift and Com­mand (or Con­trol), hit­ting the arrow until you’ve hit the end of the sen­tence, and then typ­ing Command+C or Control+C to copy the cur­rent sen­tence (a seri­ous wrist work­out), with Vim you only have to hit Escape, then ‘y)’.

Effi­ciency isn’t every­thing. And the Escape key can be a long way away (though you can set it up such that a pair of quick semi­colons is just like hit­ting Escape, which makes enter­ing nor­mal mode easy). But I think it is hard to deny that both nav­i­gat­ing and edit­ing a paper are more effi­cient in Vim, once you learn your way around the com­mands. I think it is more effi­cient than other text edi­tors after you learn just a few basic com­mands, but the more you learn, the more effi­cient it is.

Another advan­tage of Vim: it is every­where. There are really two ver­sions of Vim, one graph­i­cal and one you use in a ter­mi­nal emu­la­tor. On the sys­tems I’ve used (Mac OS X and Arch, Zen­walk, and Puppy linux), vi or Vim has been the default edi­tor in the ter­mi­nal. This means you’ll encounter it if you do any­thing in the ter­mi­nal (unless you change your pref­er­ences.) The first time I used the ver­sion con­trol sys­tem git on my Mac, I was dumped into an instance of Vim to edit a file and had zero clue what to do. I actu­ally ended up killing the pro­gram because I couldn’t fig­ure out how to type any­thing. Later that day, when I stum­bled upon ‘i’ and began typ­ing, I couldn’t fig­ure out how to save and close the file, so I had to abort the mis­sion again! (FYI: you save a file with :w in nor­mal mode, and you close the file with :q. To save and close type :wq.)

And you can install Vim on any sys­tem. So if you split your time between a linux and Mac com­puter, you can have one text edi­tor that you use on both. Kevin dis­cusses some other equally portable text edi­tors. I won­der if Vim isn’t more portable than many edi­tors, even those that have both Mac and Linux ver­sions. This is because all your Vim con­fig­u­ra­tions and pref­er­ences are done using plain text files. If you get a new com­puter, you just trans­fer your .vimrc file, and your new Vim is just like your old Vim—no mess­ing around with check boxes and other ways to set your pref­er­ences. Of course, this ‘advan­tage’ is only the result of the old-fashioned way Vim sets pref­er­ences in a text file, so maybe I’m mak­ing a virtue of neces­sity here!

How to get Vim

Vim is easy to install on most Linux dis­tri­b­u­tions and on a Mac. You can find down­load details here.

  • If you are using Linux, just fol­low your nor­mal steps for installing pack­ages. If you install Vim, you’ll get the most recent ver­sions of both the ter­mi­nal and graph­i­cal ver­sions of Vim.
  • If you are using a Mac, you’ll want to install MacVim. I highly rec­om­mend installing the most recent snap­shot instead of the sta­ble ver­sion, at least until MacVim 7.3 comes out. Snap­shot 52 has an update that you’ll need if you want to use all the plu­g­ins I rec­om­mend later. Down­load the file, then open the folder you just down­loaded. Drag the MacVim file into your appli­ca­tions direc­tory. Then drag the mvim appli­ca­tion to some­where in your PATH (some­where like /usr/local/bin). When you install the graph­i­cal MacVim, you are also installing the most recent ver­sion of the command-line Vim.
  • [Update:] There is another option for installing MacVim (on a Mac, obvi­ously): the Home­brew pack­age man­age­ment pro­gram. I highly rec­om­mend Home­brew. I dis­cuss installing Home­brew and using it to install MacVim in another post.
  • I’m going to punt on the Win­dows instal­la­tion instruc­tions. If you use a Win­dows PC, take a look at the down­load instruc­tions here. Hope­fully you’ll be able to tell what you need!

Learn­ing Vim

Now for the hard part: learn­ing Vim.

Your best bet is to start out with the built-in Vim tutor. This is a 30 minute or so hands-on tuto­r­ial. Prob­a­bly the eas­i­est way to do this is the way the MacVim site sug­gests: open up Vim (you should be able to access the graph­i­cal ver­sion of Vim in your Appli­ca­tions folder). You’ll be in insert mode. Type :!cp $VIMRUNTIME/tutor/tutor ~/Desktop. The let­ters you type will appear at the very bot­tom of your win­dow. Hit Enter and you will have cre­ated a file called ‘tutor’ on your desk­top (change the direc­tory if you like). Then click File -> Open, nav­i­gate to the new ‘tutor’ file, and open it. Then fol­low the directions.

(If you want, you can just open Vim and type, in nor­mal mode, :vimtutor. That will cre­ate a tem­po­rary ver­sion of the file and open it.)

The only way to learn to use Vim is to use Vim. You might want to start small—only use Vim for tak­ing notes or for edit­ing con­fig­u­ra­tion files. Or you might want to go all in and start using Vim for all your text edit­ing needs. With a graph­i­cal ver­sion of Vim, you could use Vim just like any other text edi­tor and get by with­out know­ing hardly any com­mands. All you need to know is that ‘i’ will put you in insert mode (let you type a paper) and the Escape key will put you in nor­mal mode.

Vim can be really over­whelm­ing. I think the best way to learn to really use Vim is to pick a small sub­set of com­mands to use, and then keep adding sub­sets as you learn the one you are on. Start out by com­mit­ting to save your work by typ­ing :w in nor­mal mode instead of by click­ing File -> Save or Command+S (or Control+S). Or start by using the basic Vim nav­i­ga­tion: ‘h’ moves left one char­ac­ter, ‘j’ moves down one line, ‘k’ up one line, ‘l’ right one char­ac­ter. And maybe throw in ‘G’ to move to the bot­tom of the file and ‘gg’ to move to the top. Then, once you are com­fort­able with those, you can start using ‘(’ to move to the start of the sen­tence, ‘)’ to move to the end, and ‘{’ and ‘}’ to move to the next empty line. You might even think about mak­ing a cheat sheet that lists just a hand­ful of com­mands you are going to use. In no time you’ll be adding more.

(You can find some nice cheat sheets that will teach you a small­ish chunk of Vim at a time. Here is one I like, though the ‘lessons’ might include too many com­mands for a beginner.)

Posted Thursday, July 8th, 2010 under LaTeX, text editors.

15 comments

  1. Among all mate­r­ial avali­able to learn vi/vim, I would like to point out the videos in http://​www​.derek​wy​att​.org/​v​i​m​/​v​i​m​-​t​u​t​o​r​i​a​l​-​v​i​d​e​os/

    • Hi Felix, Thanks for that link. I just stum­bled upon the first video in that series the other day and found it really enjoy­able. But I hadn’t had a chance to look at any oth­ers. I’m glad to hear they are good. I’ll look for­ward to check­ing them out.

      (By the way, for some rea­son the links on Derek Wyatt’s page don’t go to the right places on Vimeo. So here’s a direct link to the videos Felix is talk­ing about.

      I also quite like Vim­casts, which I’ve men­tioned here before. There are lots of peo­ple out there try­ing to explain how to use Vim. I’m happy for it, since you can always learn some­thing new about it!

  2. I haven’t been play­ing with vim very long, but I do have one quick tip to add, which I’ve found handy, which is to remap Cap­sLock to Escape on your key­board. I can’t think of any cir­cum­stances in which I‘d ever inten­tion­ally hit the Cap­sLock key, nor any cir­cum­stances in which I’d want to. On the other hand, I hit it unin­ten­tion­ally all the time, which is annoy­ing. And the one key you need to press all the time when using vim which is far from the home row is Escape. So this kills two birds with one stone.

    (On Ubuntu run­ning gnome, you can do this through Sys­tem > Pref­er­ences > Key­board > Lay­out > Options > Cap­sLock­Key Behav­ior. I’m sure there are ways to do it for mac and Win­dows too. (For Win­dows you could use Auto­HotKey, etc.))

    Another good rea­son to learn vim occurred to me recently (–prompted in part by men­tion of try­ing to edit LaTeX using an iPad on Peter Smith’s blog–), which is that it has a text-only mode and so you can use it through SSH. The iPad has an SSH client. (I won’t buy an iPad myself, but I may some­day buy a sim­i­lar linux-based prod­uct; and this could be handy in a lot of other sit­u­a­tions too.) Using SSH you could con­nect even to your home com­puter and use your own LaTeX sys­tem remotely. (In case you need cus­tom pack­ages, etc., not pro­vided by the web-based LaTeX systems.)

    I just need to fig­ure out how to set up a vim server so you can use the LaTeX box com­piler with­out escap­ing back to the com­mand line. You know any­thing about that, Charlie?

  3. Kevin, Remap­ping Caps Lock is a good idea. I actu­ally favor remap­ping ‘;;’ to Escape, but the idea is the same (I did the Caps Lock thing one time, and all of a sud­den I needed all caps every­where. It drove me crazy, so I had to switch back. I don’t think I’ve needed Caps Lock again sense, though…)

    In an upcom­ing install­ment in this series of posts I’ll share some of my favorite key map­pings for Vim, and I’ll also share some plu­g­ins I like.

    Your ques­tion about a vim server is way over my head. I think you are the res­i­dent server guy, so if you don’t know the answer, I know I won’t!

  4. It has been sug­gested to me that remap­ping Caps lock to CTRL is a good idea for emacs, because to make some­thing all caps in emacs is a mat­ter of mark­ing the region you want capped and then doing C-x C-u. I won­der if there’s a sim­i­lar short­cut in vim that would mean you wouldn’t lose the “func­tion­al­ity” of caps lock…

    Also, given what ESC does in vim, it seems like map­ping it to a key one reg­u­larly hits acci­den­tally seems like a dan­ger­ous idea, no?

    • I don’t know if remap­ping caps lock to ESC is dan­ger­ous in Vim. If any­thing, caps lock is dan­ger­ous. A ‘U’ means some­thing dif­fer­ent than a ‘u’, and there have been times when I hit Caps Lock on acci­dent and then got into an odd sit­u­a­tion as a result. You can (almost) always undo any changes you make by hit­ting ‘u’ over and over (I’m sure there is a more effi­cient way), so if you were to go into nor­mal mode on acci­dent (via ESC), you likely wouldn’t do any last­ing dam­age on accident.

      There is a way to make a word, sen­tence, etc. all caps once it has been typed. You can high­light a region (hit ‘v’ to go into visual mode and high­light regions), then ‘U’ will turn that all into caps. A ‘u’ on a high­lighted region will make it low­er­case. A faster method is to nav­i­gate to the start of the sec­tion, then hit ‘gU’ and the key to nav­i­gate to the end of the sec­tion. So if you are in the mid­dle of a word, hit ‘bgUw’ (back, global, Upcase, word). If you are some­where in a sen­tence, hit ‘(gU)’ (back to start of sen­tence, global, Upcase, end of sen­tence). You can make every­thing low­er­case by swap­ping a ‘u’ for the ‘U’.

  5. I’m the server guy? How do you figure?

    About the server thing, turns out you just need to call vim from the com­man­d­line with –server­name [what­ever] option. I’m not sure why you need to do this to call exter­nal tools when con­nected through SSH but for some rea­son you do.

    • I took your host­ing the 21st Cen­tury Mon­ads to be evi­dence that you were run­ning a server. But even if I was wrong about that, I was right in assum­ing you would fig­ure out how to use vim via SSH before I even looked into it!

  6. Nah, I just run their site on the UMass servers. (Hey, it’s edu­ca­tional!) I’m too cheap to pay the costs nec­es­sary to run my own server, or even for my own domain name.

  7. Carl Martin says:

    I’ve made the habit of hit­ting Ctrl+C to enter Nor­mal Mode. This is default in most vim builds, and I find it much more intu­itive and sta­ble than to remap.

    Other than that: Great site! I can’t think of a web­site that fits my niche inter­ests more pre­cisely than this page (being a Phi­los­o­phy Major and a com­puter enthusiast).

    • Hi Carl,

      Glad to hear you find the site inter­est­ing. I actu­ally hadn’t real­ized that Control+C would do that. But that makes sense now that you men­tion it–the same com­mand kills lots of servers you run in the ter­mi­nal and what­not. I guess it is a sort of generic kill but­ton. I can see the appeal of this over a remap, though I think my pinkie prefers the remap option!

  8. Hi Char­lie

    Is there a way to map the con­trol key to the com­mand key in Mac when I use VIM. I find using the ctrl key dif­fi­cult and would like to map it to the com­mand key when­ever I am using VIM.

  9. Hi Kaarthik, Hmn. I don’t know about that. You might try the stick­ykey plu­gin. I haven’t tried it, and it doesn’t do quite what you want, but it looks like you could maybe set it up so that ;o is like press­ing the ctrl key. That would pre­vent you hav­ing to hit ctrl (though not in the way you wanted).

    Here is a thread of peo­ple dis­cussing your ques­tion. Doesn’t look promising.

    My only other sug­ges­tion off the top of my head is that you might be able to do this with Viking. The new (or per­haps sim­ply pro­posed) con­fig­u­ra­tion file looks like it might allow you do remap those keys just in MacVim. But I haven’t tried it so I can’t say for sure.

    Let us know how it works out if you solve this!

Leave a Reply