> *Yes, what I'm looking for is (something close to) a command that iterates over matched files in the grep buffer and calls query-replace for each.*

OK, I just added command `diredp-compilation-files-other-window` to [**Dired+**](http://www.emacswiki.org/emacs/DiredPlus), which can help with this.

You use it in a compilation buffer (any buffer, such as `*grep*`, that is in a mode derived from `compilation-mode').

It prompts you for the name of a new Dired buffer, and it opens Dired on all of the files corresponding to the compilation hits, regardless of what directories they are in.  IOW, the files can be in different directories.

In that Dired buffer, you can then:

1. Use `t` to mark all of the files.
2. Use `Q` to query-replace across all of the files.

(`Q` is standard Dired command `dired-do-query-replace-regexp`).

As an alternative to using `t` to mark all of the files, with **Dired+** you can just use `C-u C-u Q` to act on all files (without bothering to mark them).  With **Dired+**, most commands that operate on the marked files let you use *multiple `C-u` to act on* ***all*** *files*, ignoring marks.

Instead of `Q` you can use `A` to  search the files, `D` to delete them, `P` to print, `Z` to compress, `!` to run a shell command, etc.

If you hit `?` during query-replace you will see this information in buffer `*Help*`:

    Query replacing regexp Sun with mooN.

    Type Space or `y' to replace one match, Delete or `n' to skip to next,
    RET or `q' to exit, Period to replace one match and exit,
    Comma to replace but not move point immediately,
    C-r to enter recursive edit (C-M-c to get out again),
    C-w to delete match and recursive edit,
    C-l to clear the screen, redisplay, and offer same replacement again,
    ! to replace all remaining matches in this buffer with no more questions,
    ^ to move point back to previous match,
    E to edit the replacement string.
    In multi-buffer replacements type `Y' to replace all remaining
    matches in all remaining buffers with no more questions,
    `N' to skip to the next buffer without replacing remaining matches
    in the current buffer.