44

For Emacs version >= 26 you should use display-line-numbers-mode or global-display-line-numbers-mode. As @eric-brown mentioned you can use M-x linum-mode. Another good idea is to use (add-hook 'prog-mode-hook 'linum-mode) in your init.el to automatically enable linum-mode in all programming modes. See EmacsWiki


18

Try (string-to-number (format-mode-line "%l")) You can extract other information using %-Constructs described in the Emacs Lisp Manual. Caveat: In addition to limitations pointed out by wasamasa and Stefan (see comments below) this does not work for buffers that are not displayed. Try this: (with-temp-buffer (dotimes (i 10000) (insert (format "%...


16

Emacs provides a good amount of debugging facilities including M-x toggle-debug-on-error, M-x toggle-debug-on-quit, debug on signal (which can be used by sending USR2 to Emacs from outside), debug-on-entry (of a function), debug-on-message (when seeing a specific regexp match of a message) and finally, debug itself as alternative to instrumenting a function ...


14

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.


12

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


12

Like most terminals, Emacs' term has a scrollback limit -- which will naturally mess with the consistency of linum's counting, as lines will be deleted from the beginning of the buffer. The term-buffer-maximum-size variable controls this, and sure enough it has a default value of 2048. So linum was telling you the truth: the terminal buffer was never more ...


7

This is a cleaner solution: ;; paste this in your init file (eval-when-compile (require 'cl-lib)) (defun nadvice/linum-update-window (old-fun &rest args) (cl-letf* ((old-count-lines (symbol-function #'count-lines)) ((symbol-function #'count-lines) (lambda (&rest args) (1- (apply old-count-lines args))))) (apply old-...


7

You can customize how to display the number. For example, define the following function that formats numbers as 003:: (defun jp-rel-format (offset) "Another formatting function" (format "%03d: " (abs offset))) Then customize the variable relative-line-numbers-format to use this new function instead of the default realtive-line-numbers-default-format. ...


6

Maybe because it's 2018 now, but in my case, I only had to turn on debugging like wasamasa suggested: M-x toggle-debug-on-error After this, M-x eval-buffer on my faulty Elisp file gave context by providing the position of the error, like this: Debugger entered--Lisp error: (invalid-read-syntax ")") eval-buffer() ; Reading at buffer position 523 ...


5

nlinum.el uses the following: (defvar nlinum--line-number-cache nil) (make-variable-buffer-local 'nlinum--line-number-cache) ;; We could try and avoid flushing the cache at every change, e.g. with: ;; (defun nlinum--before-change (start _end) ;; (if (and nlinum--line-number-cache ;; (< start (car nlinum--line-number-cache))) ;; ...


5

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/...


5

Because the last line hasn't started yet. Adding newline symbol to the end makes the cursor jump from the current line to the next one, but it doesn't add anything to it (new one). Strictly speaking, it is not a line yet. Supposing that every newline symbol was visible, you would get 1. <newline_symbol> 2. <newline_symbol> 3. <...


5

If you're using relative-line-numbers-mode, you can customize the formatting to get what you want: (defun relative-abs-line-numbers-format (offset) "The default formatting function. Return the absolute value of OFFSET, converted to string." (if (= 0 offset) (number-to-string (line-number-at-pos)) (number-to-string (abs offset)))) (setq ...


5

If the user is not getting the line number multiple times each command loop for various positions, then using line-number-at-pos is sufficient -- it will get a line number even if point is not visible: (line-number-at-pos (window-start)) If the user is getting the line number multiple times each command loop for various positions, then it can be much ...


5

To only enable line numbers in "buffers that have code", you could add a hook to prog-mode-hook: (add-hook 'prog-mode-hook #'display-line-numbers-mode) (or whatever line numbering mode/function you're using), and make sure you don't have global-display-line-numbers-mode somewhere in your init. However, "buffers with filenames" and "buffers with code" don't ...


5

display-line-numbers is a buffer-local variable. To enable line numbers for only certain modes, do not use setq-default. Instead, use setq ... something like this: (add-hook 'prog-mode-hook (lambda () (setq display-line-numbers 'relative))) Or, use a preferred major-mode hook such as the perl-mode-hook or html-mode-hook: (add-hook 'name-of-major-...


4

A little late, but here's what I have in my .emacs. It does change the numbering for all linum-mode enabled buffers, but that's what I generally want: (require 'linum) (defvar my-linum-base-line nil) (defvar my-linum-format nil) (add-hook 'linum-before-numbering-hook (lambda () (save-excursion (save-restriction ...


3

org-map-entries is a great function for this sort of thing: (org-map-entries FUNC &optional MATCH SCOPE &rest SKIP) Call FUNC at each headline selected by MATCH in SCOPE. To do what you outlined above from within the target file it's simply a matter of: (org-map-entries (lambda () (line-number-at-pos)) nil 'file)


3

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


3

There are a few ways to do this -- my preferred method is to set the frame defaults for the fringes: (set-face-attribute 'fringe nil :background "red") (add-to-list 'default-frame-alist '(left-fringe . 11)) (add-to-list 'default-frame-alist '(right-fringe . 0)) It is possible to set the windows fringes globally: (setq-default left-fringe-width 11) (setq-...


3

The "visual line number, counting from point-min" depends on the window width, font choice, font size, etc... so a given buffer's position can have several different "visual line numbers" at the same time if it's displayed in different windows. So Emacs does not really try to provide this info. OTOH you can easily get the "visual line number, counting from ...


3

Personally, I doubt the buffer implementation can be blamed for every performance problem people encounter with buffers. My specific problem with it did involve long lines that were slowing down redisplay, a known issue with a multitude of reasons behind it, including Bidi rendering, excessive movement of point crossing the problematic line and less than ...


3

I think those screenshots are actually of vim, in which case all you need to do is :set rnu. For emacs, you're looking for linum-relative. I have various hacks you might be interested, which makes the emulation of rnu closer: (with-eval-after-load 'linum (set-face-background 'linum nil) (require 'linum-relative) ;; truncate current line to four ...


3

GNU bug report logs - #28247; Display native line number on last line of buffer (when empty line): https://debbugs.gnu.org/cgi/bugreport.cgi?bug=28247 Q:  "Emacs does not display a native line number on the last line of the buffer unless there is something there in addition to the cursor. What would be a good approach to modifying the C code to draw a ...


3

This is not an insanely short snippet but it should work. Highlight a region and run this command: (defun number-region (start end) (interactive "r") (save-restriction (narrow-to-region start end) (goto-char (point-min)) (let ((counter 0)) (while (re-search-forward "^" nil t) (setq counter (+ 1 counter)) (replace-match (...


3

Specifically for the built-in line-number support in Emacs 26.1: display-line-numbers-widen is a variable defined in `xdisp.c'. Its value is nil Automatically becomes buffer-local when set. Calls these functions when changed: (#<subr set-buffer-redisplay>) Documentation: Non-nil means display line numbers disregarding any narrowing.


3

A rather naive solution - there should be no leading numbers of a paragraph with a different --textual-- meaning: Edited after request: When starting from an already numbered paragraph, take that value counting on. (defun number-paragraphs (&optional takefirst) "Numbers resp. renumber paragraphs. If starting from already numbered, take that value ...


3

The built-in line numbering mechanism uses the text-area that is sandwiched between the left/right fringes and/or left/right margins, to the extent that fringes/margins exist. [What happens internally is that the line number glyphs (with a space on each end) are prepended to the glyphs of the display line within the text area.] Thus, setting the fringe or ...


3

In a closely related question How to change line-number gutter width using display-line-numbers mode? , I wrote up an answer essentially stating that there is no user option to configure the left/right padding for the built-in line numbers. This is written into the C internals of Emacs in the file xdisp.c. The left padding of the built-in line numbers is ...


Only top voted, non community-wiki answers of a minimum length are eligible