File Type Filters
ag has a built-in map of file types to extensions. Use --[type] flags to restrict searches to specific languages.
Listing Available Types
ag --list-file-types
Output (excerpt):
--actionscript .as .mxml
--python .py .pyw
--javascript .js .jsx
--typescript .ts .tsx
--go .go
--rust .rs
--yaml .yaml .yml
--sql .sql
Using Type Flags
# Only Python files
ag --python "def process"
# Only JavaScript and TypeScript
ag --js --ts "export default"
# Only config files (YAML + TOML)
ag --yaml --toml "database_url"
Excluding a Type (--ignore-dir, not a type flag)
ag does not have a --no-[type] flag like rg -T. Instead, exclude directories:
# Skip test directories
ag "api_key" --ignore-dir=tests --ignore-dir=spec
Path Pattern Filter (-G)
-G (--file-search-regex) filters by matching a regex against the full file path. This is more flexible than type flags:
# Only files containing "config" in their path
ag -G "config" "database_host"
# Only files in the "controllers" directory
ag -G "controllers/" "def index"
# Only files ending in _test.py
ag -G "_test\.py$" "assertEqual"
Depth Limiting (--depth)
# Only search 1 level deep (no recursion into subdirs)
ag --depth 1 "pattern"
# Search up to 3 levels deep
ag --depth 3 "pattern"