65

I know how to display line numbers (and columns) in the mode-line, but I'd like emacs to display the line numbers for all the lines on the left-hand side. I've seen this done in vim and other text editors. How do I do this for emacs?

0

8 Answers 8

84

For Emacs version >= 26 you should use display-line-numbers-mode or global-display-line-numbers-mode.

You can run the command with M-xdisplay-line-numbers-modeRET or M-xglobal-display-line-numbers-modeRET to try it.

Another good idea is to use (add-hook 'prog-mode-hook 'display-line-numbers-mode) in your init.el to automatically enable that mode in all programming modes.

See EmacsWiki

2
  • 3
    You might also find (global-display-line-numbers-mode) useful. Jan 14, 2019 at 13:15
  • I'm not sure what init.el is, but I assume you can add that to your ~/.emacs file Aug 13, 2022 at 10:38
22

M-x linum-mode will do the trick. If you wish to effect this for all buffers, then M-x global-linum-mode will do this.

2
  • Do you know a way I can make this permanent? Sep 25, 2014 at 22:09
  • 2
    (per the suggestion of nispio and mgoszcz2) addition of (global-linum-mode) to your .emacs.d/init.el file will make it permanent. I find global-linum-mode to be a bit excessive, and mgoszcz2's suggestion for enabling it only in certain modes is a good one.
    – Eric Brown
    Sep 25, 2014 at 22:24
17

If you are using Emacs 26 or newer, you can use display-line-numbers-mode.

from etc/NEWS.26:

** Emacs now supports optional display of line numbers in the buffer. This is similar to what 'linum-mode' provides, but much faster and doesn't usurp the display margin for the line numbers. Customize the buffer-local variable 'display-line-numbers' to activate this optional display. Alternatively, you can use the 'display-line-numbers-mode' minor mode or the global 'global-display-line-numbers-mode'. When using these modes, customize 'display-line-numbers-type' with the same value as you would use with 'display-line-numbers'.

1
  • 3
    I like it so much, but my muscle memory still goes M-x linum-mode - so I did (advice-add 'linum-mode :override 'display-line-numbers-mode). Jun 21, 2021 at 22:10
8

nlinum-mode is another option. It has the same interface as linum-mode, so you can use the same hooks, but it uses a different technology to be more performant. Quote from nlinum.el:

;; This is like linum-mode, but uses jit-lock to be (hopefully) more efficient.

You can install it via GNU Elpa via the usual list-packages.

http://elpa.gnu.org/packages/nlinum.html

4

Certainly linum-mode is a good choice. If you don't always want to see line numbers, like me, you can use this trick from the excellent What the .emacs.d!? blog.

This temporarily enables linum-mode when you run goto-line.

Here's the code from his blog:

(global-set-key [remap goto-line] 'goto-line-with-feedback)

(defun goto-line-with-feedback ()   "Show line numbers temporarily, while prompting for the line number input"   (interactive)   (unwind-protect
      (progn
        (linum-mode 1)
        (goto-line (read-number "Goto line: ")))
    (linum-mode -1)))
1

This is what my teacher tells us to set line-display-mode always on. Add this in your .emacs file.

(add-hook 'c++-mode-hook 'display-line-numbers-mode)
(add-hook 'c-mode-hook 'display-line-numbers-mode)
(setq linum-format "%3d ")

OTTO: Professor William Knottenbelt

1

Press M-x and type just linum (or even linu will work), then press Enter to automatically execute linum-mode. No need to type the longer linum-mode.

Also, usually you just want to go to one chosen line of interest because of an error message. Then press M-x, type goto-line, press Enter and type the line of choice.

0

A better way may be is to use prog-mode as a whole instead of doing for all programming language.

(add-hook 'prog-mode-hook 'display-line-numbers-mode)

Though it doesn't work for mode other than programming langauges. But it is what i need.

Your Answer

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

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