LineNumbers

Last edit

Summary: Add hook for preset width nlinum

Added:

> To precalculate the line number width to avoid horizontal jumps on scrolling:
> <pre>
> ;; Preset width nlinum
> (add-hook 'nlinum-mode-hook
> (lambda ()
> (setq nlinum--width
> (length (number-to-string
> (count-lines (point-min) (point-max)))))))
> </pre>


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.

The Easy Way

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 Current Line Only

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.

XEmacs line-numbers-mode

JerryChen had a line-numbers-mode on Geocities. It seems to be XEmacs specific. Screenshot.

line-num

line-num.el

LinumPlus

LinumPlus

NLinum

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 width nlinum
(add-hook 'nlinum-mode-hook
          (lambda ()
            (setq nlinum--width
              (length (number-to-string
                       (count-lines (point-min) (point-max)))))))

Linum

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

Try 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.
Append (global-linum-mode 1) to your .emacs.

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 cant 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)

-AnshulVerma

Linum: Separating line numbers from text

I, personally, wanted a one-space separation between the line number display and the buffer contents. Couldn’t find a way through the customisation settings. Perhaps it can only be done in Emacs in X, but I don’t use X-based. I’ve found a simple way to do it, however:

On line 157 (in version “0.9wza”) of linum.el, change

(setq width (max width (length str)))

to

(setq width (max width (+ (length str) 1)))

Example screenshot:

LinumGap

ChrisDone

Putting the following in your .emacs yields almost the same result without modification of linum.el:

(setq linum-format “%d “)

If you want a solid line separator, try something like this:

(setq linum-format “%4d \u2502 “)

I like how “%d “ uses only as much space as is needed for what is shown in the buffer. For example, if the largest visible line number is 50, it only uses two characters for the numbers (plus one for the space), even if there are 1000 lines in the file. But I don’t like how it left aligns the numbers. Is there a way to get this behavior with right alignment? dynamic right aligns, but it wastes space. – [[AaronMeurer?]]
I guess I don’t see what you see. Using the version of 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. – DrewAdams
No, I think you are misunderstanding the question. %6d right aligns for me too. But this adds a bunch of unnecessary whitespace to the left of the numbers. %d (no number) uses only as much space as is needed, but it left aligns (this is the printf standard, I think). dynamic right aligns, but it does so by setting N in %Nd to be length of number of lines in the file, which still wastes space if you are at the top. Does this make sense? I can give an example if that would help. – [[AaronMeurer?]]
With emacs -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

linum-align1 linum-align2

There is extra space in the first picture, where the largest visible line number is two digits long. With %d, you get the following (sorry, I don’t know how to upload images to this wiki; feel free to upload them here if you want). See https://gist.github.com/1602365. Notice how in the first and second, only two spaces are used for the numbering (three if you count the padding space on the right; but you should use the mode line plus the cursor to see where column 0 is). In the third, I’ve moved down two lines, and it automatically uses three spaces. All three of these screen shots were taken without editing the file in between (as you can tell from the percentage in the mode line in the second image). This is the behavior I like, but I want it to right align the numbers. – [[AaronMeurer?]]
1. I see what you mean, but don’t have an answer for you. – DrewAdams
2. To upload an image to the wiki, check this page’s source code for examples. You use the source code [[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. – DrewAdams

Or 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 it’s 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:

LinumSeparatorNewApproach

Linum: Numbering the last line of the buffer

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

Linum: Disable linum for certain major-modes

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)))

– FlorianAdamsky?

I have generalized this to a lisp add-on file Lisp:linum-off.el

MatthewFidler

Linum: Select lines my clicking

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

Linum: background color under linum in the left-margin area?

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.

leftmargin

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

Setnu

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.

How hard did you look? ;-) Try command ‘setnu-mode’. Try, especially, the version of ‘setnu-mode’ defined in Lisp:setnu+.el. – DrewAdams

Problems with setnu

You’ll have to provide more information, if you expect help.
    1. Try setnu without setnu+, to see which library is the problem. Setnu+ is an extension of setnu.
    2. Try with emacs -q, to see if the problem comes from Emacs and the setnu(+) library or from something in your InitFile.
    3. Report an exact recipe to either KyleJones (for setnu) or DrewAdams (for setnu+), being sure to specify your Emacs version and the library version/date. You can post the recipe here also – someone else might have a solution.
