Looking to set relative line numbers when in programming modes. Looking through the older posts I see a way to set line numbers based on the mode - How do I display line numbers in emacs (not in the mode line)? :

@AnthonyB : (add-hook 'prog-mode-hook 'linum-mode)

Currently, use (setq-default display-line-numbers 'relative) in my init file for relative line numbers, however, would like to use this setting on a per mode basis partially all programming modes.

1 Answer 1


display-line-numbers is a buffer-local variable. To enable line numbers for only certain modes, do not use setq-default. Instead, use setq ... something like this:

(add-hook 'prog-mode-hook (lambda () (setq display-line-numbers 'relative)))

Or, use a preferred major-mode hook such as the perl-mode-hook or html-mode-hook:

(add-hook 'name-of-major-mode-hook (lambda () (setq display-line-numbers 'relative)))
  • Thanks this is just what I was looking for.
    – hicksca
    Commented Jul 5, 2019 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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