Skip to main content

Basic Search

ag [OPTIONS] PATTERN [PATH]

If PATH is omitted, ag searches the current directory recursively.

Your First Searches

# Search the current project for "TODO"
ag "TODO"

# Search a specific directory
ag "nginx" /etc/

# Search a single file
ag "return" src/app.py

ag outputs results grouped by file, with line numbers and highlighted matches:

src/app.py
42:1: return HttpResponse(data)
87:1: return render(request, "index.html")

src/utils.py
15:1: return cache.get(key)

The format is line:column:content.

Case Sensitivity

ag "error" # strict case: matches "error" only
ag -i "error" # case-insensitive: matches error, Error, ERROR
ag -s "Error" # force case-sensitive (useful when -i is in your config)

Smart Case (-S)

Like fd and rg, ag supports smart case:

ag -S "error" # lowercase → case-insensitive
ag -S "Error" # has uppercase → case-sensitive

Whole Word Match (-w)

Prevents partial matches:

ag -w "is" # matches " is " but NOT "redis" or "history"

Exit Codes for Scripts

ag -q "FAIL" report.log && echo "failures found" || echo "all clear"
# -q: quiet mode — suppress output, only set exit code
Exit codeMeaning
0At least one match
1No matches
2Error