ag - the silver searcher

Ag is an open-source command line search tool similar to grep, just much faster.

This page is a list of some notes and tips for common searches.

When searching for a pattern that is all lowercase, Ag will search case-insensitively.

For example:

1    ag elixir

This will match both “elixir” and “Elixir” in files in the current directory or deeper.

To search case-sensitively, add the -s option:

1    ag elixir -s

This will only match occurrences of “elixir” but not “Elixir”.

When the search term uses mixed case, Ag will automatically search case-sensitively. This is referred to as “smart case”

1    ag Elixir

This will match both “Elixir” but not “elixir”.

Regular Expressions

This section is a collection of some useful regular expressions used in the search pattern. The search pattern can include regex searches, but often need many escape characters, e.g. \\b to match a word-boundary, instead of \b. To avoid excessive escape characters, it’s easier to surround the pattern in quotes by default.

Find lines starting with “init(), but indented by at least one white-space character”:

1    ag "^\s+init\(\)"

Find whole word “row” match:

1    ag "\brow\b"

Find blank lines:

1    ag "^$"

Find locations with 2 or more consecutive blank lines:

1    ag "^\n{2,}"

Same as before, but “blank” lines may include whitespace:

1    ag "^(\s*\n){2,}"

Find lines ending with ',

1    ag "',$"

Find lines starting with “import Route”:

1    ag "^import Route"

Find match “height” if it’s preceded by either “max” or “avail”:

1    ag "(max|avail)height"

Find all 3 or 4 digit numbers:

1    ag "\b\d{3,4}\b"

Find all 24-character words:

1    ag "\b\w{24}\b"

find all date format strings in quotes, e.g. “YYYY-MM-DD”:

1    ag "['\"][ymd-]{4,12}['\"]"

Search within specific file types or file patterns

It is often useful to restrict searches to certain file types to reduce the list of matches. For example adding --js will only search for javascript files with the ‘.js’ file extension. Run ag --list-file-types to see available file types.

To restrict the search to specific files, either list the files after the search pattern or use the -G switch to match the file name. For example, to search only Ruby files where the file name ends with “_controller.rb”, find lines that assign a new Service instance:

1    ag "= \w+Service\.new\b" -G _controller\.rb

Or, within Ruby files, find lines that assign a new class instance:

1    ag "= \w*\.new\b" --ruby

Literal match

If you want to search a pattern that contains special regex characters, but you want to treat those characters as part of a regex, use the Q option, which means “literal match”.

For example, find occurrences of “height()” literally:

1    ag "height()" -Q