DrewAdams
Well, first you should know, I am not an expert emacs user (yet), I really think this mode is important especially when debugging script, your eye can quickly go to the line number where the error was reported, and for this, this problem doesn’t appear. This problem only appear in large files or very large files.
To find files on my computer to test this I first issues this command ~$ find ~/ -iname ‘*txt’ -size +500k
I found the text version of a book I downloaded long ago (and never had the time to read) called Underground by Suelette Dreyfus. You can download the same file from this link http://www.xs4all.nl/~suelette/underground/underground.txt (it’s legally available for free download)
As for my Emacs configuration, I will try to describe it to my best of knowledge: My computer have a pentium 3 1 giga hertz processor and 256 MB Ram. It runs debian sarge and linux 2.6 kernel. Emacs version is “GNU Emacs 21.4.1 (i386-pc-linux-gnu, X toolkit, Xaw3d scroll bars) of 2005-03-17 on trouble, modified by Debian”
I downloaded both setnu and setnu+ and put them in a folder on my loadpath. And added (require setnu+) to my InitFile. I am not sure what an extension really means, and I dont fully understand what require really do!
Anyway, to the interesting part: I opened the underground.txt file. And type M-x setnu-mode. It took emacs almost 4 and a half minutes to show the line numbers. During this time emacs was not responsive at all. I typed M-x setnu-mode again. It took emacs 2 minutes for to remove the numbers (actually it didnt remove them right away, I have to click inside emacs once for it, I clicked inside emacs when I noticed that the processor monitor showed normal activity, during the 4.5 minutes and the other 2 minutes it took emacs to process setnu-mode the processor was at 100%)
I also tried the same file in Nedit and gvim (to test how long their implementation of showing line numbers take, to see if maybe 4 minutes is normal) in both of them the line number appearance was insantaneous.
I tried the same file in Xemacs, I think Xemacs use the same startup file as GNU Emacs so I am guessing it is using the same setnu+ script. It took it 15 seconds to show the numbers, but when i typed M-x setnu-mode again the remove the numbers the effect was insantaneous
The Xemacs version was “XEmacs 21.4 (patch 17) “Jumbo Shrimp” [Lucid] (i386-debian-linux, Mule) of Tue Feb 8 2005 on penell”
I hope this helps. – SysTems
Thanks. I don’t have an answer for you yet, but it would be good for you to at least determine if you have the same problem when you do (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+.
So, at least try that, please. If it turns out that the problem is 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.
If the problem is in 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. – DrewAdams
I retried the test as you suggested with (require setnu) instead of (require setnu+), on both GnuEmacs and XEmacs. And I got the same results 4 minutes for GNU Emacs and almost 10 seconds for XEmacs. (10 seconds is still very poor performance compared to vim or nedit)
And I also visited KyleJones personal site and I quote from his setnu description this line /“This package is best used on files with a few thousand lines at most, unless you have very fast hardware and are willing to waste it recalculating and redisplaying line numbers.”/. So obviously he is aware of the bad performance! – SysTems
OK, so I guess you got the answer. Sorry about the bad news. You might still send an email to KyleJones, to at least let him know of the incredible performance difference between XEmacs and GnuEmacs. 10 seconds might be tolerable in some contexts (or less, with a smaller file), but 4 minutes is useless. You never know – perhaps there is a tiny bug that causes this performance decline in GnuEmacs relative to XEmacs. – DrewAdams
  • (XEmacs) Can anybody confirm my problems described here?
  • (Emacs) Does anybody use setnu successfully with Gnu Emacs? For me it just f’ups whenever I do some kill’n’yanking. The line numbers don’t show up at all or the appear at the end of a line and from then are undeletable and remain cluttering the buffer display until I reopen the file. – StefanKamphausen
I have no such problem. The version of setnu I have is from 1997. Unless someone else here has a suggestion, try emailing help-gnu-emacs@gnu.org or the setnu maintainer (listed in the setnu.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
OK, using the stupid approach (emacs -q, manually loading line after line from my init files, each time performing a test for the problem) I found that the following in my custom.el introduces the problems:

(custom-set-variables ‘(global-font-lock-mode t nil (font-lock)))

as soon as I load a file containing the above code setnu stops working in Gnu Emacs. Any pointers? How do you turn on font lock? – StefanKamphausen
I turn it on this way: (global-font-lock-mode t). But see also EnablingFontLock. – DrewAdams
OK, even simpler…

shell> emacs -q

in scratch type sth with at least two lines
(let ((x 0)) (print x)) ;; go to the beginning ;; M-x mark-sexp, C-w, C-y … everything is fine. This is our test ;; Now.. M-x load-library setnu M-x setnu-mode ;; perform the test… OK ;; type (global-font-lock-mode 1) ;; C-x C-e ;; perform the test…. OOOps (at least for me).
Result is shown here
Yes, the same thing happens to me. However, I don’t have that problem when I use Lisp:setnu+.el (which uses setnu.el). See above, where it says that Lisp:setnu+.el fixes problems with adding/removing newlines. – DrewAdams
I will give it a try… – StefanKamphausen

Horizontal frame resize problem with setnu+

I 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

Problems with Soft wrap and syntax coloring

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

LineNo: Alternative line number function

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


CategoryDisplay