brew search is more than just a way to find packages; it’s a surprisingly powerful tool for understanding Homebrew’s internal structure and how it categorizes software.
Let’s see it in action. Imagine you’re looking for a command-line JSON processor. You might start with a broad search:
brew search json
This will spit out a lot of results, not just direct matches for "json" but also packages that contain "json" in their description or have it as a dependency. You’ll see things like jq, jsonlint, python@3.9, and even less obvious ones like go-json.
The magic behind brew search isn’t just a simple text match. Homebrew maintains a vast index of formulas (the recipes for building software) and casks (pre-compiled applications). When you run brew search, it queries this index. The output you get is a combination of:
- Formula Names: Packages that have "json" as part of their name (e.g.,
jsonpp). - Formula Descriptions: Packages whose descriptions contain "json" (e.g.,
jq’s description might mention JSON processing). - Cask Names: Applications that have "json" in their name (e.g.,
jsoneditor). - Cask Descriptions: Applications whose descriptions mention "json".
This broad approach is why you see so many results. It’s designed to help you discover related tools even if you don’t know the exact package name.
To get more specific, you can use regular expressions. If you only want formulas whose names start with "json", you can do this:
brew search '^json'
The ^ anchors the search to the beginning of the string. Conversely, to find packages whose names end with "json":
brew search 'json$'
The $ anchors to the end. You can combine these for more complex patterns, like finding packages that contain "json" but are not necessarily just "json":
brew search '.*json.*'
This expression .*json.* means "any characters (zero or more), followed by 'json', followed by any characters (zero or more)". It’s a way to say "contains 'json' anywhere."
Homebrew also has a dedicated search for casks if you’re only interested in GUI applications:
brew search --cask json
This will filter out all the command-line tools and only show you applications. Similarly, for formulas:
brew search --formula json
The mental model Homebrew gives you with brew search is one of interconnectedness. It’s not just a flat list of packages; it’s a graph where packages have names, descriptions, dependencies, and are categorized. brew search is your primary tool for navigating this graph.
What most people don’t realize is how the search results are structured to prioritize certain types of matches. By default, brew search will often show exact formula name matches first, followed by descriptions, then cask matches. This ordering is driven by Homebrew’s internal search algorithm, which tries to present the most likely candidates at the top. This isn’t explicitly documented in a user-facing way, but observing the output over time reveals this implicit hierarchy.
If you want to perform an exact match on a formula name, you can use brew info <formula_name>.
The next concept to explore is how to manage these discovered packages, specifically with brew install and brew uninstall.