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: Drew Adams
Subject: RE: #1: How to do vi's 'g/foo/s/x/y/' #2: + with a Q-R "yes/no?"
Date: Mon, 9 Apr 2007 00:48:19 -0700

> (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





reply via email to

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