Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
30

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. For Emacs version >= 26 you can use display-line-numbers-mode or global-display-line-numbers-mode. See EmacsWiki


17

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


15

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


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


10

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.


7

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


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

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


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

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

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

In order for your changes to persist, you need to add them to your init file. If you've used any customize functionality, it probably generated one for you, and it's saved in ~/.emacs. If that isn't the case, you're better off putting it in ~/.emacs.d/init.el. In order to have line numbers in all buffers and have them persistently, you can put (global-...


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

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


4

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


3

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

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


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


2

Add a 1- to line-number-at-pos and count-lines. (require 'linum) (defun linum-update-window (win) "Update line numbers for the portion visible in window WIN." (goto-char (window-start win)) (let ((line (1- (line-number-at-pos))) (limit (window-end win t)) (fmt (cond ((stringp linum-format) linum-format) ((eq linum-...


2

M-x what-line gives both the relative line number and the narrow buffer number in the mini buffer echo area, for example: M-x what-line line 189 (narrowed line 1)


2

emacs-26 comes with a built-in line number mode, controlled by display-line-numbers which might combine better with other packages.


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