I write/modify code in multiple file formats like Python, MySQL, Perl, HTML, CSS
ID: 658453 • Letter: I
Question
I write/modify code in multiple file formats like Python, MySQL, Perl, HTML, CSS, PHP, JavaScript, AutoHotkey, etc.
I often search my personal library of source code for examples of syntax or complex logic for reuse in new code. Sometimes I search for cryptic strings like =~, because I am searching for a particular regular expression in one of my Perl programs.
Sometimes I search existing code using Copernic, but unfortunately it can only search for words and automatically ignores any programming syntax. It also lacks programming language syntax color coding.
My question is: How do you search through your own libraries of source code? What software is good for this? Copernic is imperfect, but still the best tool I have yet found for this purpose.
Grep and grep-like solutions are nice, but I am most interested in programs with a UI and available in Windows.
Explanation / Answer
I use grep. As I store all my code under the same path on my filesystem I open up my linux shell (but that will work via cygwin on windows as well) and cd to the directory.
Grep is a very sophisticated text search tool that can search for all kinds of text and has no limit to what you want to look for.
Your =~ would be searched like this:
grep -r "=~" .
Whereas the . references the current directory, -r makes the search recursive. So, if you have your programs grouped by language you could cd into your perl directory and search only there.
grep does have downsides, the first would be the speed. It doesn't have an index so each search goes sequentially over all your code until something is hit. Mostly I have a rough idea on where the specific code I look for would be so I only grep over the appropriate directories.
Another downside is complexity. To become good with this approach you will need to spend some time with them.
grep becomes more and more useful the more linux/unix commands you know. You could for example use find to find all the files you want (like all perl files) and then use grep to know if somethings in there. I (as a Java developer) sometimes need to search for a class file inside a jar, but have like gazillions of jars lying around and don't know where it is. So I happen to have a command line that finds jars, lists their contents and outputs only those that have the required file in them. I could use the same technique to also search through the content of the files etc.
So for this specific problem: grep. But in general I recommend the whole toolchain that comes with your unix.