16

I am asking this after having read this post:

How do I display line numbers in emacs (not in the mode line)?

I have tried using: M-x linum-mode and display-line-numbers-mode

After closing emacs and reopening, they are gone. Is there not a way to have emacs permanently display the line numbers?

3 Answers 3

21

In order for your changes to persist, you need to add them to your init file. If you've used any customize functionality, it probably generated one for you, and it's saved in ~/.emacs. If that isn't the case, you're better off putting it in ~/.emacs.d/init.el.

In order to have line numbers in all buffers and have them persistently, you can put

(global-display-line-numbers-mode)

into your init file.

If you're using an older version of Emacs, you'll want to use linum-mode, in that case you should put this in your init file isntead:

(global-linum-mode)
4
  • Both of those files are empty for me. And adding that line did nothing in both cases.
    – user1394
    Commented Oct 26, 2018 at 21:12
  • You either have to evaluate contents of your new init file to see the changes right away, or restart Emacs. Also, if you have both files, ~/.emacs will take precedence if I recall correctly, so make sure you have only one of these files.
    – user12563
    Commented Oct 26, 2018 at 21:37
  • Thank you for your continued help. I restarted everything. I am putting that line in an empty ~/.emacs file. And getting this error: Symbol's function definition is void: global-display-line-numbers-mode
    – user1394
    Commented Oct 26, 2018 at 21:42
  • 1
    (global-linum-mode) for older versions of Emacs.
    – phils
    Commented Oct 27, 2018 at 9:12
2
  1. Menu Options > Show/Hide > Line Numbers for All Lines > Global Line Numbers Mode

  2. Menu Options > Save Options

Menus are your friends. They can be a good way to discover Emacs features.

You can also alternatively use Customize to set the equivalent option directly: M-x customize-option global-display-line-numbers-mode.

3
  • I only have a 'Line Numbers' completion (I do not see a 'Line Numbers for All Lines') under 'Show/Hide' and choosing that one simply toggles line number mode.
    – user1394
    Commented Oct 26, 2018 at 21:52
  • I see. Your question doesn't specify any Emacs release. I guess you need release 26 or later to have those menu entries and option global-display-line-numbers-mode. I'll leave the answer, as it might help someone else.
    – Drew
    Commented Oct 26, 2018 at 23:10
  • Ah I see. Makes sense. Thank you and will just assume this is not possible in my release (I'm working on a specific server which I cannot modify).
    – user1394
    Commented Oct 26, 2018 at 23:31
-1

I had the same problem, and when I was looking for an unrelated issue, I found this in Stack Overflow:

The problem for me was the Initial Start Up Screen of emacs. It seems that the default welcome screen of emacs (with the tutorial links) forces you to start in your $HOME path.

Since the Initial Start Up Screen has to be shown in a certain way, it seems like it disables line numbering and other things. Therefore, by adding

    (setq inhibit-startup-message t)

to .emacs made some changes, such as line numbering, become persistent for me.

4
  • You're conflating unrelated issues here. Whether or not you inhibit the start screen has no bearing (whatsoever) on the ability to configure line numbers or other things.
    – phils
    Commented Jun 28, 2021 at 12:20
  • Note that every Emacs buffer has a default-directory associated with it, and it simply happens that the value in the startup message buffer will indeed be your home directory. If you dismiss that buffer or otherwise switch to another one, then you will be viewing some other buffer with its own default-directory value which may be different. If it's a file buffer, it will be the directory of that file path.
    – phils
    Commented Jun 28, 2021 at 12:22
  • @phils I understand your point about default-directory. It makes sense, I had already noticed that before, and I am not suggesting that you can change the default directory for every buffer. But the Save Option in the Options menu only started working for line numbers after I inhibited the startup message. Why did the inhibition affected the line numbers? Commented Jun 28, 2021 at 14:16
  • I can't reproduce that, nor can I conceive of a reason how or why it would happen, so to me it sounds like the inhibit-startup-message change was purely coincidental to your issue. If you can reproduce this behaviour, let us know which version of Emacs you are using, and ideally a recipe to demonstrate the issue.
    – phils
    Commented Jun 28, 2021 at 22:00

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.