安裝
the silver searcher 在各大平台上都可以從軟件庫直接安裝。除了 Debian/Ubuntu 外,其他系統使用的包名都是一樣的。
MacOS:
brew install the_silver_searcher
Debian/Ubuntu:
sudo apt-get install silversearcher-ag
CentOS:
sudo yum install the_silver_searcher
Fedora:
sudo dnf install the_silver_searcher
ArchLinux:
sudo pacman -S the_silver_searcher
安裝后,系統會新增一個 ag 指令來使用 the silver searcher。作者應該是看穿了人類懶惰的本性,選取了一個比 grep 更短的名字。
常用命令
使用語法:
ag [options] pattern [path ...]
ag [可選項] 匹配模式 [路徑...]
由於模式可以是一個正則表達式,使得搜索功能極為強大。
下面介紹一些常用的場景:
ag -A 3 "string-to-search"
此時 ag 會遍歷當前目錄下的文本文件,在每個文件的每一行中查找 "string-to-search" 這種模式,把文件名、行號和匹配的內容高亮顯示出來,同時顯示了匹配行數的后3行。-C/--context [LINES]
,輸出匹配內容前后[ LINES ]行內容(默認2行)
如果想在某個指定的目錄下搜索,或只搜索某個文件的內容,在搜索的字符串后面加上路徑就行:
ag "string-to-search" /path/to/directory
除此以外,ag -G
提供了強大的過濾功能,使搜索在特定的文件中進行。下面的例子只搜索 java 類型的文件:
ag -G ".+\.java" "string-to-search" /path/to/directory
ag 根據輸入智能判定大小寫的匹配方式。如果查詢的字符串只含有小寫字符,使用大小寫不敏感的匹配方式;如果出現了大寫字符,就改為大小寫敏感的匹配方式。如果想要直接使用不敏感的匹配方式,請用 ag -i
選項。
另一個很有用的選項是 ag -w
的全詞匹配,它要求匹配的字符串前后都需要有合適的分隔符。
如果想要搜索不滿足特定模式的行,用 ag -v
對搜索結果取反。
最后,如果只關心有哪些文件匹配(而不在意文件的內容),可以用 ag -l
顯示有匹配的文件名,類似的 ag -L
顯示沒有任何匹配的文件名。
參考:
幫助文檔
NAME
ag - The Silver Searcher. Like ack, but faster.
SYNOPSIS
ag [options] pattern [path ...]
ag [可選項] 匹配模式 [路徑...]
DESCRIPTION
Recursively search for PATTERN in PATH. Like grep or ack, but faster.
遞歸搜索路徑中的模式。像grep或ack,但更快
OPTIONS
--ackmate
Output results in a format parseable by AckMate https://github.com/protocool/AckMate.
輸出結果以AckMate可解析的格式輸出
--[no]affinity
Set thread affinity (if platform supports it). Default is true.
設置線程關聯(如果平台支持的話)。默認是True
-a --all-types
Search all files. This doesn´t include hidden files, and doesn´t respect any ignore files.
搜索所有文件。這不包括隱藏文件,也不包括任何ignore文件。
-A --after [LINES]
Print lines after match. If not provided, LINES defaults to 2.
打印匹配項之后的行數。如果不提供行數,使用這個參數的話,就默認顯示匹配項之后的2行
-B --before [LINES]
Print lines before match. If not provided, LINES defaults to 2.
打印匹配項之前的行數。如果不提供行數,使用這個參數的話,就默認顯示匹配項之前的2行
--[no]break
Print a newline between matches in different files. Enabled by default.
在不同文件中的匹配項之間打印換行符。 默認情況下啟用。使用--nobreak就不會換行了
-c --count
Only print the number of matches in each file. Note: This is the number of matches, not the number of matching lines. Pipe out‐
put to wc -l if you want the number of matching lines.
僅打印每個文件中的匹配數。 注意:這是匹配數,而不是匹配行數。如果你想要知道匹配的行數,使用管道命令,加上wc -l。★★★
--[no]color
Print color codes in results. Enabled by default.
在結果中打印顏色。默認啟用。
--color-line-number
Color codes for line numbers. Default is 1;33.
行號的顏色,默認是1;33
--color-match
Color codes for result match numbers. Default is 30;43.
結果中匹配數字的顏色。
--color-path
Color codes for path names. Default is 1;32.
結果中路徑的顯示顏色
--column
Print column numbers in results.
結果中列號的顏色
-C --context [LINES]
Print lines before and after matches. Default is 2.
打印匹配項上下文的行數。默認顯示2行。★★★
-D --debug
Output ridiculous amounts of debugging info. Not useful unless you´re actually debugging.
輸出大量的調試信息。 除非你實際調試,否則沒用。
--depth NUM
Search up to NUM directories deep, -1 for unlimited. Default is 25.
最多可搜索NUM個目錄深度,-1表示無限制。 默認值為25。★★★
--[no]filename
Print file names. Enabled by default, except when searching a single file.
打印文件名。 默認情況下啟用,但搜索單個文件時除外
-f --[no]follow
Follow symlinks. Default is false.
包含符號鏈接。默認是False.
-F --fixed-strings
Alias for --literal for compatibility with grep.
別名為--literal,與grep兼容。
--[no]group
The default, --group, lumps multiple matches in the same file together, and presents them under a single occurrence of the
filename. --nogroup refrains from this, and instead places the filename at the start of each match line.
默認--group,將同一個文件中的多個匹配項集中在一起,並在文件名下一行顯示匹配結果。 --nogroup避免這種情況,而是將文件名放在每個匹配行的開頭
-g PATTERN
Print filenames matching PATTERN.
打印與PATTERN匹配的文件名。★★★
-G --file-search-regex PATTERN
Only search files whose names match PATTERN.
僅搜索與PATTERN匹配的文件中的內容,后面還需要加上需要搜索的內容。★★
-H --[no]heading
Print filenames above matching contents.
打印匹配內容以上的文件名。
--hidden
Search hidden files. This option obeys ignored files.
搜索隱藏文件。 此選項服從ignored文件。
--ignore PATTERN
Ignore files/directories whose names match this pattern. Literal file and directory names are also allowed.
--ignore-dir NAME
Alias for --ignore for compatibility with ack.
-i --ignore-case
Match case-insensitively.
-l --files-with-matches
Only print the names of files containing matches, not the matching lines. An empty query will print all files that would be
searched.
-L --files-without-matches
Only print the names of files that don´t contain matches.
--list-file-types
See FILE TYPES below.
-m --max-count NUM
Skip the rest of a file after NUM matches. Default is 0, which never skips.
--[no]mmap
Toggle use of memory-mapped I/O. Defaults to true on platforms where mmap() is faster than read(). (All but macOS.)
--[no]multiline
Match regexes across newlines. Enabled by default.
-n --norecurse
Don´t recurse into directories.
--[no]numbers
Print line numbers. Default is to omit line numbers when searching streams.
-o --only-matching
Print only the matching part of the lines.
--one-device
When recursing directories, don´t scan dirs that reside on other storage devices. This lets you avoid scanning slow network
mounts. This feature is not supported on all platforms.
-p --path-to-ignore STRING
Provide a path to a specific .ignore file.
--pager COMMAND
Use a pager such as less. Use --nopager to override. This option is also ignored if output is piped to another program.
--parallel
Parse the input stream as a search term, not data to search. This is meant to be used with tools such as GNU parallel. For
example: echo "foo\nbar\nbaz" | parallel "ag {} ." will run 3 instances of ag, searching the current directory for "foo",
"bar", and "baz".
--print-long-lines
Print matches on very long lines (> 2k characters by default).
--passthrough --passthru
When searching a stream, print all lines even if they don´t match.
-Q --literal
Do not parse PATTERN as a regular expression. Try to match it literally.
-r --recurse
Recurse into directories when searching. Default is true.
-s --case-sensitive
Match case-sensitively.
-S --smart-case
Match case-sensitively if there are any uppercase letters in PATTERN, case-insensitively otherwise. Enabled by default.
--search-binary
Search binary files for matches.
--silent
Suppress all log messages, including errors.
--stats
Print stats (files scanned, time taken, etc).
--stats-only
Print stats (files scanned, time taken, etc) and nothing else.
-t --all-text
Search all text files. This doesn´t include hidden files.
-u --unrestricted
Search all files. This ignores .ignore, .gitignore, etc. It searches binary and hidden files as well.
-U --skip-vcs-ignores
Ignore VCS ignore files (.gitignore, .hgignore), but still use .ignore.
-v --invert-match
Match every line not containing the specified pattern.
-V --version
Print version info.
--vimgrep
Output results in the same form as Vim´s :vimgrep /pattern/g
Here is a ~/.vimrc configuration example:
set grepprg=ag\ --vimgrep\ $* set grepformat=%f:%l:%c:%m
Then use :grep to grep for something. Then use :copen, :cn, :cp, etc. to navigate through the matches.
-w --word-regexp
Only match whole words.
--workers NUM
Use NUM worker threads. Default is the number of CPU cores, with a max of 8.
-z --search-zip
Search contents of compressed files. Currently, gz and xz are supported. This option requires that ag is built with lzma and
zlib.
-0 --null --print0
Separate the filenames with \0, rather than \n: this allows xargs -0 <command> to correctly process filenames containing spaces
or newlines.
FILE TYPES
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.
ag --py xiang
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.
EXAMPLES
ag printf: Find matches for "printf" in the current directory.
ag foo /bar/: Find matches for "foo" in path /bar/.
ag -- --foo: Find matches for "--foo" in the current directory. (As with most UNIX command line utilities, "--" is used to signify
that the remaining arguments should not be treated as options.)
ABOUT
ag was originally created by Geoff Greer. More information (and the latest release) can be found at http://geoff.greer.fm/ag
SEE ALSO
grep(1)
December 2016 AG(1)