2

Does anyone know of a quick method to get hexadecimal line numbers in emacs? I am using linum for line numbers. I suppose I could hack it, but just thought to ask first.

6

Linum mode provides the linum-format customizable variable that allows you to format how you want your line numbers to appear. Set this variable to have the value "%x" to get hexadecimal line numbers. This can be done either through the built-in customization process or by running (setq linum-format "%x").

Edit: You may only want to have hexadecimal line numbers for certain buffers and decimal numbers for the rest. In that case you will want to make a buffer local version of that variable ((make-variable-buffer-local 'linum-format)) and then set linum-format to be "%x" for each buffer you want to display hexadecimal numbers.

Edit 2: I'm not sure why I didn't include this before, but here's a function that makes sure linum is turned on, creates the buffer local variable and sets it to display hexadecimal line numbers (and a corresponding function to delete this buffer local variable):

(defun my-display-hexadecimal-line-numbers ()
  (interactive)
  (linum-mode t)
  (make-variable-buffer-local 'linum-format)
  (setq linum-format "%x"))


(defun my-return-to-default-linum ()
  (interactive)
  (kill-local-variable 'linum-format))

As an aside, if you are using Emacs 26 or later, you may want to use the built-in display-line-numbers-mode instead of linum. This built-in mode is much faster and doesn't suffer the lags that sometimes occur when using linum. The downside is that I'm not sure if there is as easy of answer to this question when using display-line-numbers-mode.

| improve this answer | |
  • Thanks! Never knew it was that easy. As to the alternative, alas -- running 25.3 here... – Roadowl Jul 12 at 0:32
  • 1
    I might add the following for people coming here: I wanted the hex line numbers for a buffer that was basically a hex dump of some file. In such cases, you usually also want zero based line numbering. To achieve that, I found this: emacs.stackexchange.com/questions/20008/… (the answer from PythonNut). – Roadowl Jul 12 at 21:22
  • And thanks for the edits re. make-variable-buffer-local. – Roadowl Jul 12 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.