I am attempting to set up the new emacs 26.1 display-line-numbers-mode as described in this comment: hybrid line number mode in emacs?

I have added this to my .emacs file:

(setq display-line-numbers 'relative
      display-line-numbers-current-absolute t)

When I evaluate the above expression the desired behavior is produced, but it is not persistent. For example, it is not automatically evaluated on restarting emacs, and when I try to use my keybinding toggle:

(global-set-key (kbd "C-c l") 'display-line-numbers-mode)

The line numbers that are toggled are not relative.

What is the appropriate way to set display-line-numbers to relative in my configuration file so that using the keybinding results in relative line numbers?


Use setq-default to set buffer-local default values. Or, make sure you are in the desired buffer if you wish to use setq.

C-h v [aka M-x describe-variable] for display-line-numbers: ". . . Automatically becomes buffer-local when set."

display-line-numbers-current-absolute is a global variable. [How do we know? Because the *Help* buffer for describing that variable does not mention anything about it being buffer-local.]

The variable display-line-numbers-type is a global variable with a doc-string that states: "The default type of line numbers to use in ‘display-line-numbers-mode’. See ‘display-line-numbers’ for value options." Therefore, the O.P. could use (setq display-line-numbers-type 'relative) in the .emacs or init.el file to achieve the desired result.

  • Apologies - I'm new to emacs so I'm still not sure how to proceed. I've tried setq-default with display-line-numbers 'relative in my .emacs file but it does not seem to work.
    – Marcel
    Jun 1 '18 at 1:41
  • If I evaluate (setq-default display-line-numbers 'realtive) and then type M-x describe-variable RET display-line-numbers RET, the *Help* buffer says: "... Its value is ‘realtive’. Original value was nil." If you place that same code in your .emacs or init.el file, it should show the same thing if you restart Emacs and type M-x describe-variable ... Is that what it says?
    – lawlist
    Jun 1 '18 at 1:47
  • Yes, that is true, and it is enabled in all new buffers. However, using C-c l as set above reverts it to absolute line numbering.
    – Marcel
    Jun 1 '18 at 1:51
  • C-c l is undefined in a new installation of Emacs 26.1 (with no user-configuration) in fundamental-mode. What function is your C-c l bound to? Type: C-h k or M-x describe-key.
    – lawlist
    Jun 1 '18 at 1:56
  • I'm trying to enable toggling of line number mode as well. I'm using: (global-set-key (kbd "C-c l") 'display-line-numbers-mode)
    – Marcel
    Jun 1 '18 at 1:59

So I am an emacs novice myself, but the way I understood it (and have it in my init.el file):

;; set type of line numbering (global variable)
(setq display-line-numbers-type 'relative) 

;; activate line numbering in all buffers/modes

You could also be more specific and only have line numbering in a subset of major modes by using hooks. For instance:

;; Activate line numbering in programming modes
(add-hook 'prog-mode-hook 'display-line-numbers-mode)

Source for the last snippet.

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.