vim to get you ready for clojure
There’s a guy here in Nashville that is working really hard to get a Lisp programmers meetup to gain traction. I’m working almost as hard to try and actually get to one of those meetups. He’s succeeding, I’m failing. But, I was able to contribute in some small way by writing up some quick notes on vim and clojure, and even though I’m not sure if anyone was able to use it, because again, I can’t seem to get there, it certainly helped me and now, maybe you. The Lisp meetup is focusing on web dev in clojure right now and I’ve recently spent several hours trying to get that kind of setup working with vim, vim-fireplace and lein. It turns out it’s pretty easy, but finding what I needed in one (or two or even five locations) was elusive. So, I’m putting it all in one place; I hope it helps someone else.
If you’re familiar with vim, skip ahead, there’s nothing new for you here. If you’re not familiar with vim, welcome to a whole new world! Although it may be sacrilege, I suggest starting with gvim or MacVim instead of the command line versions. More than likely you’ve come from other GUI based apps and having that mouse as a crutch is actually really helpful. Once you’re comfortable, you can move to the command line version and keep the neck-beards off your back :) If you’re on Mac or some version of Linux, you should have the command line version of vim installed, but you will likely need to install the gui version. Just google how to install it on your system. Vim is powerful, but not intuitive. Once you learn the muscle memory of moving around and editing, you’ll love it, I promise, so even if you’re feeling frustrated at first and think that it’s a ridiculous way to edit your files and you’ll never be as fast in vim as you can be in Textmate or Intellij or whatever you’re used to, just stick with it for a while. It may end up not being the right editor for you, but if it ends up sticking with you, you’ll have an editor for life.
When you start vim, you’re immediately in Normal mode, which you might think means you can start typing code, but that’s a ridiculous thought! You obviously need to be in Insert mode for that; duh. Normal mode is where you move quickly around your code (very quickly) and from where you can enter into other modes such as Insert mode or Command Mode or Visual mode or Depeche mode. Wait, nope, ignore that last one. Anyway, hit a lower case “i” (no quotes) and you’re in Insert mode. Type a few lines of text so you’ll have something to work with and then hit the ESC key. Now you’re back in Normal mode. You can always thump the ESC key a few time to get back to Normal mode and then “i” lets you insert.
Vim is designed to be efficient at text editing and a lot of times that means moving quickly to various parts of your text; you can move around your text in Normal mode without having to leave the home row. “h” moves left, “j” moves down, “k” moves up and “l” moves right. Try it. Now, want to see something awesome? Type 3j and watch your cursor jump down three lines. Type 5 l and watch your cursor move 5 to the right. Take a breath and let that sink in. Ready for more? move to the bottom line of your text and then type gg. And now you’re at the top! Type “G” (capital) and now you’re back at the bottom! This is just the beginning of how easily you can move around in vim. There are some great tutorials out there for moving around, check them out to go beyond these basics. And keep your fingers off those arrow keys! It’s a hard habit to break, but worth it.
Command mode is where you enter commands. From Normal mode, you enter Command mode by typing “:” and you’ll see the : appear in the bottom left of vim. So, you can save your file by :w <filename> (w for write), you can open a different file with :e <path to file> (e for edit), and you can quit vim with :q . If you have changes you haven’t saved, vim will not quit until you save them or you can quit and lose your changes with :q! . You can also enter Command mode for searching by starting with “/” instead of “:” . Type “/” and then the pattern that you’re looking for and hit enter; vim will take you to the first occurrence of that pattern. Hit “n” to go to the next occurrence, hit “N” to go to the previous occurrence. By default searching is case sensitive and I’d suggest leaving it that way.
The last mode you’ll need is Visual mode; this is where you can highlight text for copy/paste funness. From Normal mode, hit “v” and vim will highlight the space that the cursor is on and you’ll see the word Visual in the bottom left of the screen. You can then use the movement keys (h,j,k,l) to move around and highlight the text you need. You can even use the movements like 4j to highlight down four lines or 10h to highlight 10 characters to the left. “V” will highlight the entire line and Ctrl-v will enter block-highlighting, which lets you copy columns of data. I actually Ctrl-v a lot, because if you use it to highlight the first column of characters along a few rows, then hit capital I (for insert) type some text and then hit ESC, all the text you typed will be copied in from of every line you highlighted. I use it for commenting out code in Python; I put my cursor on the first letter of the first line, hit Crtl-v, move down to the last line I’m interested in and hit I then # then ESC and vim puts a # in front of all those rows to comment them out. This may be the feature that made move from infatuation to love.
Once you have your text highlighted, you can hit “y” to copy (y for yank) then move to where you want to paste the text and hit “p” (for put, not paste. Don’t ever think otherwise). When you have your text highlighted and hit the “y”, vim takes you out of Visual mode and drops you back into Normal mode, so you’ll see your cursor as a block over an entire text character. “p” puts the yanked text after the cursor. You can also yank entire lines by moving your cursor to anyplace on the line and hitting “yy”. When you yank an entire line this way, you can still put it with “p”, but it will put the line under whatever line you’re on. You can also delete and put text with “d” or “dd” and then put it with “p”.
Ok, that’s a very, very brief introduction to vim. They key is to build up the muscle memory so all these commands (and so many more) become second nature. Once you do, your code editing will jump to a new level.
In the next part, I’ll show you how I got vim-fireplace talking to the clojure repl.