Accueil / Blog / Métier / 2017 / Productivity tip: use a better grep

Productivity tip: use a better grep

Par Alex Marandon publié 18/01/2017
Improve your life as a developer with a better tool for searching through text files

As developers, we often need to search through text files. Whether they are code, Markdown documents or config files, the grep command is the traditional tool we use to quickly find where a text string occurs:

$ grep -r somestring .

The main problem with this is that it does a lot of unnecessary work because it looks at each and every file it finds, which is usually not needed. Binary files, source code management files (.git, .svn, etc.), code editor backup files (.swp) are usually not files you want to search. Often you also know which type of file you want to search. For instance, if you're trying to fix a web page layout, you're probably more interested in CSS files than in Python code. Searching irrelevant files has two downsides:

  • it slows things down
  • it clutters the result with irrelevant matches

Of course, you can use clever command combinations to limit the scope of search and filter the results (eg. with find or grep -v), but there are tools that have been designed specifically to address the need of developers:

By default, both tools ignore most files that are usually irrelevant, such as binary files, SCM files, etc. They also both allow you to specify the type of files you want to search with a simple command line switch. They're also both recursive by default. For instance, if you wish to search only Python code using ag, you'd do this:

$ ag --py somestring

Both tools should be available through your favorite package manager, so do yourself a favor and upgrade to a better grep!