# The Silver Searcher (ag) — Lightning-Fast Code Search from the Terminal > A code-searching tool similar to ack but optimized for speed. ag recursively searches directories for a regex pattern while respecting .gitignore rules, delivering results 5-10x faster than ack and on par with ripgrep. ## Install Save as a script file and run: # The Silver Searcher (ag) — Lightning-Fast Code Search from the Terminal ## Quick Use ```bash # Install brew install the_silver_searcher # macOS apt-get install silversearcher-ag # Debian/Ubuntu # Search for a pattern recursively ag "function handleClick" ./src ``` ## Introduction The Silver Searcher (ag) is a code-searching tool originally inspired by ack but rewritten in C for raw speed. It automatically skips files listed in .gitignore and .hgignore, making it ideal for searching large codebases without wading through build artifacts or vendor directories. ## What The Silver Searcher Does - Recursively searches directories for regex or literal patterns at high speed - Honors .gitignore, .hgignore, and custom ignore files out of the box - Prints results with filenames, line numbers, and color highlighting - Supports PCRE-style regular expressions for complex pattern matching - Searches compressed files and allows file-type filtering with flags ## Architecture Overview ag is written in C and uses POSIX threads to parallelize directory traversal and file searching. It memory-maps files for fast I/O, builds an ignore-rule tree from .gitignore and similar files at startup, and applies those rules during traversal so excluded paths are never opened. Pattern matching is handled by the PCRE library. ## Self-Hosting & Configuration - Ships as a single binary with no runtime dependencies beyond libc and PCRE - Available in all major package managers (brew, apt, yum, pacman, choco) - Configure default flags in a ~/.agrc file (e.g., --hidden, --depth) - Override ignore rules per-project with a .ignore or .agignore file - Integrates with editors via plugins for Vim (ag.vim), Emacs (ag.el), and VS Code ## Key Features - Typically 5-10x faster than ack on large repositories - Automatic .gitignore awareness removes the need for manual exclude lists - Colored, grouped output is easy to scan in a terminal - Pager support pipes long results through less automatically - Broad platform support across Linux, macOS, FreeBSD, and Windows ## Comparison with Similar Tools - **ripgrep (rg)** — newer Rust-based successor, generally faster on modern hardware but ag remains well-established - **ack** — the Perl-based predecessor; ag is a performance-oriented reimplementation - **grep -r** — universal but slower and requires manual ignore configuration - **git grep** — fast inside git repos but limited to tracked files only - **ugrep** — newer C++ grep alternative with ag-compatible output ## FAQ **Q: How does ag compare to ripgrep?** A: ripgrep is generally faster in benchmarks, especially on very large repos. ag is still fast and widely installed, making it a solid choice when rg is unavailable. **Q: Can ag search binary files?** A: By default ag skips binary files. Use the --search-binary flag to include them. **Q: Does ag support multiline search?** A: ag does not natively support multiline patterns. Use ripgrep with the -U flag or pcregrep -M for that. **Q: How do I search only specific file types?** A: Use file-type flags like ag --python "pattern" or provide a glob with -G "*.ts". ## Sources - https://github.com/ggreer/the_silver_searcher - https://geoff.greer.fm/ag/ --- Source: https://tokrepo.com/en/workflows/asset-d77daba2 Author: Script Depot