3

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?

Marcel
  • 33
  • 1
  • 3

2 Answers2

3

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.

lawlist
  • 18,826
  • 5
  • 37
  • 118
  • 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 01 '18 at 01: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 01 '18 at 01: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 01 '18 at 01: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 01 '18 at 01: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 01 '18 at 01:59
  • If we type `M-x find-function RET display-line-numbers-mode RET`, we see that the variable `display-line-numbers` is set to the value of `display-line-numbers-type`. And, we already know now that `display-line-numbers` is a buffer-local variable and `setq` sets that variable locally in the buffer that has been selected. If we type `M-x describe-variable RET display-line-numbers-type RET`, we see that it is a global variable whose value is `t`. FYI: I had to evaluate `(require 'display-line-numbers)` to load the entire library to look-up the aforementioned variable. Set it to `'relative`. – lawlist Jun 01 '18 at 02:05
  • Perfect. (Thanks for the lesson also.) – Marcel Jun 01 '18 at 02:12
1

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
(global-display-line-numbers-mode) 

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.