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.

(add-hook 'abg-code-modes-hook
          (lambda () (linum-mode 1)))

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.

(add-hook 'ruby-mode-hook
          (lambda () (run-hooks 'abg-code-modes-hook)))

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.


[boilerplate bypath=”emacs-reboot”]

Published by Avdi Grimm


  1. (setq linum-format “%4d “) makes line numbers look much prettier

  2. For the record, prog-mode-hook does basically what your code-modes-hook does if you’re on Emacs 24.

  3. I added a space after the line number by adjusting the definition of the dynamic format.
    The function linum-update-window defines the dynamic format; in the compiled version of the function, I changed the “d” part to “d “.

    I’m using Emacs 24.3.1. The dynamic format is defined in linum-update-window on line 150 of /usr/share/emacs/24.3/lisp/linum.el.gz and on line 75 of the compiled version in /usr/share/emacs/24.3/lisp/linum.elc.


Leave a Reply to Avdi Grimm Cancel reply

Your email address will not be published. Required fields are marked *