Skip to content

Instantly share code, notes, and snippets.

@sonulohani
Last active February 22, 2023 09:22
Show Gist options
  • Save sonulohani/6eb7995f961761e56f6001eea6173b4f to your computer and use it in GitHub Desktop.
Save sonulohani/6eb7995f961761e56f6001eea6173b4f to your computer and use it in GitHub Desktop.
AG tool cheatsheet
  • Find files containing "foo", and print the line matches in context: ag foo
  • Find files containing "foo" in a specific directory: ag foo path/to/directory
  • Find files containing "foo", but only list the filenames: ag -l foo
  • Find files containing "FOO" case-insensitively, and print only the match, rather than the whole line: ag -i -o FOO
  • Find "foo" in files with a name matching "bar": ag foo -G bar
  • Find files whose contents match a regular expression: ag '^ba(r|z)$'
  • Find files with a name matching "foo": ag -g foo
  • Find files with a name matching extension ".txt": ag -g .txt
  • Find "non-regex" pattern(literal): ag -Q print( -G bar
  • It is possible to restrict the types of files searched. For example, passing --html will search only files with the extensions htm, html, shtml or xhtml. For a list of supported types, run ag --list-file-types. If you want to search #include only in c++ files, run `ag --cpp -Q '#include'
  • You can also use regex for file name as well: ag "#include" -G '\.cpp$' or ag -g '\.cpp$'
  • Ignoring Files - By default, ag will ignore files whose names match patterns in .gitignore, .hgignore, or .ignore. These files can be anywhere in the directories being searched. Binary files are ignored by default as well. Finally, ag looks in $HOME/.agignore for ignore patterns. If you want to ignore .gitignore and .hgignore, but still take .ignore into account, use -U. Use the -t option to search all text files; -a to search all files; and -u to search all, including hidden files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment