Reading Code with Emacs

I have long believed that one of the best ways to move from journeyman to master coder is to read the code of the masters. I learned most of my advanced C techniques by reading the Unix source code. Other languages have different masters that you can learn from. Nathaniel Knight has a post that suggests some convenient methods of reading code with Emacs. These boil down to learning the marking and narrowing commands.

Once you learn to conveniently mark expressions and functions, for example, you can narrow to the region and look just at the code you’re interested in. That may not seem like a huge win but as Knight explains, it often makes working with the code easier. Once you get in the habit of using narrowing, you’ll want to take a look at Artur Malabarba’s excellent post on narrow-or-widen-dwim. It’s really great because you need only call a single command and it figures out what you want to do by context. It’s a huge timesaver.

Knight also covers the little-known clone-indirect-buffer command. That’s just what you need when you want to narrow to two (or more) separate areas at the same time. Again, the utility of doing this may not be obvious but it turns out to be tremendously useful. One common use case is where you have different types of code in the same buffer. You can clone the buffer, narrow to the desired code segments, and then work in the appropriate Emacs mode for each segment independently.

You can follow Knight’s recipe for doing this but there’s an easier way. The code discussed at the link is just a little bit of Elisp to automate the cloning and narrowing steps but it’s a real time saver. Follow the link to Zane Ashby’s post (at the easier way link) to see an example of the use case I mentioned.

This entry was posted in General and tagged . Bookmark the permalink.