Friday, June 19, 2009

An Introduction to Clojure's Agents

For anyone that's interested in alternative programming languages, I've written a lengthy introduction to Clojure's agents.

For those unfamiliar with Clojure, it's a modern Lisp running on the JVM with excellent support for concurrency. It allows excellent interoperability with Java, so essentially you get a dynamic and clean programming language with a huge standard library. On the performance front, it's often on par with Java. I've spent a good deal of time learning the language lately, and I have to say that it's very enjoyable to work with.

One huge bonus of working with Clojure inside Vim is the VimClojure project. It allows interactive programming from inside of Vim and proxies code between a stateful NailGun server and the editor. To be honest, I wasn't aware that an interactive REPL was possible inside of Vim until I installed this, and I'm wondering if there are any other projects out there using similar techniques with different languages.

Wednesday, June 17, 2009

Switching from Horizontal to Vertical Split

If you have two windows which are horizontally split, and you'd like to make them vertically split instead, the following command sequence works well:


To orient them back to a horizontal split do this:


Note that if the top (or left) window is already current you can omit the <ctrl-w>t and simply do <ctrl-w>H or <ctrl-w>K.

Monday, June 15, 2009

Vi Mode in Readline Applications

Mike Pea submitted this comment in a recent tip, and I thought it deserved it's own post.

If you find that you do like using vi editing mode (and what's not to love about it :), add 'set editing-mode vi' to your .inputrc, and vi mode will work in any readline based client, eg psql, irb.

Thanks Mike!

Friday, June 12, 2009

Recursive Macros

A friend at work pointed out to me yesterday that he had accidentally written a recursive macro in Vim. I had never thought about the fact that this was possible, but I'd imagine there could be some potential usefulness for these somewhere. As a trivial (and useless) example, here's a recursive macro to move the cursor to the end of the current line one step at a time: qal@aq

Edit: A little research has shown that this is an excellent way to repeat a macro to the end of the file. Say that you have a file containing one number per line from top to bottom:


Put the cursor on the first line and type the following:

qaq (clears register "a" of any previous macros)

Now type @a once more, and the macro will run to the bottom of the file incrementing the value on each line by one leaving you with the following:


Friday, June 5, 2009

Screen in OS X Leopard

Sorry for the horribly platform specific tip, but this has been annoying me, and I thought it might help someone. If you're running OS X 10.5, you may have noticed that screen destroys your path when you start it. This can easily be remedied by putting the following in your ~/.screenrc.

shell -/bin/bash

Thursday, June 4, 2009

Vim + Eclipse = Eclim

I know some of you may consider this to sacrilegious, but a lot of programmers are subjected to using Eclipse from time to time for various reasons. For those of you that need Eclipse for a given plugin, debugging session, or what have you, the Eclim project provides a flexible solution for integrating Vim with Eclipse.

Using Eclim gives you three options for how you'd like to integrate Vim and Eclipse. The least intrusive allows you to run a headless Eclipse and control it via Vim. This makes Eclipse act as sort of an app server, which Vim can pipe to and from.

The second option allows you to run both programs side by side. This way, Vim is still running standalone, but you can control Eclipse directly should you need to.

The final option allows you to embed Vim directly into Eclipse. You'll lose some screen real estate when using this option, but it may suit certain people's needs.

I'm not really a fan of Eclipse by any stretch of the imagination, but for things like Java debugging, it does offer some utility, so if you're in the same boat, you should give Eclim a try.

Wednesday, June 3, 2009

Recursively Replace Ctrl-M

Ctrl-M's are a plague inflicted upon programmers everywhere. I've mentioned a number of ways to deal with them in the past, and some readers have also contributed helpful advice as well. Today I ran into a situation where I needed to do a huge source diff on two different directory trees. My preprocessor happened to strip out all Ctrl-M leading to a bunch of false positives on files that differed. Some Google'ing around lead me to find this gem, which recursively strips Ctrl-M on all files within a given directory.

for file in $(find /path/to/dir -type f); do
tr -d '\r' <$file >temp.$$ && mv temp.$$ $file

In order to give credit where it's due, a user named blowtorch gave the tip at the following forum.

Disable Comment Autocompletion

There are times when Vim's autocompletion of comments gets in my way more than it helps me. Fortunately, disabling comment autocomplete is easy. Just add the following to your vimrc.

au FileType * setl fo-=cro

Tuesday, June 2, 2009

Snippit Support

Next time someone who uses TextMate starts blabbering on about how awesome snippits are, politely remind them that Vim supports snippits as well. You can download the snippitsEmu plugin here, and it works quite well. For those unfamilar with snippits, they allow tab-style completion of boilerplate code. For example, you might type class, hit tab, and then a template for a class will be expanded based on the current filetype Vim is set to.

Update: Several readers have cited snipMate as a preferred alternative to snippitsEmu. Thanks guys!

Monday, June 1, 2009

Easier Command Line Editing

Assuming you're using the bash shell, the following can be helpful when composing long command lines.

Start typing on the command line and then type Ctrl-x Ctrl-e, it should drop you into your system's default editor (hopefully Vim) and allow you to edit the command line from there. Once finished, save the command line, and bash will run the command.