ReplaceCount

This page concerns auto-incrementing numbers in otherwise-repeating lines, based on the thread here, from 1996.

Since Emacs22, such tasks can be accomplished as described by Drew Adams below. See ReplaceRegexpWithLispExpressions for further examples.

In Emacs 22, you can just use `\#’ in the replacement string of ‘query-replace-regexp’, to get the number of the current replacement. Use that to increment instead of using (format "%03d" replace-count) with ‘query-replace-regexp-eval’. You can also use `\#’ in a replacement string in Icicles – see Icicles - Search-And-Replace. – DrewAdams


Here is another nice function, from Groups:increase+number+under+cursor

Original author: ThienThiNguyen, 28-Jan-1996, modified for multiple lines: EricLudlam.

  (defun another-line (num-lines)
    "Copies line, preserving cursor column, and increments any numbers found.
  Copies a block of optional NUM-LINES lines.  If no optional argument is given,
  then only one line is copied."
    (interactive "p")
    (if (not num-lines) (setq num-lines 0) (setq num-lines (1- num-lines)))
    (let* ((col (current-column))
           (bol (save-excursion (forward-line (- num-lines)) (beginning-of-line) (point)))
           (eol (progn (end-of-line) (point)))
           (line (buffer-substring bol eol)))
      (goto-char bol)
      (while (re-search-forward "[0-9]+" eol 1)
        (let ((num (string-to-int (buffer-substring
                                    (match-beginning 0) (match-end 0)))))
          (replace-match (int-to-string (1+ num))))
        (setq eol (save-excursion (goto-char eol) (end-of-line) (point))))
      (goto-char bol)
      (insert line "\n")
      (move-to-column col)))
  (define-key global-map (kbd "M-o") 'another-line)

Basically the same function as above, but with the following extra functionality: it increments every number by the amount of numbers in the line. This is quite useful for some cases, for example when editing latex-documents.

  (defun another-line (num-lines)
  "Copies line, preserving cursor column, and increments any numbers found.
  Copies a block of optional NUM-LINES lines.  If no optional argument is given,
  then only one line is copied."
  (interactive "p")
  (if (not num-lines) (setq num-lines 0) (setq num-lines (1- num-lines)))
  (let* ((col (current-column))
	 (bol (save-excursion (forward-line (- num-lines)) (beginning-of-line) (point)))
	 (eol (progn (end-of-line) (point)))
	 (line (buffer-substring bol eol)))
    (goto-char bol)
    (let ((numam 0))
      (while (re-search-forward "[0-9]+" eol 1)
	(setq numam (+ numam 1)))
      (goto-char bol)
      (while (re-search-forward "[0-9]+" eol 1)
	(let ((num (string-to-int (buffer-substring
				   (match-beginning 0) (match-end 0)))))
	(replace-match ((insert )nt-to-string (+ num numam))))
	(setq eol (save-excursion (goto-char eol) (end-of-line) (point)))))
    (goto-char bol)
    (insert line "\n")
    (move-to-column col)))
  (define-key global-map (kbd "M-o") 'another-line)

94.218.213.34


CategoryEditing CategorySearchAndReplace