Emacs Reboot #11: Line Numbers

Out of the box, Emacs shows me the current line number in the modeline, not the column number. And it doesn’t show any line numbers down the side of the page. Today I’m going to change that.

First of all, I customize column-number-mode to be globally enabled. Now when I move the cursor around in a buffer, I can see two numbers changing in the modeline: the line number, and the column number.

I find that having visible line numbers is tremendously useful for remote pair-programming sessions. So I want to enable line numbering down the left-hand-side of the buffer. But unlike column-number-mode, I don’t want to enable it globally. I just want it to be enabled for source code files, like Ruby files.

I open up my init.el, and define a new hook called abg-code-modes-hook. This will be a kind of meta-hook which gets called whenever any code file is loaded. For now, the only action I’ll attach to this hook is one which turns on line-numbering using linum-mode.

Now to make sure that code gets called when a Ruby file is loaded, I attach my abg-code-modes-hook to the ruby-mode-hook, using the run-hooks function.

I save the file and evaluate both of these statements. The next time I open up a ruby file, I see line numbers down the side! But when I open up a plain-text file, there are no line numbers.

This post is part of the Emacs Reboot series. Check out the rest of the series for more Emacs goodness. You can also subscribe to a podcast feed of just the videos.