Here’s a Dired thing that most Emacs veterans know well, but only clicked for me yesterday: searching for specific words and terms in different files while narrowing the results down until you find exactly what you need.

There are many ways to do this, some in Emacs and some outside, but I find that the manual-visual process in the middle makes this especially powerful, as you get to see the files selected each time and decide what to keep as you keep narrowing down your search.

Let’s say you want to find journal entries where you wrote about a vacation that involved going out of state, and maybe get an idea of where you stayed, say a hotel vs. an Airbnb.

  1. In Dired, go to the folder where the words are (say your journal notes directory) and enter % g for a regex search for term 1 — in our case, “vacation”.
  2. Dired will mark the files containing “vacation” somewhere in the text.
  3. You now start the manual narrowing process:
    1. Enter t to mark all the opposite files, the ones that were not included in the results before. What you have now are all the files that do not have the word “vacation” in them.
    2. Enter k to remove the marked lines from view (don’t worry, this just hides the files. They are still there, just hit g). You now have the files you don’t need — those that do not have the word “vacation” in them — out of the way.
  4. You can now go back to step 1, running another search on those narrowed results. In our example, enter % g again for term 2 — “airport” (maybe you drive, but you get the idea) — and narrow again. Because we are now only working with the files that remained after our first round of narrowing, these files (by deductive logic of what we did) now contain the word “vacation” already. The files we are now narrowing (steps 3.1 and 3.2) will contain the word “vacation” and the word “airport”. When you’re done narrowing a second time and have the files that contain “vacation” and “airport”:
  5. Mark all the files you have left (or the one you decide you want from what’s left, looking at the list of files), and enter A1. This works on the file at point unless you have a couple marked, so that’s why we need to mark those first. This runs a fresh regex search over the full contents of the marked files, and will show the results in a dedicated buffer, so you see all the results from all the files in a list, showing you which file they are from and on what lines you can find them. Since this is a regex search, you can get fancy here. For me, I found that at this point the selection is small enough to find what I need: I can run one search for “hotel”, another for “Airbnb”, no problem.

As I said above, there are many ways to run something automatic and not go through these steps, but the fact that you see the files in front of you is powerful.

For example, in step 5, when you look at what you have left, you might find an old packing list template that somehow snuck into your journal folder (this never happens to me though! I’m way too organized… uhm…), or maybe you have an idea of what time of year you went on a particular vacation you’re looking for (say if it was a vacation during the summer), so you can manually take out files that contain the above words but have a timestamp of December and February.


  1. A runs the command dired-do-find-regexp, for searching, so it’s worth noting that in Dired it has a sibling: Q for dired-do-find-regexp-and-replace, which replaces the searched term with your choice. Great for fixing a mistake you may have in a couple of files, like a misspelled name you keep getting wrong. ↩︎