25

How can I add line numbers to Emacs?

Please explain, I'm an absolute beginner.

2 Answers 2

27

If you're running it in default mode, it's in the menu: Options > Show/Hide > Line Numbers. Then Options > Save Options to save it for future sessions.

If you're running it in a terminal (emacs -nw), then it's M-x line-number-mode. M- is usually the Windows key, but may be Alt, or you can press Esc followed by x.

(Edit: The above only affects the mode line. For line numbers in the margin, you want M-x linum-mode, or more likely (setq global-linum-mode t) in your ~/.emacs.)

4
  • 2
    That only displays the current line number in the mode line May 6, 2011 at 19:44
  • @Florian: Good catch. I added the instructions for linum-mode to the answer.
    – geekosaur
    May 6, 2011 at 19:48
  • Ok, you've been faster - deleted my answer May 6, 2011 at 19:55
  • 9
    (setq global-linum-mode t) is not working when I add this line to .emacs.d/init.el, I just change it to(global-linum-mode t) and it works. Any reasons why?
    – ZijunLost
    Dec 28, 2015 at 2:15
21

Add (global-linum-mode t) to ~/.emacs.d/init.el

3
  • 1
    This worked for me. Heads up for Windows users - this file is located in C:\Users\username\AppData\Roaming directory, and is called ".emacs". Just add (global-linum-mode t) at the end of the file. Oct 1, 2017 at 16:24
  • Could also be in the "C:\Users\username\" directory Oct 2, 2017 at 12:49
  • how do you add a space between the line number and the text? because adding just (global-linum-mode t) shows the line numbers and the text without spaces.
    – m4l490n
    Jan 18, 2018 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .