一、软件介绍
ag又名The silver search是与grep类似的搜索软件。是使用c++实现,支持mac、windows和linux。在搜索大量文件时速度明显优于grep。所以推荐平时可以结合grep使用。
二、下载安装
- github源码地址:https://github.com/ggreer/the_silver_searcher
- 安装:
macOS
brew install the_silver_searcher
or
port install the_silver_searcher
Linux
Ubuntu
apt-get install silversearcher-ag
Arch
pacman -S the_silver_searcher
zypper install the_silver_searcher
CentOS
yum install the_silver_searcher
Windows
Win32/64
Unofficial daily builds are available.
winget
winget install "The Silver Searcher"
- windows版本直接下载地址:https://github.com/k-takata/the_silver_searcher-win32/releases
三、vim中集成ag
- 下载ag插件,https://github.com/rking/ag.vim
- 然后放入vim的插件目录下。
- 修改vim配置文件.vimrc 添加如下配置: let g:ag_prg="ag.exe --column"
四、ag使用方法
Usage: ag [OPTIONS] PATTERN [PATH]
Recursively search for PATTERN in PATH.
Like grep or ack, but faster.
Example:
ag -i foo /bar/
Output Options:
--ackmate Print results in AckMate-parseable format
-A --after [LINES] Print lines before match (Default: 2)
-B --before [LINES] Print lines after match (Default: 2)
--[no]break Print newlines between matches in different files
(Enabled by default)
--[no]color Print color codes in results (Enabled by default)
--color-line-number Color codes for line numbers (Default: 1;33)
--color-match Color codes for result match numbers (Default: 30;43)
--color-path Color codes for path names (Default: 1;32)
--column Print column numbers in results
--[no]heading
--line-numbers Print line numbers even for streams
-C --context [LINES] Print lines before and after matches (Default: 2)
--[no]group Same as --[no]break --[no]heading
-g PATTERN Print filenames matching PATTERN
-l --files-with-matches Only print filenames that contain matches
(don't print the matching lines)
-L --files-without-matches
Only print filenames that don't contain matches
--no-numbers Don't print line numbers
--print-long-lines Print matches on very long lines (Default: >2k characters)
--stats Print stats (files scanned, time taken, etc.)
Search Options:
-a --all-types Search all files (doesn't include hidden files
or patterns from ignore files)
-D --debug Ridiculous debugging (probably not useful)
--depth NUM Search up to NUM directories deep (Default: 25)
-f --follow Follow symlinks
-G --file-search-regex PATTERN Limit search to filenames matching PATTERN
--hidden Search hidden files (obeys .*ignore files)
-i --ignore-case Match case insensitively
--ignore PATTERN Ignore files/directories matching PATTERN
(literal file/directory names also allowed)
--ignore-dir NAME Alias for --ignore for compatibility with ack.
-m --max-count NUM Skip the rest of a file after NUM matches (Default: 10,000)
-p --path-to-agignore STRING
Use .agignore file at STRING
-Q --literal Don't parse PATTERN as a regular expression
-s --case-sensitive Match case sensitively (Enabled by default)
-S --smart-case Match case insensitively unless PATTERN contains
uppercase characters
--search-binary Search binary files for matches
-t --all-text Search all text files (doesn't include hidden files)
-u --unrestricted Search all files (ignore .agignore, .gitignore, etc.;
searches binary and hidden files as well)
-U --skip-vcs-ignores Ignore VCS ignore files
(.gitignore, .hgignore, .svnignore; still obey .agignore)
-v --invert-match
-w --word-regexp Only match whole words
-z --search-zip Search contents of compressed (e.g., gzip) files
五、使用场景
1. 在查日志的时候 -C选项特别适用。例:我们需要查找一个玩具登录之前之后发生了什么 登录关键字为login 则可以使用命令:
ag -C3 login log.log
这样可以看到登录前后的3行日志。
2. 查找那些文件包含了关键字cluster
ag -l cluster

3. 批量替换时查找到需要修改的文件:
ag -l cluster | xargs sed -i "s/cluster/cluster_new/g"
六、为什么使用ag而不是grep?
使用ag而不是grep主要有2点原因:
- ag 速度快
- ag支持中文搜索
如果你的任务没有上面2点要求的时候,也可以用grep,但是总体而言还是强烈推荐你安装使用ag,在大部分情况下,它更能胜任你的工作。