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

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

Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?"


From: Markus Triska
Subject: Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?"
Date: Sun, 08 Apr 2007 20:48:20 +0200

Sam Peterson <skpeterson@nospam.please.ucdavis.edu> writes:

> The best method would be to write some lisp code as I thought about
> how to do it with a macro and it started getting too complicated.

A rudimentary definition:

   (defun vis-g (arg)
     "ARG `foo/x/y' changes x to y on all lines that contain
   regexp `foo'."
     (interactive "sPattern: ")
     (let* ((ls (split-string arg "/"))
            (regexp (nth 0 ls))
            (from (nth 1 ls))
            (to (nth 2 ls))
            (nlines 0))
       (save-excursion
         (goto-char (point-min))
         (while (re-search-forward regexp nil t)
           (setq nlines (1+ nlines))
           (replace-regexp from to nil
                             (line-beginning-position) (line-end-position))
           (forward-line)))
       (message "Replaced all occurrences on %s matching line%s"
                nlines (if (= nlines 1) "" "s" ))))



reply via email to

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