rg - 當(dāng)前最快的文本搜索神器

前言

之前有寫過一篇文本搜索工具簡單使用博文:ag - the Silver Searcher鸽疾,后來發(fā)現(xiàn) ag 在 Windows 下搜索結(jié)果中文顯示亂碼,官方至今一直仍未解決测蘑。

并且現(xiàn)在灌危,最近幾年又出現(xiàn)了一款更加優(yōu)秀的文本搜索神器:ripgrep,稍微試了一下碳胳,速度很快勇蝙,跨平臺(tái)并且支持中文,很符合本人需求固逗。

下面就簡單介紹下 rg 及其常用的一些選項(xiàng)浅蚪。

簡介

依據(jù)正則匹配遞歸搜索目錄

依據(jù)給定的正則表達(dá)式,rg 以面向行為單位的模式進(jìn)行遞歸搜索烫罩。默認(rèn)情況下惜傲,rg 會(huì)尊重你的.gitignore配置以及自動(dòng)忽略隱藏文件/目錄和二進(jìn)制文件。

rg 具有以下一些特性:

  • 自動(dòng)遞歸搜索目錄(相當(dāng)于:grep -R
  • 自動(dòng)高亮匹配結(jié)果
  • 默認(rèn)自動(dòng)忽略.gitignore指定的文件贝攒,隱藏文件和二進(jìn)制文件
  • 可以搜索指定文件類型
  • 支持grep大部分常用特性
  • 支持各種文件編譯(UTF-8盗誊, UTF-16, latin-1, GBK, EUC-JP, Shift_JIS 等等)
  • 支持搜索常見壓縮文件(gzip,xz哈踱,lzma荒适,bzip2,lz4)
  • 不支持多行搜索和花哨的正則

用法

  • 格式:
USAGE:

    rg [OPTIONS] PATTERN [PATH ...]
    rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...]
    rg [OPTIONS] --files [PATH ...]
    rg [OPTIONS] --type-list
    command | rg [OPTIONS] PATTERN

舉個(gè)例子:

rg RxJava  // 遞歸搜索當(dāng)前目錄內(nèi)含 RxJava 的文件
rg install ReadMe.md // 在 ReadMe.md 中搜索字符串 install

常用選項(xiàng)簡介

  • 輸出選項(xiàng)

-g, --glob <GLOB>
Include or exclude files and directories for searching that match the given glob. This always overrides any other ignore logic. Multiple glob flags may be used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude it.
正則匹配添加或排除搜索某些類型文件或目錄开镣。在glob之前加上一個(gè)!表示排除搜索刀诬。

eg:在當(dāng)前目錄搜索require('uglifyjs-webpack-plugin')

rg -F "require('uglifyjs-webpack-plugin')" -g "*.md"  // 搜索所有`.md`文件
rg -F "require('uglifyjs-webpack-plugin')" -g "!*.md" // 不搜索`.md`文件

--iglob <GLOB>:同-g, --glob <GLOB>,但忽略大小寫。

-l, --files-with-matches
Only print the paths with at least one match.
只打印匹配內(nèi)容的文件名邪财。

-v, --invert-match
Invert matching. Show lines that do not match the given patterns.
反向匹配陕壹。

-C/--context [Lines]
Show the lines surrounding a match.
輸出匹配內(nèi)容前后[ LINES ]行內(nèi)容

eg:搜索require('uglifyjs-webpack-plugin'),輸出該行前后各2行內(nèi)容:

rg -C 2 "require\('uglifyjs-webpack-plugin'\)" // 括號(hào)需要轉(zhuǎn)義

-F, --fixed-strings
Treat the pattern as a literal string instead of a regular expression.
When this flag is used, special regular expression meta characters such as .(){}*+ do not need to be escaped.
將匹配字符作為字符串树埠,而不是正則表達(dá)式糠馆。也就是匹配字符.(){}*+無須進(jìn)行轉(zhuǎn)義。

eg:搜索require('uglifyjs-webpack-plugin')怎憋,輸出該行前后各2行內(nèi)容:

rg -C 2 -F "require('uglifyjs-webpack-plugin')" ./   // 括號(hào)無須轉(zhuǎn)義

--max-depth <NUM>
Limit the depth of directory traversal to NUM levels beyond the paths given. A value of zero only searches the explicitly given paths themselves.

