ag Cheatsheet
Syntax
ag [OPTIONS] PATTERN [PATH]
Pattern is PCRE regex by default. PATH defaults to ..
Core Flags
| Flag | Purpose |
|---|---|
-i | Case insensitive |
-s | Force case sensitive |
-S | Smart case |
-w | Whole word match |
-v | Invert match |
-Q | Fixed/literal string (no regex) |
-f FILE | Read patterns from file |
Scope Control
| Flag | Purpose |
|---|---|
--hidden | Include hidden dotfiles |
-u | Skip ignore files |
-uu | Skip ignore files + hidden |
--ignore-dir=DIR | Exclude a directory |
--ignore=GLOB | Exclude file pattern |
-G REGEX | Only files matching path regex |
--depth N | Limit traversal depth |
File Types (select)
| Flag | Files searched |
|---|---|
--python | .py, .pyw |
--js | .js, .jsx |
--ts | .ts, .tsx |
--go | .go |
--rust | .rs |
--yaml | .yaml, .yml |
--sql | .sql |
Run ag --list-file-types for the full list.
Output Flags
| Flag | Purpose |
|---|---|
-l | Only filenames |
-c | Count per file |
-o | Only matching text |
-n | Line numbers (default) |
--column | Show column number |
--noheading | Flat file:line:col:text format |
-q | Quiet (exit code only) |
-A N | N lines after |
-B N | N lines before |
-C N | N lines each side |
-m N | Stop after N matches |
--color / --nocolor | Force color on/off |
Production Pipelines
# Find and replace across project
ag -l "old_name" src/ | xargs sed -i 's/old_name/new_name/g'
# Top 10 IPs from log
ag -o "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log \
| sort | uniq -c | sort -nr | head -10
# Extract TODO authors
ag -o "TODO\(([^)]+)\)" src/ | sort | uniq -c
# Audit for hardcoded secrets
ag -i "(password|api_key|secret)\s*=" --python --js src/
# Open matches interactively with fzf
ag --noheading "TODO" | fzf | awk -F: '{print $1, $2}' | xargs -r sh -c 'vim +$2 $1'