This page is about displaying line numbers in a buffer, or otherwise indicating line numbers, without actually changing the buffer content. If you just want to work on a specific part of the file and you are accustomed to doing that by showing line numbers in the margin, try BasicNarrowing instead.
See Also: The code on this page just displays line numbers without actually changing the buffer content. NumberLines will add the line numbers to the buffer content.
display-line-numbers-mode
is available since Emacs 26.
For older Emacs, use M-x linum-mode
. It could slow down emacs when viewing file with more than tens of thousands of lines. See https://github.com/redguardtoo/emacs.d/issues/178 for bug report and solution. The solution is simple: disable `linum-mode` or any linum-mode alternative when file is too big.
You can enable it globally by appending this to your .emacs
file:
(when (version<= "26.0.50" emacs-version ) (global-display-line-numbers-mode))
To disable this in certain major modes you can redefine display-line-numbers--turn-on
:
(require 'display-line-numbers) (defcustom display-line-numbers-exempt-modes '(vterm-mode eshell-mode shell-mode term-mode ansi-term-mode) "Major modes on which to disable the linum mode, exempts them from global requirement" :group 'display-line-numbers :type 'list :version "green") (defun display-line-numbers--turn-on () "turn on line numbers but excempting certain majore modes defined in `display-line-numbers-exempt-modes'" (if (and (not (member major-mode display-line-numbers-exempt-modes)) (not (minibufferp))) (display-line-numbers-mode))) (global-display-line-numbers-mode)
If you just want the current line number in your modeline (and it isn’t there already), use LineNumberMode by running the M-x line-number-mode
command.
If you just want to know the current line number that the TextCursor is on, you can use the M-x what-line
command (which is usually not on a key). Or use count-lines-page
(C-x l
) that reports the total number of lines as well as the numbers of lines before and after the current one.
JerryChen had a line-numbers-mode on Geocities. It seems to be XEmacs specific. Screenshot.
The nlinum.el
library, available from GNU ELPA (http://elpa.gnu.org/packages/nlinum.html) is meant as a replacement for linum.el, using the jit-lock infrastructure to fix various corner case bugs in linum.el. It should usually be about as fast or faster than linum.el.
To precalculate the line number width to avoid horizontal jumps on scrolling:
;; Preset `nlinum-format' for minimum width. (defun my-nlinum-mode-hook () (when nlinum-mode (setq-local nlinum-format (concat "%" (number-to-string ;; Guesstimate number of buffer lines. (ceiling (log (max 1 (/ (buffer-size) 80)) 10))) "d")))) (add-hook 'nlinum-mode-hook #'my-nlinum-mode-hook)
Note: a previous version set nlinum--width
and then called nlinum-flush
, but that can cause problems in nlinum’s timers, see http://debbugs.gnu.org/cgi/bugreport.cgi?bug=23777.
the lastest Nlinum-mode git version(1.6) not worked with emacs deamon mode, use emacsclient to open a file, will result a “ERROR: Invalid face: linum” error, following hack worked for me on emacs-version 24.5.1.
(defun initialize-nlinum (&optional frame) (require 'nlinum) (add-hook 'prog-mode-hook 'nlinum-mode)) (when (daemonp) (add-hook 'window-setup-hook 'initialize-nlinum) (defadvice make-frame (around toggle-nlinum-mode compile activate) (nlinum-mode -1) ad-do-it (nlinum-mode 1)))
- zw963(Billy)
Library linum.el
works incrementally and can number large files very quickly.
http://stud4.tuwien.ac.at/~e0225855/linum/linum.html
linum.el
is distributed with Emacs in versions after 22 (currently only CVS Emacs)
Seems very good, to me. It looks like a good candidate to replace setnu.el
. – DrewAdams
I was wondering if there is way to change the color of the line numbers e.g change color/change background color so that it can be readily differentiated from the neighboring text. Great code. Thanks – Saptarshi Also is there a way to turn on linum mode automatically for any opened file? - Thanks Saptarshi
M-x customize-face RET linum
. Also try M-x global-linum-mode
, or append (add-hook 'find-file-hook (lambda () (linum-mode 1)))
to your .emacs
.(global-linum-mode 1)
to your .emacs
.Note: adding (global-linum-mode 1)
to your .emacs
will cause Emacs to ask “Options have changed - save them?” when exiting. Clearly this is not a good way to enable linum-mode
globally.
Another workaround that keeps the appearance of the dynamic linum-format is to use this function which replaces the spaces on the left with zeroes that have the same foreground and background color.
(eval-after-load 'linum '(progn (defface linum-leading-zero `((t :inherit 'linum :foreground ,(face-attribute 'linum :background nil t))) "Face for displaying leading zeroes for line numbers in display margin." :group 'linum) (defun linum-format-func (line) (let ((w (length (number-to-string (count-lines (point-min) (point-max)))))) (concat (propertize (make-string (- w (length (number-to-string line))) ?0) 'face 'linum-leading-zero) (propertize (number-to-string line) 'face 'linum)))) (setq linum-format 'linum-format-func)))
--Michael Hoffman
If you can’t to just align line numbers to the right as well as have a little padding on the right.
(defun linum-format-func (line) (let ((w (length (number-to-string (count-lines (point-min) (point-max)))))) (propertize (format (format "%%%dd " w) line) 'face 'linum))) (setq linum-format 'linum-format-func)
Putting the following in your .emacs will put one space separation between the linenumber display and the buffer contents:
(setq linum-format "%d ")
If you want a solid line separator, try something like this:
(setq linum-format "%4d \u2502 ")
linum.el
that is in Emacs 24 (a build from Dec 2011), the default value of ‘line-format’
is "%6d "
, and the digits are right-aligned. – DrewAdamsemacs -Q
(Emacs 24), I see no extra space at the left. The the last line number, which has the most digits, is flush with the left side, and all line numbers are right aligned. Here are two images, from the top and bottom of the buffer. What am I missing? – DrewAdams[[image:SomePageName]]#. Then click `Preview' to show the page and that source text will show you a `
?##’ link in the previewed page. Click that link to get to an upload page. – DrewAdamsOr you can set it as a function like the following if you’d like it right justified. The body of the function is modified from linum-0.9x.
(setq linum-format (lambda (line) (propertize (format (let ((w (length (number-to-string (count-lines (point-min) (point-max)))))) (concat “%” (number-to-string w) “d “)) line) ‘face ‘linum)))
You can use ‘linum-before-numbering-hook’
to count and store the number of lines only once per update. Thus you can avoid the somewhat expensive ‘count-lines’
call for each updated line. And a more simplistic constant-width version of the above could look like:
(setq linum-format “%6d “)
Here’s an approach based on the previous suggestion of using ‘linum-before-numbering-hook’
to make the line counting more efficient:
(defvar my-linum-format-string "%4d")
(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)
(defun my-linum-get-format-string () (let* ((width (length (number-to-string (count-lines (point-min) (point-max))))) (format (concat "%" (number-to-string width) "d"))) (setq my-linum-format-string format)))
(setq linum-format 'my-linum-format)
(defun my-linum-format (line-number) (propertize (format my-linum-format-string line-number) 'face 'linum))
Here’s a better approach. Every buffer has its own format variable and the separator use the mode-line face.
(unless window-system (add-hook 'linum-before-numbering-hook (lambda () (setq-local linum-format-fmt (let ((w (length (number-to-string (count-lines (point-min) (point-max)))))) (concat "%" (number-to-string w) "d")))))) (defun linum-format-func (line) (concat (propertize (format linum-format-fmt line) 'face 'linum) (propertize " " 'face 'mode-line))) (unless window-system (setq linum-format 'linum-format-func))
Example screenshot:
I’m no expert but when I add the below to my .spacemacs it accomplishes what you are looking for. None of the above worked for me, cannot remember where I found this, was hacked together from multiple places.
(require 'hl-line) (defface my-linum-hl `((t :inherit linum :background ,(face-background 'hl-line nil t))) "Face for the current line number." :group 'linum) (add-hook 'linum-before-numbering-hook 'my-linum-get-format-string) (defun my-linum-get-format-string () (let* ((width (1+ (length (number-to-string (count-lines (point-min) (point-max)))))) (format (concat "%" (number-to-string width) "d \u2502"))) (setq my-linum-format-string format))) (defvar my-linum-current-line-number 0) (setq linum-format 'my-linum-format) (defun my-linum-format (line-number) (propertize (format my-linum-format-string line-number) 'face (if (eq line-number my-linum-current-line-number) 'my-linum-hl 'linum))) (defadvice linum-update (around my-linum-update) (let ((my-linum-current-line-number (line-number-at-pos))) ad-do-it)) (ad-activate 'linum-update)
NOTE: This is now incorporated into the version distributed with Emacs 24.5 and possibly earlier versions.
Since most editors number the last line of a file even if it is empty (ie. no characters after the last eol char), I kept having to think about how many lines are in the file. Even Emacs lists the line number in the mode line, but no number appears in the margin. The below patch fixes this:
--- linum.el-rev474.svn000.tmp.el Fri May 08 11:30:24 2009 +++ linum.el Fri May 08 11:29:38 2009 @@ -135,8 +135,15 @@ - (let ((line (line-number-at-pos)) - (limit (window-end win t)) - (fmt (cond ((stringp linum-format) linum-format) - ((eq linum-format 'dynamic) - (let ((w (length (number-to-string - (count-lines (point-min) (point-max)))))) - (concat "%" (number-to-string w) "d"))))) - (width 0)) + (let* ((line (line-number-at-pos)) + (limit (window-end win t)) + ;; set empty-line-at-eob flag + (empty-line-at-eob (or (equal ?\n (char-before (point-max))) + (equal (point-min) (point-max)))) + ;; we will automatically number the line at eob if it's not empty + ;; (so we'll say it's already done) + (numbered-line-at-eob (not empty-line-at-eob)) + (fmt (cond ((stringp linum-format) linum-format) + ((eq linum-format 'dynamic) + (let* ((c (count-lines (point-min) (point-max))) + (w (length (number-to-string + (+ c (if empty-line-at-eob 1 0)))))) + (concat "%" (number-to-string w) "d"))))) + (width 0)) @@ -146 +153,2 @@ - (while (and (not (eobp)) (<= (point) limit)) + ;; stop if point>limit, or if eobp and numbered-line-at-eob + (while (and (not (and (eobp) numbered-line-at-eob)) (<= (point) limit)) @@ -165,0 +174,4 @@ + ;; before moving forward, if we're already at eob + (if (eobp) + ;; then we've numbered the empty line + (setq numbered-line-at-eob t))
If this is seen as useful, I’d like for it to be incorporated into the original code. – AdamKerz
I don’t want to use linum on certain major-modes like eshell-mode, wl-summary-mode or compilation-mode. Therefore I tried to hook on these modes, but this doesn’t work for me (maybe a bug?) [1]. So I tried to overwrite the function (linum-on), which works fine. Here is my solution:
(setq linum-disabled-modes-list ‘(eshell-mode wl-summary-mode compilation-mode)) (defun linum-on () (unless (or (minibufferp) (member major-mode linum-disabled-modes-list)) (linum-mode 1)))
I have generalized this to a lisp add-on file Lisp:linum-off.el
Select lines by click-dragging on the margin. Tested with GNU Emacs 23.3
(defvar *linum-mdown-line* nil) (defun line-at-click () (save-excursion (let ((click-y (cdr (cdr (mouse-position)))) (line-move-visual-store line-move-visual)) (setq line-move-visual t) (goto-char (window-start)) (next-line (1- click-y)) (setq line-move-visual line-move-visual-store) ;; If you are using tabbar substitute the next line with ;; (line-number-at-pos)))) (1+ (line-number-at-pos))))) (defun md-select-linum () (interactive) (goto-line (line-at-click)) (set-mark (point)) (setq *linum-mdown-line* (line-number-at-pos))) (defun mu-select-linum () (interactive) (when *linum-mdown-line* (let (mu-line) ;; (goto-line (line-at-click)) (setq mu-line (line-at-click)) (goto-line (max *linum-mdown-line* mu-line)) (set-mark (line-end-position)) (goto-line (min *linum-mdown-line* mu-line)) (setq *linum-mdown* nil)))) (global-set-key (kbd "<left-margin> <down-mouse-1>") 'md-select-linum) (global-set-key (kbd "<left-margin> <mouse-1>") 'mu-select-linum) (global-set-key (kbd "<left-margin> <drag-mouse-1>") 'mu-select-linum)
– sabof
It is possible to change the linum’s face so that the background has a custom color, but how to fix the background of the left-margin under line numbers so that it has the same color (not the default one)? See the snapshot.
As far as i know that space under the numbers is the “default” face and there is no difference between this color and the background of other buffers. So all you can do is to have the same background color for the “default” and “linum” faces. (You can customize a face with “M-x customize-face”) – AlexKost
linum-highligth-current-line-number implements a little function to highlight the current line number with a customizable face (linum group).
When font size is increased in text-scale mode (maybe just invoking ‘text-scale-increase’
), linum mode’s line numbers are not properly displayed. The number’s font increases but the area containing them stays the same size. As a consequence the numbers don’t fit.
I think I can fix that problem with the following code:
(require 'linum) (defun linum-update-window-scale-fix (win) "fix linum for scaled text" (set-window-margins win (ceiling (* (if (boundp 'text-scale-mode-step) (expt text-scale-mode-step text-scale-mode-amount) 1) (if (car (window-margins)) (car (window-margins)) 1) )))) (advice-add #'linum-update-window :after #'linum-update-window-scale-fix)
( EDIT: some small bugs fixed. 2015-10-19 01:47 CEST)
It seems to work, at least with 24.5.
Scaling the frame instead of the buffer using zoom-frm.el will preserve linum’s column.
--sshaw
If you want to display line numbers for every line you are editing, take a look at setnu.el by KyleJones.
I couldn’t find a function to toggle setnu mode on/off, so I wrote one:
(defun toggle-setnu-mode () (interactive) (if setnu-mode (setnu-mode -1) (setnu-mode 1)))
I have this bound to C-n.
‘setnu-mode’
. Try, especially, the version of ‘setnu-mode’
defined in Lisp:setnu+.el. – DrewAdamsemacs -q
, to see if the problem comes from Emacs and the setnu(+) library or from something in your InitFile.(require 'setnu)
instead of (require 'setnu+)
. If so, then the problem is not in Lisp:setnu+.el. When you use ‘require’
, Emacs loads the required library. Library setnu+.el
does its own ‘require’
of setnu.el
, so that’s how setnu
gets loaded if you require setnu+
.setnu+.el
and not setnu.el
, then the workaround is to just use (require 'setnu)
. I suspect the problem is really in setnu.el
, but please find that out.setnu.el
, then please notify KyleJones (the setnu
maintainer). It seems odd that XEmacs would be very fast and GnuEmacs very slow. Perhaps there is a GnuEmacs bug associated with this. It would be worth finding out. – DrewAdamssetnu.el
, probably). Do you have the problem when you start with emacs -q
? If not, then probably some other library you use is interfering. Does the problem occur in every mode? – DrewAdams(custom-set-variables ‘(global-font-lock-mode t nil (font-lock)))
(global-font-lock-mode t)
. But see also EnablingFontLock. – DrewAdamsshell> emacs -q
setnu.el
). See above, where it says that Lisp:setnu+.el fixes problems with adding/removing newlines. – DrewAdamsI tried using setnu+ with Aquamacs and when I add newlines to the current buffer, the frame gets wider. (if I delete the newline, it does not decrease the width framesize)
This is fixed if you customize the variable setnu+-fit-frame-flag to nil
(custom-set-variables ‘(setnu+-fit-frame-flag nil))
this will leave the frame alone, and everything is fine.
– V.Tron
In soft wrap mode the wrapped line goes under the linenumbers which gives the impression that line numbers are part of the buffer. Also, line numbers participate in syntax coloring which I think is basically wrong.
The highlight problem can be rectified by assigning the line number extent to a new customizable face. I sent this very simple patch to the author who will incorporate it in a later release.
According to the author of sentu+, there is no easy way to get the correct wrapped line behaviour fixed. In fact, the proper place for line-numbers would be on the fringe, not the buffer proper. I am truely surprised noone ever needed this basic feature of a code editor.
– V.Tron
Last year I wrote lineno.el, which works on any size files. Rather than making line numbers for the whole buffer, it just displays them for the lines visible in the selected window, updating them as the buffer is edited or the location changed. That way the actual size of the file does not matter. I don’t use it much, but it seems to work well.
russell young
On a larger buffer with line numbers, when I do C-x 2 to split the window and then scroll one of them, the numbers in the other window vanish.
Yes, that is the way it works - it draws in the current window. It would not be too hard to make it work in multiple windows, but it didn’t really seem necessary to how I wanted to use it. (you would have to use buffer-window-count to find how many and what windows it is showed in, and replace the local variables with lists)
If I had time to go in and fix something, the highest priority would be to make it work with ediff. It has essentially the same problem there, the numbers are updated from inside post-command-hook, if the buffer is changed in some way that is not a command the update function is not called.
ry
I have added lineno.el to marmalade repo (http://marmalade-repo.org/packages/lineno/0.1) (GOOD LIVE LINK). The file is in the repo as “.tar”, but just change the ending to “.el” - it is not really a TAR file.
I find that the redraw operation to put in the line numbers makes the text “jitter” uncomfortably to the right as I page up and down the buffer. First the text is drawn by Emacs, then the line number routine redraws it shifted right to put the line numbers it. It would be nice to hook into the first rendering, instead of hooking in after the first rendering is already done. But it does put the line numbers in nicely.
I tried linum, nlinim and lineno on the sqlite-amalgamation file sqlite3.c (5MB, 148784 lines). lineno is the only one to be usable on my machine (3.5 GHz Intel Core 7).
– priyadarshan 2014-08-07 13:56 UTC
nlinum
and try to make-frame
I got a error font-info: Invalid face: linum
, even start Emacs via emacs -q
. Disable nlinum
can workaround. This problem was met in nlinum-1.6.el
. I did not tried other version. – David Young