For example, 'rg --max-depth 0 dir/' is a no-op because dir/ will not be
descended into. 'rg --max-depth 1 dir/' will search only the direct children of
'dir'.
限制文件夾遞歸搜索深度又碌。rg --max-depth 0 dir/則不執(zhí)行任何搜索;rg --max-depth 1 dir/只在dir/當(dāng)前目錄中進(jìn)行搜索绊袋。

-M/--max-columns
Limit the length of lines printed by ripgrep.
限制輸出最大行數(shù)毕匀。

--files
Print the files that ripgrep would search, but don't actually search them.
打印會(huì)進(jìn)行查找的文件,該選項(xiàng)并不會(huì)執(zhí)行實(shí)際查詢操作愤炸。
格式:rg [OPTIONS] --files [PATH ...],此處不能加pattern期揪。

eg:打印當(dāng)前文件會(huì)進(jìn)行查找的文件:

rg --files . // 列出當(dāng)前文件夾會(huì)進(jìn)行查詢的所有文件

:該選項(xiàng)其實(shí)可相當(dāng)于:find . -type f,查找當(dāng)前目錄所有文件

-c/--count
Report a count of total matched lines.
計(jì)算匹配文件數(shù)量

--debug
Shows ripgrep's debug output. This is useful for understanding why a particular file might be ignored from search, or what kinds of configuration ripgrep is loading from the environment.
顯示調(diào)試信息规个。有利于了解某一個(gè)具體文件被忽略的原因,或者 rg 從環(huán)境變量中加載了什么配置姓建。

  • 輸入選項(xiàng)

-e, --regexp <PATTERN>
A pattern to search for. This option can be provided multiple times, where all patterns given are searched. Lines matching at least one of the provided patterns are printed. This flag can also be used when searching for patterns that start with a dash.

For example, to search for the literal '-foo', you can use this flag: rg -e -foo

You can also use the special '--' delimiter to indicate that no more flags will be provided. Namely, the following is equivalent to the above: rg -- -foo
使用正則搜索诞仓。

rg 本身就支持正則表達(dá)式,-e主要用于匹配前綴帶-的字符串 或者 想匹配多個(gè)字符串(多次使用該選項(xiàng)即可)速兔。

eg:同時(shí)查找'hello' 和 'world':

echo 'hi hello world' | rg -e 'hello' -e 'world'
echo 'hi hello world' | rg 'hello|world'          // 或者直接使用正則

eg:搜索內(nèi)容包含startXXXend的字符串:

rg -e "start.*end" .  // 這樣墅拭,`startXXXend`,`XXXstartendXXX`涣狗,`XXXstartXXXend`谍婉,`XXXstartXXXendXXX`都會(huì)匹配
rg "start.*end" .     // 與上述一致功能

-i/--ignore-case
When searching for a pattern, ignore case differences. That is rg -i fast matches fast, fASt, FAST, etc.
忽略大小寫

-S/--smart-case
This is similar to --ignore-case, but disables itself if the pattern contains any uppercase letters. Usually this flag is put into alias or a config file.
打開智能大小寫,通常該選項(xiàng)相當(dāng)于--ignore-case,但在輸入大寫時(shí)镀钓,則取消忽略大小寫功能穗熬。

-w/--word-regexp
Require that all matches of the pattern be surrounded by word boundaries. That is, given pattern, the --word-regexp flag will cause ripgrep to behave as if pattern were actually \b(?:pattern)\b.
打開單詞邊界,只進(jìn)行單詞匹配丁溅。

-a/--text
Search binary files as if they were plain text.
搜索二進(jìn)制文件(將二進(jìn)制文件看出文本文件)

--hidden
Search hidden files and directories. By default, hidden files and directories are skipped. Note that if a hidden file or a directory is whitelisted in an ignore file, then it will be searched even if this flag isn't provided.
搜索隱藏文件唤蔗。默認(rèn)不搜索隱藏文件,如果隱藏文件處于.gitignore配置白名單中,則會(huì)進(jìn)行搜索妓柜,即使沒有提供該選項(xiàng)箱季。

-r, --replace <REPLACEMENT_TEXT>
Replace every match with the text given when printing results. Neither this flag nor any other ripgrep flag will modify your files.

Capture group indices (e.g., $5) and names (e.g., $foo) are supported in the replacement string.

Note that the replacement by default replaces each match, and NOT the entire line. To replace the entire line, you should match the entire line.

