help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Absolutely naïve question about line numbering and lisp code


From: Teemu Likonen
Subject: Re: Absolutely naïve question about line numbering and lisp code
Date: Tue, 03 Mar 2009 16:25:39 GMT
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)

On 2009-03-03 09:16 (-0600), B. T. Raven wrote:

> If you just want to add line numbers permanently you could run this,
> of unknown provenance:
>
> (defun insert-line-numbers ()
>     "Insert a line number on all lines of the current buffer."
>   (interactive)
>   (save-excursion
>     (save-restriction
>       (widen)
>       (let ((fmt (format "%%0%dd "
>                          (1+ (truncate
>                               (log (count-lines (point-min) (point-max))
>                                    10)))))
>             (i 0))
>         (goto-char (point-min))
>         (while (< (point) (point-max))
>           (setq i (1+ i))
>           (insert (format fmt i))
>           (forward-line)))))
>   );;insert-line-numbers

That is quite clever. Usually something like this is enough:

    M-<
    M-x replace-regexp RET ^ RET \,(format "%4s. " (1+ \#)) RET


reply via email to

[Prev in Thread] Current Thread [Next in Thread]