Manual Dired Search

JTR over at The Art Of Not Asking Why reminds us of a very useful technique for iteratively searching through a series of files for those that contain a given set of terms. The process if simple but very flexible:

  1. Open a Dired buffer with the files you want to search.
  2. Use %g to do a regular expression search through the files for one of the terms. The files containing the term will be marked.
  3. Use t to toggle the marks, resulting in all the files that don’t contain the term being marked.
  4. Use k to temporarily hide the marked files, leaving only those that contain the term.
  5. Go to step 2 using the next term.

When you’ve searched for all the terms, you’re left with a list of files that each contain all the target terms. JTR recommends that you mark the remaining files and do a final regular expression search with the A command to search through the contents of the marked files and show the results in another buffer.

The nice thing about this method is that it’s interactive: you progressively narrow down the list of target files by eliminating files that don’t contain the terms your looking for. You can, as JTR says, even manually eliminate files that you don’t want to consider as you go along. It’s a good technique and worth remembering.

This entry was posted in General and tagged . Bookmark the permalink.