This flag can be used with the -o/--only-matching flag.
字符替換。將匹配的字符替換成自定義字符棍掐,該選項(xiàng)不會(huì)更改源文件藏雏,只是替換輸出顯示。

eg:查找文件類型為.py的文件作煌,并將其路徑中的\改為/

rg --files . | rg -F ".py"| rg -F \ -r /

其他

  • 自動(dòng)過濾:前面說過掘殴,rg 在遞歸搜索時(shí),會(huì)自動(dòng)過濾.gitignore匹配規(guī)則最疆,忽略隱藏文件和目錄杯巨,忽略二進(jìn)制文件和鏈接。
    但所有這些過濾都可以通過各自指定標(biāo)記進(jìn)行消除:
    1)--no-ignore/-u:不響應(yīng).gitignore的匹配規(guī)則
    2)--hidden/-uu:搜索隱藏文件和目錄
    3)-a/--text/-uuu:搜索二進(jìn)制文件
    4)-L/--folow:追蹤鏈接文件

  • 手動(dòng)過濾:globs(-g, --glob:手動(dòng)過濾文件類型與目錄努酸,其會(huì)按與.gitignore·一樣的模式被解析服爷,也就是,位于后面的 glob 會(huì)替換前面的 glob获诈。
    舉個(gè)例子:rg clap -g "*.toml" -g "!*.toml"仍源,其實(shí)相當(dāng)于:rg clap -g "!*.toml",也就是不會(huì)搜索.toml文件了舔涎。

rg word -g "*.py"        // 查找 .py 文件內(nèi)含 word
rg word -g "!test/*"     // 排除當(dāng)前目錄下的 test 目錄
rg --file . -g "!**/.git/**" // 排除當(dāng)前目錄及其子目錄下的 .git 目錄
  • 手動(dòng)過濾:文件類型(-g, --glob:使用上述 globs 模式其實(shí)就能實(shí)現(xiàn)文件類型與目錄的過濾了笼踩,但如果只想過濾文件類型,可直接使用選項(xiàng):-t/--type亡嫌。
    舉個(gè)例子:
rg "fn run" --type rust // 文件類型:rust
rg "fn run" --trust     // 文件類型:rust嚎于,更簡潔
rg "int main" -tc       // 文件類型:C,包含`.c`和`.h`文件挟冠,相當(dāng)于:rg "int main" -g "*.{c,h}"

rg clap --type-not rust // 排序 rust 文件
rg clap -Trust          // 排序 rust 文件,更簡潔

:即-t包含文件類型于购,-T排除文件類型。
文件類型可通過:rg --type-list進(jìn)行查看知染。

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肋僧,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子控淡,更是在濱河造成了極大的恐慌嫌吠,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掺炭,死亡現(xiàn)場離奇詭異辫诅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)竹伸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門泥栖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來簇宽,“玉大人,你說我怎么就攤上這事吧享∥焊睿” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵钢颂,是天一觀的道長钞它。 經(jīng)常有香客問我,道長殊鞭,這世上最難降的妖魔是什么遭垛? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮操灿,結(jié)果婚禮上锯仪,老公的妹妹穿的比我還像新娘。我一直安慰自己趾盐,他們只是感情好庶喜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著救鲤,像睡著了一般久窟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上本缠,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天斥扛,我揣著相機(jī)與錄音,去河邊找鬼丹锹。 笑死稀颁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的楣黍。 我是一名探鬼主播峻村,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼锡凝!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起垢啼,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤窜锯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝴悉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年秘遏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亏钩。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡都伪,死狀恐怖钱床,靈堂內(nèi)的尸體忽然破棺而出翠勉,到底是詐尸還是另有隱情妖啥,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布对碌,位于F島的核電站荆虱,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏朽们。R本人自食惡果不足惜怀读,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望骑脱。 院中可真熱鬧菜枷,春花似錦、人聲如沸叁丧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽歹袁。三九已至坷衍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間条舔,已是汗流浹背枫耳。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留孟抗,地道東北人迁杨。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像凄硼,于是被迫代替她去往敵國和親铅协。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,383評(píng)論 0 5
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評(píng)論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,457評(píng)論 0 13
  • 地方 - 四諦法 - 四諦法 ################### Filebeat Configuration...
    linghu323323閱讀 999評(píng)論 0 1
  • ################### Filebeat Configuration Example ######...
    ssdsss閱讀 9,716評(píng)論 1 1