Capturing Code Snippets

The other day in my post about Alain Lafon’s talk on playing Emacs like an instrument—you have watched that video, haven’t you?—I mentioned that one of the things I picked up was an Org capture template for capturing code snippets. That’s very nice and just the thing when you want to put some code in a journal or log entry.

Right after seeing the video, I saw a post from Nick Barnwell that had an even better template for capturing code snippets. His template automatically captures the language for the code block, and the file and line number from which the code is being extracted. That minimizes the amount of text that you have to enter manually.

You may have to autoload which-function if you aren’t already using something like which-function-mode. Also note that the template is assuming you’re going to be grabbing the code snippet from a file you have open in Emacs. That’s probably almost always true for a capture template of this type.

When I first started using capture templates, they were described as a way of quickly recording a note or thought while you were busy with something else and then getting back to the job at hand. That’s still true, of course, but I find that they’re just as useful for entering data related to your current task. For example, when I’m reading an article that I’m considering writing about on Irreal, I capture a link to the article and any thoughts I have about it with a template that adds the information to my blog queue. Once you start using capture templates, you keep finding new uses. Barnwell’s post is another example of that.

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