[lug] simple text editing problem

Tkil tkil at scrye.com
Thu Aug 22 12:53:49 MDT 2002


>>>>> "Daniel" == Daniel Webb <webb at danielwebb.us> writes:

Daniel> I need to find and replace text from around 10,000 lines of c
Daniel> code in around 50 files in several directory trees.  I'm going
Daniel> to have to do this quite a few times, so the solution needs to
Daniel> be able to use "find" as the input for which files to load, or
Daniel> else remember from one day to the next which files I'm
Daniel> interested in.  Also, it has to be interactive, since I won't
Daniel> always be able to tell if I need to do the replace without
Daniel> context.

Daniel> Probably Emacs can do it, but I don't know Emacs yet.

As Tom says -- learn it!  Or, learn another editor with similar power
and expressiveness.  (Or an environment with that power -- there are
solutions with just the unix tools + a [simpler] editor that other
people have already mentioned...)

>>>>> "Tom" == Tom Tromey <tromey at redhat.com> writes:

Tom> The way to solve this in Emacs is to use M-x tags-query-replace.
Tom> This function is like query-replace but it sequentially visits
Tom> every file in your current tags file.

Hm.  Recent Emacs (at least the FSF flavor) allow you to do a "find",
then have those results show up in a dired window.  Then you could do
a query-replace on all those files.  At least, I think that is
possible; I should try...

   M-x find-dired RET

then it asks you for a directory to run it within, then the selectors
for 'find'.  (Make sure you don't use "-print"!)

Then it shows a normal dired window; you then tag all the files (with
"t").  After that, you can do the query-replace with "Q".

Another way of doing this is to use "grep-find".  This will show you
every line from every file in the hierarchy that matches a given grep
expression (you can modify the "find" command, too, to look only at
certain patterns or dates, etc).  Then you can use the normal
compilation movement commands (C-x ` for "next-error", etc); this has
the advantage of opening up each file for you, and bringing you to the
line where the string occurred.  Not quite as automatic as above, but
maybe a bit more flexible.  (And remember that the operation
"query-replace from here to end of file" is bound to M-%, which is
easy to get to, and you can access the previous arguments by using
M-p.)

Tom> IMHO it's really worth your time to become an expert with one of
Tom> the major editors.  I've never regretted the time I put into
Tom> learning Emacs.

Fully agreed -- with the addition of two comments:  one, I've been
using emacs for 15+ years, and I'm still learning things; two, having
a sane, easy-to-modify extension language makes life much nicer.

Finally, there's a very healthy user community out there for emacs,
and that helps a lot, too!

t.



More information about the LUG mailing list