前言
說到文本搜索工具,大家一定會知道 grep, 它是 linux 最有用并最常用的工具之一。
但如果要再一個大的工程項目中搜索某個關鍵詞,大家也一定知道它比較耗時。
所以就有了很多替代工具,之前最出名的是 Ack,Ag
而最近又有了新的替代者 Ripgrep, 這個工具和 Ack/Ag 一樣都使用了多線程的方法,但 rg 比它們更快
簡介
ripgrep 是一個以行為單位的搜索工具, 它根據提供的 pattern 遞歸地在指定的目錄里搜索。它是由 Rust 語言寫成,相較與同類工具,它的特點就是無與倫比地快。
幾個特點如下:
安裝 ripgrep
先安裝 RUST
curl https://sh.rustup.rs -sSf | sh
然后一路 enter 就好了
用 RUST 安裝 rigpre
git clone https://github.com/BurntSushi/ripgrepcd ripgrepcargo build --releasecp ./target/release/rg /usr/local/bin/
最后一步根據你的情況把它放到某個在 PATH 里的路徑里
使用
搜索結果展示

用法總體格式
USAGE: rg [OPTIONS] PATTERN [PATH ...] rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...] rg [OPTIONS] --files [PATH ...] rg [OPTIONS] --type-list command | rg [OPTIONS] PATTERN
輸入參數
ARGS: <PATTERN> A regular expression used for searching. To match a pattern beginning with a dash, use the -e/--regexp flag. 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 <PATH>... A file or directory to search. Directories are searched recursively. Paths specified on the command line override glob and ignore rules.
| options | Description | other | 
|---|---|---|
| -A, --after-context <NUM> | 顯示匹配內容后的<NUM>行 | 會覆蓋--context | 
| -B, --before-context <NUM> | 顯示匹配內容前的<NUM>行 | 會覆蓋--context | 
| -b, --byte-offset | 顯示匹配內容在文件中的字節偏移 | 和-o 一起使用,只打印偏移 | 
| -s, --case-sensitive | 大小寫敏感 | 會覆蓋-i(ignore case), -S(smart case) | 
| --color <WHEN> | 什么時候使用顏色,默認 auto | 如果--vimgre 被使用,那么默認值是 never | 
| 可選項有: never, auto, always, ansi | ||
| --colors <COLOR_SPEC>... | 設定輸出顏色: | color: red, blue, green, cyan | 
| {type}:{attribute}:{value} | magenta, yellow, white, black | |
| {type}: path, line, column, match | style: nobold, bold, nointense | |
| {attribute}: fg, bg, style | intense, nounderline, underline | |
| {value}: a color or a text style | Example: | |
| {type}:none會清空{type}的顏色設定 | rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo | |
| 擴展顏色集可以被 {value}使用,如果終端支持 ANSI color | ||
| 描述方法是'x'(256-color) 或 'x,x,x'(24-bit true color) | ||
| x 是 0-255 之間的數值,默認是 10 進制, 0x 前綴是 16 進制 | ||
| 比如: rg --colors 'match:bg:0,128,255' | ||
| 或者等價的:rg --colors 'match:bg:0x0,0x80,0xFF' | ||
| 在使用 extended color code 時 intense 和 nointense 是無效的 | ||
| --column | 第一次匹配所在列數(從 1 開始) | 能夠被--no-column 取消掉 | 
| -C, --context <NUM> | 顯示匹配內容的前面和后面的<NUM>行 | 它會覆蓋-B 和-A 選項 | 
| --context-separator <SEPARATOR> | 在輸出中用來分隔非連續的行 | x7F 或t 可被使用,默認是-- | 
| -c, --count | 只顯示匹配的行數 | 如果只有一個文件給 ripgrep,那只打印匹配行數 | 
| 可以用--with-filename 來強制打印文件名 | ||
| 它會覆蓋--count-matches 選項 | ||
| --count-matches | 只顯示匹配的次數 | 可以用--with-file 來強制在只有一個文件時也輸出文件名 | 
| --debug | 顯示調試信息 | |
| --dfa-size-limit <NUM+SUFFIX?> | regex DFA 的上限, 默認 10M | |
| -E, --encoding <ENCODING> | 描述文本編碼, 默認是 auto | https://encoding.spec.whatwg.org/#concept-encoding-get | 
| -f, --file <PATTERNFILE>... | 從文件中讀入 pattern, 一行一 pattern | 可以被多次使用或和-e 一起組合使用,所以有組合會被匹配 | 
| --files | 打印所有將被搜索的文件 | 以 rg <options> --files [PATH...]方式使用,不能加 pattern | 
| -l, --files-with-matches | 只打印有匹配的文件名 | 覆蓋--files-without-match | 
| --files-without-match | 只打印無匹配的文件名 | 覆蓋 --file-with-matches | 
| -F, --fixed-strings | 把 pattern 當成常規文字而非 regex | 可以用--no-fixed-strings 來禁止這個選項 | 
| -L, --follow | 會遞歸搜索鏈接,默認關閉 | 可以用--no-follow 來關閉 | 
| -g, --glob <GLOB>... | 通配符文件或文件夾,可以用!來取反 | 可以多次使用, 會匹配.gitignore 的通配符規則 | 
| -h, --help | 打印幫助信息 | |
| --heading | 打印文件名到匹配內容的上方而不是同一行 | 這是默認行為,可以用--no-heading 來關閉 | 
| --hidden | 搜索隱藏文件和文件夾 | 默認忽略, 可用--no-hidden 關閉 | 
| --iglob <GLOB>... | 同--glob, 但這個大小寫不敏感 | |
| -i, --ignore-case | pattern 大小寫不敏感 | 可通過-s/--case-sensitive 或-S/--smart-case 覆蓋這個選項 | 
| --ignore-file <PATH>... | 忽略路徑,格式同.gitignore, 可多個 | 多個--ignore-file 標記時,后面優先級高 | 
| 在命令上時,使用-g 來達到同樣效果 | ||
| -v, --invert-match | 反向匹配 | |
| -n, --line-number | 顯示文件行數,默認打開 | |
| -x, --line-regexp | 只顯示整行都匹配 pattern 的行 | 會覆蓋--word-regexp | 
| -M, --max-columns <NUM> | 不打印長于<NUM>列的匹配行 | |
| -m, --max-count <NUM> | 限制一個文件最多<NUM>行匹配 | |
| --max-depth <NUM> | 限制文件夾遞歸搜索深度 | rg --max-depth 0 dir/不執行任何搜索 | 
| --max-filesize <NUM+SUFFIX?> | 忽略大于<NUM>byte 的文件 | suffix 可以是 K, M,G, 默認是 byte | 
| --mmap | 盡量使用 memory maps, 默認行為 | 目前它不支持所有選項, 用--no-mmap 來關閉 | 
| --no-config | 不讀取 conf 文件, 忽略 RIPGREP_CONFIG_PATH | |
| --no-filename | 不要打印匹配文件名 | |
| --no-heading | 在每個匹配行前都打印文件名 | |
| --no-ignore | 取消 ignore 文件,如.gitignore, .ignore | 可以用--ignore 關閉 | 
| --no-ignore-global | 取消對全局的 ignore 文件讀取 | 如$HOME/.config/git/ignore | 
| --no-ignore-messages | 取消解析.ignroe, .gitignore 文件相關錯誤 | 可通過--ignore-messages 關閉 | 
| --no-ignore-parent | 不讀取父文件夾里的.gitignore, .ignore 文件 | 可通過 --ignore-parent 關閉 | 
| --no-ignore-vcs | 只全能.ignore 文件 | 可通過--ignore-vcs 關閉 | 
| -N, --no-line-number | 不打印匹配行數 | |
| --no-messages | 不打印打開和讀取文件相關錯誤 | |
| -0, --null | 在打印的文件路徑后加一個 NUL 字符 | 對于 xargs 非常有用 | 
| -o, --only-matching | 只打印匹配的內容,而不是整行 | |
| --passthru | 打印匹配和不匹配的行 | |
| --path-separator <SEPARATOR> | 路徑分隔符,在 linux 上默認是/ | |
| --pre <COMMAND> | 用<COMMAND>處理文件,并將結果給 rg | 可能有巨大的性能懲罰 | 
| 例如 | ||
| case "$1" in | ||
| *.pdf) | ||
| exec pdftotext "$1" - | ||
| ;; | ||
| *) | ||
| case $(file "$1") in | ||
| _Zstandard_) | ||
| exec pzstd -cdq | ||
| ;; | ||
| *) | ||
| exec cat | ||
| ;; | ||
| esac | ||
| ;; | ||
| esac | ||
| -p, --pretty | --color always --heading --line-number | |
| -q, --quiet | 不打印到 stdout, 如果匹配發現,停止 rg | 當 rg 用于 exit 代碼時非常有用 | 
| --regex-size-limit <NUM+SUFFIX?> | 編譯 regex 的上限 | |
| -e, --regexp <PATTERN>... | 使用正則來匹配 | 可多次使用這個選項,打印匹配任何 pattern 的行 | 
| 可以用于搜索-開頭的 pattern,如 rg -e -foo | ||
| -r, --replace <REPLACEMENT_TEXT> | 用相應文件代替匹配內容打印出來 | 組序號($5)可以被使用 | 
| -z, --search-zip | 在 gz,bz2,xz,lzma,lz4 文件類型中搜索 | 可通過--no-search-zip 關閉 | 
| -S, --smart-case | 如果全小寫,則大小寫不敏感,否則敏感 | 可通過-s/--case-sensitive 和-i/--ignore-case 關閉 | 
| --sort-files | 根據文件路徑排序輸出結果 | 會關閉并行搜索線程 | 
| --stats | 打印出統計結果 | |
| -a, --text | 搜索二進制文件 | 可通過--no-text 關閉 | 
| -j, --threads <NUM> | 大約使用的線程數 | |
| -t, --type <TYPE>... | 只搜索某種文件類型 | 可通過--type-lsit 來列出支持的文件類型 | 
| --type-add <TYPE_SPEC>... | 添加文件類型 | 如 rg --type-add 'foo:*.foo' -tfoo PATTERN | 
| 也可以用來創建某種包含多種文件類型的規則 | --type-add 'src:include:cpp,py,md' | |
| --type-clear <TYPE>... | 清除默認的文件類型 | |
| --type-list | 列出所有內置文件類型 | |
| -T, --type-not <TYPE>... | 不要搜索某種文件類型 | |
| -u, --unrestricted | -u 搜索.gitignore 里的文件, -uu 搜索隱藏文件 | -uuu 搜索二進制文件 | 
| -V, --version | 打印版本信息 | |
| --vimgrep | 每一次匹配打印一行 | 一行有多次匹配會打印多行 | 
| -H, --with-filename | 打印匹配的文件路徑,默認 | 可通過--no-filename 關閉 | 
| -w, --word-regexp | 把 pattern 作為單獨單詞匹配,與< >等價 | 
新聞熱點
疑難解答