安装

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 显示没有任何匹配的文件名。

参考:

  • the silver searcher:快如闪电的 grep

帮助文档

NAMEag - The Silver Searcher. Like ack, but faster.SYNOPSISag [options] pattern [path ...]ag [可选项] 匹配模式 [路径...]
DESCRIPTIONRecursively search for PATTERN in PATH. Like grep or ack, but faster.递归搜索路径中的模式。像grep或ack,但更快
OPTIONS--ackmateOutput results in a format parseable by AckMate https://github.com/protocool/AckMate.输出结果以AckMate可解析的格式输出--[no]affinitySet thread affinity (if platform supports it). Default is true.设置线程关联(如果平台支持的话)。默认是True  -a --all-typesSearch 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]breakPrint a newline between matches in different files. Enabled by default.在不同文件中的匹配项之间打印换行符。 默认情况下启用。使用--nobreak就不会换行了-c --countOnly 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]colorPrint color codes in results. Enabled by default.在结果中打印颜色。默认启用。--color-line-numberColor codes for line numbers. Default is 1;33.行号的颜色,默认是1;33--color-matchColor codes for result match numbers. Default is 30;43.结果中匹配数字的颜色。--color-pathColor codes for path names. Default is 1;32.结果中路径的显示颜色--columnPrint column numbers in results.结果中列号的颜色-C --context [LINES]Print lines before and after matches. Default is 2.打印匹配项上下文的行数。默认显示2行。★★★-D --debugOutput ridiculous amounts of debugging info. Not useful unless you´re actually debugging.输出大量的调试信息。 除非你实际调试,否则没用。--depth NUMSearch up to NUM directories deep, -1 for unlimited. Default is 25.最多可搜索NUM个目录深度,-1表示无限制。 默认值为25。★★★--[no]filenamePrint file names. Enabled by default, except when searching a single file.打印文件名。 默认情况下启用,但搜索单个文件时除外-f --[no]followFollow symlinks. Default is false.包含符号链接。默认是False.-F --fixed-stringsAlias for --literal for compatibility with grep.别名为--literal,与grep兼容。--[no]groupThe default, --group, lumps multiple matches in the same file together, and presents them under  a  single  occurrence  of  thefilename. --nogroup refrains from this, and instead places the filename at the start of each match line.默认--group,将同一个文件中的多个匹配项集中在一起,并在文件名下一行显示匹配结果。 --nogroup避免这种情况,而是将文件名放在每个匹配行的开头-g PATTERNPrint filenames matching PATTERN.打印与PATTERN匹配的文件名。★★★-G --file-search-regex PATTERNOnly search files whose names match PATTERN.仅搜索与PATTERN匹配的文件中的内容,后面还需要加上需要搜索的内容。★★-H --[no]headingPrint filenames above matching contents.打印匹配内容以上的文件名。--hiddenSearch hidden files. This option obeys ignored files.搜索隐藏文件。 此选项服从ignored文件。--ignore PATTERNIgnore files/directories whose names match this pattern. Literal file and directory names are also allowed.--ignore-dir NAMEAlias for --ignore for compatibility with ack.-i --ignore-caseMatch case-insensitively.-l --files-with-matchesOnly  print  the  names  of files containing matches, not the matching lines. An empty query will print all files that would besearched.-L --files-without-matchesOnly print the names of files that don´t contain matches.--list-file-typesSee FILE TYPES below.-m --max-count NUMSkip the rest of a file after NUM matches. Default is 0, which never skips.--[no]mmapToggle use of memory-mapped I/O. Defaults to true on platforms where mmap() is faster than read(). (All but macOS.)--[no]multilineMatch regexes across newlines. Enabled by default.-n --norecurseDon´t recurse into directories.--[no]numbersPrint line numbers. Default is to omit line numbers when searching streams.-o --only-matchingPrint only the matching part of the lines.--one-deviceWhen recursing directories, don´t scan dirs that reside on other storage devices. This lets you  avoid  scanning  slow  networkmounts. This feature is not supported on all platforms.-p --path-to-ignore STRINGProvide a path to a specific .ignore file.--pager COMMANDUse a pager such as less. Use --nopager to override. This option is also ignored if output is piped to another program.--parallelParse  the  input  stream  as  a search term, not data to search. This is meant to be used with tools such as GNU parallel. Forexample: echo "foo\nbar\nbaz" | parallel "ag {} ." will run 3 instances of ag,  searching  the  current  directory  for  "foo","bar", and "baz".--print-long-linesPrint matches on very long lines (> 2k characters by default).--passthrough --passthruWhen searching a stream, print all lines even if they don´t match.-Q --literalDo not parse PATTERN as a regular expression. Try to match it literally.-r --recurseRecurse into directories when searching. Default is true.-s --case-sensitiveMatch case-sensitively.-S --smart-caseMatch case-sensitively if there are any uppercase letters in PATTERN, case-insensitively otherwise. Enabled by default.--search-binarySearch binary files for matches.--silentSuppress all log messages, including errors.--statsPrint stats (files scanned, time taken, etc).--stats-onlyPrint stats (files scanned, time taken, etc) and nothing else.-t --all-textSearch all text files. This doesn´t include hidden files.-u --unrestrictedSearch all files. This ignores .ignore, .gitignore, etc. It searches binary and hidden files as well.-U --skip-vcs-ignoresIgnore VCS ignore files (.gitignore, .hgignore), but still use .ignore.-v --invert-matchMatch every line not containing the specified pattern.-V --versionPrint version info.--vimgrepOutput results in the same form as Vim´s :vimgrep /pattern/gHere is a ~/.vimrc configuration example:set grepprg=ag\ --vimgrep\ $* set grepformat=%f:%l:%c:%mThen use :grep to grep for something. Then use :copen, :cn, :cp, etc. to navigate through the matches.-w --word-regexpOnly match whole words.--workers NUMUse NUM worker threads. Default is the number of CPU cores, with a max of 8.-z --search-zipSearch  contents  of  compressed files. Currently, gz and xz are supported. This option requires that ag is built with lzma andzlib.-0 --null --print0Separate the filenames with \0, rather than \n: this allows xargs -0 <command> to correctly process filenames containing spacesor newlines.FILE TYPESIt  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 FILESBy default, ag will ignore files whose names match patterns in .gitignore, .hgignore, or .ignore. These files can be anywhere  in  thedirectories 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.EXAMPLESag 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 signifythat the remaining arguments should not be treated as options.)ABOUTag was originally created by Geoff Greer. More information (and the latest release) can be found at http://geoff.greer.fm/agSEE ALSOgrep(1)December 2016                                                           AG(1)

