Ack for developers
If you - like me - don’t use an IDE you obviously need a tool for searching text. First utility that comes to mind is grep. Grep is awesome but if you are a developer you can find other tools that can suit you better.
I use ack. Of course there are other tools (e.g ag, or - more recently - ripgrep). A lot of articles on the web focus on speed of searching tools. Authors of such articles often run tests trying to show which utility is the fastest. For me such tests are not very important. Firstly, results of those benchmarks are highly dependent on what we are trying to find and where are we searching – in other words: what is our needle and what is our haystack. Secondly, for me mere speed of a tool is not so important. Difference between searching utilities aren’t that big…
What is important for me is an interface of a utility. And even if speed is very important for you… I think you’ll seldom encounter a situation where search will execute longer than you’ll type actual command. So great cli interface of a search tools can has bigger impact on how fast we can search the codebase than theoretical speed of given search utility.
What so good about ack?
Why ack is great? I’ll show you now. On its website ack is described as “a tool like grep, optimized for programmers”. How does that optimization manifest? Firstly, ack by default ignores directories and files that you probably don’t want to search. So ack doesn’t search through version control directories (e.g
.git) or for example
node_modules folder. Ack also omits backup files and temp files created by your editor. Moreover, ack ignores minified css and js files and source maps for js and css. It’s really annoying when you search codebase and results of your query contains parts of minified files. Those features makes your searches faster and more precise because ack presents only results from files that are interesting for you.
Ack allows you to easily specify types of files that you’re interested in. You are only interested in php files? No problem. Just type
ack --php pattern. You can also specify files that ack should ignore in your search:
ack --nocss pattern. Ack has already defined many file types but you can easily add new filetypes:
Another great thing about ack is how nicely - and easy - you can format search output. By default ack’s output consists of filename of a file and numbered lines where your query was found. Let’s say that you query directory with a lot of files and you suspect that your query exists in many places. So probably you don’t want to clutter output of your search with contents of files - you just want filenames where your search is present. Easy - just add option
-l. What if you are really interested in the context of your search occurence? Using options
-A -B or
-C you can specify how many lines that are around you matching lines should be present in ack’s output.
I’m pretty sure that you can achive all of mentioned features of ack in other ways. The thing is that you probalby would need to pipe some cli utilities and write much more to your console. Ack is fast and easy to use - just try it.