0

I am NOT looking for emacs's line-number mode. I don't want to just display line numbers. I want to insert line numbers in a buffer, starting at point and going to mark, or perhaps continuing for some number that I specify via C-u. For example, suppose I have the following lines in my buffer:

So shall I live, supposing thou art true,
Like a deceived husband; so love's face
May still seem love to me, though alter'd new;
Thy looks with me, thy heart in other place:
For there can live no hatred in thine eye,
Therefore in that I cannot know thy change.
In many's looks, the false heart's history
Is writ in moods, and frowns, and wrinkles strange.
But heaven in thy creation did decree
That in thy face sweet love should ever dwell;
Whate'er thy thoughts, or thy heart's workings be,
Thy looks should nothing thence, but sweetness tell.
      How like Eve's apple doth thy beauty grow,
      If thy sweet virtue answer not thy show!

Now i put the cursor on line 3, the line that begins with the word "May." Then, I type C-u7M-xyour-new-elisp-programRET; or I type C-space, scrape down to line 9, the line that begins with "But," and then type M-xyour-new-elisp-programRET. In both cases, I get something like:

So shall I live, supposing thou art true,
Like a deceived husband; so love's face
001 May still seem love to me, though alter'd new;
002 Thy looks with me, thy heart in other place:
003 For there can live no hatred in thine eye,
004 Therefore in that I cannot know thy change.
005 In many's looks, the false heart's history
006 Is writ in moods, and frowns, and wrinkles strange.
007 But heaven in thy creation did decree
That in thy face sweet love should ever dwell;
Whate'er thy thoughts, or thy heart's workings be,
Thy looks should nothing thence, but sweetness tell.
      How like Eve's apple doth thy beauty grow,
      If thy sweet virtue answer not thy show!

I don't know enough elisp to start this, but I vaguely recall seeing an episode of "emacs rocks" a long time ago with an insanely short snippet of code done on-the-fly to do it! I haven't been able to find the episode again.

Every other "Similar Question" suggested seems to pertain to emacs's line-number mode, which apparently doesn't help me.

EDIT: I know that multiple-cursors may have a command to do this, but multiple-cursors seems to be permanently broken in Spacemacs, and I must use Spacemacs so that I can evangelize VIM users.

  • 2
    I know you wanted an Emacs Lisp function, but for this I'd just use keyboard macros. I don't use evil, but in the default keybindings, this would be (with the point right before "May") C-x C-k C-f %03d RET C-1 <f3> <f3> SPC C-a C-n C-7 <f4>. I'm sure it's a comparable number of keystrokes in evil. – Omar Feb 5 at 19:58
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 (format "%03d" counter) nil nil)))))

There are some shorter ways I've found if you don't need the numbers to be formatted with 3 digits. Keyboard macros have a way to insert a number, and replace-regexp has \# to insert the number. I couldn't figure out how to make those insert a 3 digit version of the number with leading zeros though. This loop will do that.

  • 2
    For keyboard macros you can set the format with kmacro-set-format, bound by default to C-x C-k C-f. – Omar Feb 5 at 20:00
  • 3
    Another way: C-M-% ^ RET \,(format "%03d" (1+ \#)) RET – clemera Feb 6 at 0:27
  • 1
    Another way: C-x r N (verified). From emacswiki.org/emacs/NumberLines – Reb.Cabin Mar 26 at 4:19

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.