Eclipse search – a force for good! (and line counting)

There are tons of excellent plugins for profiling code in eclipse. But that’s probably a bit heavyweight if all you’re after is a crude idea of how many lines of code you’ve written. Obviously you could just turn on line numbers and count the bottom line number in each class on your favourite calculate app. But thats going to get tedious for 20, 100, 1000 files, but ah ha! There is an easier way.

The search facility of Eclipse is extremely powerful, and as well as being good for searching for a snippet of text or a specific declaration, its facility for regular expressions opens up a world of possibilities. But how do you count line numbers you say? Well, allow me to tell you…

Ensure the project you wish to count line numbers for is selected in the Package Explorer. Then, go to the search menu and hit the File… menu item. The search dialog should open on the File Search tab.

Eclipse's search functionality makes all-sorts possible

Eclipse's search functionality makes all-sorts possible

On the right, check the box to use regular expressions and enter our search term as the new line character “\n”. We need to make sure we’re only searching source files, so for file name pattern enter “*.java” if it’s Java you’re working with, or whatever file extension your language choice uses. Finally, make sure Enclosing projects is the selected scope at the bottom. If you wanted to count line numbers over multiple projects you could use workspace or a working set. And we’re done. Hit the Search button and allow Eclipse to do its thing.

Bosh! The number of matches returned is your line count. Of course, this includes comments and blank lines. But if you were picky about it you should be able to write a more fancy regular expression to get what you want.

Creative use of the search feature makes all sorts of other repetitive tasks easy. For example, I used the replace facility yesterday to quickly insert a GPL licence message at the top of every java file, just by searching and replacing on package names. A powerful tool to keep in mind!

Tagged with: , ,
Posted in Eclipse

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">