2

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?

1

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

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.