25

How can I add line numbers to Emacs?

Please explain, I'm an absolute beginner.

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 – Florian Diesch May 6 '11 at 19:44
  • @Florian: Good catch. I added the instructions for linum-mode to the answer. – geekosaur May 6 '11 at 19:48
  • Ok, you've been faster - deleted my answer – Florian Diesch May 6 '11 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 '15 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. – Kirill Yunussov Oct 1 '17 at 16:24
  • Could also be in the "C:\Users\username\" directory – Kirill Yunussov Oct 2 '17 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 '18 at 8:02

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.