views:

213

answers:

6

What tool or method do you recommend to find and replace values in your code? If code is on Linux/Unix, are find and grep the best method?

I'm currently using Dreamweaver's find and replace and it's sloooow.

+1  A: 

I'm assuming you intend to rename a variable.

It depends on what the body of code is. For C#, Visual Studio's renaming tool is very good. Remember that for it to work reliably, you need the renaming to work hand in hand with the compiler/interpreter, so you can make sure you only make the change in the right scope.

If it is a very simple find/replace, surely the lowly notepad would do an OK job?

biozinc
A: 

find and grep don't build indecies, so they're always going to be slower than alternatives that do. That said, they work fine if your codebase is only a few dozen files.in Fi

Eclipse has a nice file search feature (Ctrl+H). It can also take language semantics into consideration if you have the right plugins (Java works out of the box, of course). The only caveat is that it can be a bit slow the first time you search in a certain file set.

noah
+6  A: 

Take a look at ack, which is designed for searching big codebases.

For replacing, look at Perl's -i, -p and -e flags. You can do stuff like:

$ perl -i -p -e's/\bthisword\b/thatword/g' $(find . -name *.html)

to replace all instances of thisword with thatword in all .html files in the tree.

Andy Lester
You could also use ack for the find part in your replace example: $ perl -i -p -e's/\bthisword\b/thatword/g' $(ack -f --html)
+1  A: 

find and grep will find the word you're looking for, but to replace it you need to use sed, awk or your favorite stream editing filter.

groovy, python, perl will all do search/replace operations - use your favorite or pick one to learn.

For operations on a code base, I'll use find | sed for simple operations (cygwin is your friend) and the IDE's search/replace with regex support for more complex operations. Eclipse, Idea, Visual Studio and even SQL Server Manglement Studio have powerful search/replace functions. The bad news is that not all of them use the same regex syntax.

Ken Gentle
+1  A: 

Take a look into PowerGrep... it has ads everywhere in SO. I'm starting to want it even when i don't really need it-

PD: They have a 15 days free demo, so if this is a small job you could just use the demo.

levhita
+1  A: 

This is just a tip to Andy's above accepted answer.could not add this as a comment due to less reputation

Use ! as a seperator when you have tough time escaping the strings

e.g $ perl -i -p -e's!\bthisword\b!thatword!g' $(find . -name *.html)

Sen