* #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" @ 2007-04-07 17:51 David Combs 2007-04-08 10:28 ` Sam Peterson ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: David Combs @ 2007-04-07 17:51 UTC (permalink / raw) To: help-gnu-emacs (1) How to do, in gnu-emacs, vi's 'g/foo/s/x/y/': That is, "on all lines that contain regexp "foo", change x to y. And maybe also with option: only the first one on that line, or all of them? (2) Same as above, BUT WITH A QUERY-REPLACE-LIKE "YES|NO|!|etc" prompt. (No, I believe that vi-emulator .el-package does NOT implement vi's "g/pat/<vi-command>/" (Sure would be nice if it did!) Thanks so very much! David ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" 2007-04-07 17:51 #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" David Combs @ 2007-04-08 10:28 ` Sam Peterson 2007-04-08 18:48 ` Markus Triska 2007-04-08 19:07 ` Peter Dyballa 2007-04-09 7:48 ` Drew Adams 2 siblings, 1 reply; 6+ messages in thread From: Sam Peterson @ 2007-04-08 10:28 UTC (permalink / raw) To: help-gnu-emacs dkcombs@panix.com (David Combs) on 7 Apr 2007 13:51:52 -0400 didst step forth and proclaim thus: > (1) How to do, in gnu-emacs, vi's 'g/foo/s/x/y/': > > That is, "on all lines that contain regexp "foo", change x to y. > > And maybe also with option: only the first one on that line, or all of them? Good question. There's no quick equivalent to my knowledge. Though there are probably a few ways to implement it. 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. M-x occur <RET> is definitely worth looking into. > (2) Same as above, BUT WITH A QUERY-REPLACE-LIKE "YES|NO|!|etc" prompt. > > (No, I believe that vi-emulator .el-package does NOT implement > vi's "g/pat/<vi-command>/" > > (Sure would be nice if it did!) What are you talking about? There's three different vi emulators in Emacs. I tried the above command in both vip and viper and it worked perfectly. -- Sam Peterson skpeterson At nospam ucdavis.edu "if programmers were paid to remove code instead of adding it, software would be much better" -- unknown ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" 2007-04-08 10:28 ` Sam Peterson @ 2007-04-08 18:48 ` Markus Triska 0 siblings, 0 replies; 6+ messages in thread From: Markus Triska @ 2007-04-08 18:48 UTC (permalink / raw) To: help-gnu-emacs 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" )))) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" 2007-04-07 17:51 #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" David Combs 2007-04-08 10:28 ` Sam Peterson @ 2007-04-08 19:07 ` Peter Dyballa 2007-04-09 7:48 ` Drew Adams 2 siblings, 0 replies; 6+ messages in thread From: Peter Dyballa @ 2007-04-08 19:07 UTC (permalink / raw) To: David Combs; +Cc: help-gnu-emacs Am 07.04.2007 um 19:51 schrieb David Combs: > (1) How to do, in gnu-emacs, vi's 'g/foo/s/x/y/': M-x replace-regexp RET ^\(.*foo.*\)x\(.*\)$ RET \1y\2 RET and M-x replace-regexp RET ^\(.*\)x\(.*foo.*\)$ RET \1y\2 RET ? I think it's necessary to distinguish the two cases where the MUST HAVE is left or right of the MUST NOT BE. Instead of replace-regexp you can also use query-replace-regexp. It might be possible to unite these into one expression using alternatives. -- Greetings Pete Claiming that the Macintosh is inferior to Windows because most people use Windows, is like saying that all other restaurants serve food that is inferior to McDonald's. ^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" 2007-04-07 17:51 #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" David Combs 2007-04-08 10:28 ` Sam Peterson 2007-04-08 19:07 ` Peter Dyballa @ 2007-04-09 7:48 ` Drew Adams 2 siblings, 0 replies; 6+ messages in thread From: Drew Adams @ 2007-04-09 7:48 UTC (permalink / raw) To: David Combs, help-gnu-emacs > (1) How to do, in gnu-emacs, vi's 'g/foo/s/x/y/': > > That is, "on all lines that contain regexp "foo", change x to y. > > And maybe also with option: only the first one on that line, or > all of them? > > (2) Same as above, BUT WITH A QUERY-REPLACE-LIKE "YES|NO|!|etc" prompt. You can do these things with Icicles. `icicle-search' (`C-`') and other Icicles search commands (`icicle-occur', `icicle-imenu', etc.) let you combine replacement with searching. Whenever you are at a search hit, you can replace it (or all matches) by hitting a key. Searching (and replacement) is contextual: 1. You provide a context regexp (e.g. `.*foo.*' in your example - lines with `foo'). 2. Icicles presents all matches for that regexp (in the buffer, the region, multiple buffers, or multiple saved regions) - as completion candidates. 3. You can navigate among the search hits, in order or with random (direct) access, using `C-mouse-2', `C-RET', `C-next', or `C-prior'. The first two are for direct access, the last two are for access in order. 4. You can replace any of the search hits with other text (e.g. `y' in your example) selectively, using `S-C-mouse-2', `S-C-RET', `S-C-next', or `S-C-prior'. The first time you use one of these, you are prompted for the replacement string. 5. At any time, you can type input (e.g. `x' in your example), which can be a regexp. Then, whatever that current input matches is replaced, instead of the entire search hit (`foo') - but only in the context of the matching hits (lines with `foo'). That is, `x' is replaced by `y', but only in lines that contain `foo'. Each occurrence of `x' is treated individually. 6. You can choose instead to replace entire search hits (e.g. the whole line with `foo'), instead of just the parts (`x') that match your current input. You can toggle this at any time, using `C-,'. Even if you replace entire search hits, you can still type input (e.g. `x') to filter the set of initial matches (e.g. lines with `foo'). 7. You can change the in-context matches by changing your minibuffer input at any time: Erase `x' and type `j.*m', and matches of `j.*m' will be found (and available for replacement) in lines that contain `foo'. 8. You can replace all matches of your current input (`x'), by using `S-C-insert'. Doc: http://www.emacswiki.org/cgi-bin/wiki/Icicles_-_Search_Enhancements#SearchAn dReplace Code: http://www.emacswiki.org/cgi-bin/wiki/Icicles_-_Libraries HTH ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <mailman.1852.1176105200.7795.help-gnu-emacs@gnu.org>]
* Re: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" [not found] <mailman.1852.1176105200.7795.help-gnu-emacs@gnu.org> @ 2007-04-23 3:32 ` David Combs 0 siblings, 0 replies; 6+ messages in thread From: David Combs @ 2007-04-23 3:32 UTC (permalink / raw) To: help-gnu-emacs Thanks for all the suggestions! I'll have to look at some of the *other* vi-simulators -- and also at "icicles". David ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2007-04-23 3:32 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2007-04-07 17:51 #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?" David Combs 2007-04-08 10:28 ` Sam Peterson 2007-04-08 18:48 ` Markus Triska 2007-04-08 19:07 ` Peter Dyballa 2007-04-09 7:48 ` Drew Adams [not found] <mailman.1852.1176105200.7795.help-gnu-emacs@gnu.org> 2007-04-23 3:32 ` David Combs
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).