转载于:https://www.cnblogs.com/michael-xiang/p/10466890.html

ag 命令的帮助文档相关推荐

  1. windows命令行帮助文档如何使用

    windows命令行帮助文档如何使用( 文章目录: 一.查看windows的常用命令提示符 二.如何查看每个命令的使用手册 1.more命令:查看文档内容 2.find命令:查找命令 只有学会查看帮助 ...

  2. linux查看帮助文档的命令,Linux如何使用查看一个命令的帮助文档呢?

    摘要: 下文讲述Linux操作系统中查看命令的帮助文档的方法分享,如下所示: 实现思路: 使用 --help命令即可查看命令自带的帮助文档信息 例: 查看ls命令的帮助信息 [root@localho ...

  3. Linux入门学习(九)—— 怎么查看命令的帮助文档、怎么查看系统配置文件的帮助文档?

    引言:上期回顾(想要学习的童鞋可以点击看看)     上一章讲诉了怎么快速查找文件,以及怎么找到一个命令所在位置,那么这一章就来讲怎么查看命令的帮助文档.在Linux命令中,我们是不会每个命令都会讲到 ...

  4. 环境变量PATH 、mv 命令 、cp 命令和 查看文档相关的命令

    2019独角兽企业重金招聘Python工程师标准>>> 环境变量PATH which 命令 which+命令名 //查找某个命令的绝对路径 如:查找rm命令的绝对路径 which的查 ...

  5. linux 解决man命令输出到文档打开乱码问题

    来源:http://www.th7.cn/system/lin/201406/59466.shtml 为了写一篇关于linux命令的文章,懒得手动去输入,就想着把man命令的内容输出到.txt文档,然 ...

  6. linux查看命令的帮助文档,Linux查看命令和帮助文件位置

    1. whereis简介 本文主要讲解whereis的功能与日常使用方法,该命令可以用于查找Linux指定命令的二进制文件.源文件和帮助文件的位置. 显示结果会去除主路径名和后缀名.whereis默认 ...

  7. 【转官方】Android ADB调试命令、支持的命令、ADB文档

    ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具. 持续更新中,欢迎提 PR 和 Issue 补充指 ...

  8. java文档注释生产api没有注释_如何使用javadoc命令生成api文档,文档注释

    /** * 计算器工具类 * * @62616964757a686964616fe78988e69d8331333365646332author GaoHuanjie * @version V1.0 ...

  9. 手把手教最新最全最详细Git使用教程(图文并茂,附Git命令大全学习文档)

    导读 因为教程详细,所以行文有些长,新手边看边操作效果出乎你的预料.GitHub虽然有些许改版,但并无大碍. 最全Git命令学习文档下载(集合整理,非常适合新手) 一.Git是什么? Git是目前世界 ...

最新文章

  1. B - Dungeon Master POJ - 2251
  2. SpringBoot事件与监听机制
  3. 在网页中插入百度地图的那些事
  4. noip退役之路--祝福
  5. leetcode350C++
  6. HTML5学习笔记简明版(8):新增的全局属性
  7. form data怎么接收_VUE发送Formdata数据,NodeJS接收
  8. jQuery数组处理汇总
  9. 微软发布的新开源编程语言 Power Fx
  10. 同步图计算:GraphLite的安装和使用
  11. android模糊搜索文件,模糊搜索_八门神器教程|安卓八门神器怎么用图文教程__单机攻略_跑跑车单机游戏网...
  12. c语言项目答辩演讲稿,关于竞选社团负责人的演讲稿
  13. 个人申请阿里云ICP备案流程
  14. 【karle 游戏】永劫无间太刀连招
  15. (三)5.自动控制原理 Time domain analysis and correct 稳态误差
  16. 组织机构树型数据库结构
  17. Docker镜像压缩和优化
  18. java 剔除工作日计算超时时间
  19. Autodock--基于windows10系统下的详细安装教程
  20. QT-日期倒计时功能的小坑

热门文章

  1. 外卖小哥、女装大佬、水泥匠工人,市井中的黑客群像
  2. redis 命令总结
  3. FFmpeg入门详解之6:VLC播放器简介
  4. PDFMaker文件遗失的解决方法
  5. Slide 插件的实现
  6. Arduino IAP(OTA)远程升级与Flash文件读写
  7. android平板待机电流,Android 功耗(4)---MTK平台待机功耗分析流程
  8. 华为nova5pro有没有搭载鸿蒙系统,华为nova 5 Pro现身GeekBench数据库:搭载麒麟980+8G内存...
  9. jq版本搜索文本关键字高亮
  10. movie计算机英语作文,求一篇以“Talkaboutyourfavoritemovie”为题的英语作文