Skip to content

Instantly share code, notes, and snippets.

@GalenkoEugene
Forked from sonulohani/AG tutorial.md
Created February 22, 2023 09:22
Show Gist options
  • Save GalenkoEugene/f213e2ac655dfadf53fed6f8a74f8364 to your computer and use it in GitHub Desktop.
Save GalenkoEugene/f213e2ac655dfadf53fed6f8a74f8364 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