PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://github.com/BurntSushi/ripgrep
Markdown | 433 lines | 327 code | 106 blank | 0 comment | 0 complexity | 01b5976bf6357694134dc62951292cb3 MD5 | raw file
Possible License(s): MIT, Unlicense
  1. ripgrep (rg)
  2. ------------
  3. ripgrep is a line-oriented search tool that recursively searches your current
  4. directory for a regex pattern. By default, ripgrep will respect your .gitignore
  5. and automatically skip hidden files/directories and binary files. ripgrep
  6. has first class support on Windows, macOS and Linux, with binary downloads
  7. available for [every release](https://github.com/BurntSushi/ripgrep/releases).
  8. ripgrep is similar to other popular search tools like The Silver Searcher, ack
  9. and grep.
  10. [![Build status](https://github.com/BurntSushi/ripgrep/workflows/ci/badge.svg)](https://github.com/BurntSushi/ripgrep/actions)
  11. [![Crates.io](https://img.shields.io/crates/v/ripgrep.svg)](https://crates.io/crates/ripgrep)
  12. [![Packaging status](https://repology.org/badge/tiny-repos/ripgrep.svg)](https://repology.org/project/ripgrep/badges)
  13. Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org).
  14. ### CHANGELOG
  15. Please see the [CHANGELOG](CHANGELOG.md) for a release history.
  16. ### Documentation quick links
  17. * [Installation](#installation)
  18. * [User Guide](GUIDE.md)
  19. * [Frequently Asked Questions](FAQ.md)
  20. * [Regex syntax](https://docs.rs/regex/1/regex/#syntax)
  21. * [Configuration files](GUIDE.md#configuration-file)
  22. * [Shell completions](FAQ.md#complete)
  23. * [Building](#building)
  24. * [Translations](#translations)
  25. ### Screenshot of search results
  26. [![A screenshot of a sample search with ripgrep](https://burntsushi.net/stuff/ripgrep1.png)](https://burntsushi.net/stuff/ripgrep1.png)
  27. ### Quick examples comparing tools
  28. This example searches the entire
  29. [Linux kernel source tree](https://github.com/BurntSushi/linux)
  30. (after running `make defconfig && make -j8`) for `[A-Z]+_SUSPEND`, where
  31. all matches must be words. Timings were collected on a system with an Intel
  32. i7-6900K 3.2 GHz.
  33. Please remember that a single benchmark is never enough! See my
  34. [blog post on ripgrep](https://blog.burntsushi.net/ripgrep/)
  35. for a very detailed comparison with more benchmarks and analysis.
  36. | Tool | Command | Line count | Time |
  37. | ---- | ------- | ---------- | ---- |
  38. | ripgrep (Unicode) | `rg -n -w '[A-Z]+_SUSPEND'` | 452 | **0.136s** |
  39. | [git grep](https://www.kernel.org/pub/software/scm/git/docs/git-grep.html) | `git grep -P -n -w '[A-Z]+_SUSPEND'` | 452 | 0.348s |
  40. | [ugrep (Unicode)](https://github.com/Genivia/ugrep) | `ugrep -r --ignore-files --no-hidden -I -w '[A-Z]+_SUSPEND'` | 452 | 0.506s |
  41. | [The Silver Searcher](https://github.com/ggreer/the_silver_searcher) | `ag -w '[A-Z]+_SUSPEND'` | 452 | 0.654s |
  42. | [git grep](https://www.kernel.org/pub/software/scm/git/docs/git-grep.html) | `LC_ALL=C git grep -E -n -w '[A-Z]+_SUSPEND'` | 452 | 1.150s |
  43. | [ack](https://github.com/beyondgrep/ack3) | `ack -w '[A-Z]+_SUSPEND'` | 452 | 4.054s |
  44. | [git grep (Unicode)](https://www.kernel.org/pub/software/scm/git/docs/git-grep.html) | `LC_ALL=en_US.UTF-8 git grep -E -n -w '[A-Z]+_SUSPEND'` | 452 | 4.205s |
  45. Here's another benchmark on the same corpus as above that disregards gitignore
  46. files and searches with a whitelist instead. The corpus is the same as in the
  47. previous benchmark, and the flags passed to each command ensure that they are
  48. doing equivalent work:
  49. | Tool | Command | Line count | Time |
  50. | ---- | ------- | ---------- | ---- |
  51. | ripgrep | `rg -uuu -tc -n -w '[A-Z]+_SUSPEND'` | 388 | **0.096s** |
  52. | [ugrep](https://github.com/Genivia/ugrep) | `ugrep -r -n --include='*.c' --include='*.h' -w '[A-Z]+_SUSPEND'` | 388 | 0.493s |
  53. | [GNU grep](https://www.gnu.org/software/grep/) | `egrep -r -n --include='*.c' --include='*.h' -w '[A-Z]+_SUSPEND'` | 388 | 0.806s |
  54. And finally, a straight-up comparison between ripgrep, ugrep and GNU grep on a
  55. single large file cached in memory
  56. (~13GB, [`OpenSubtitles.raw.en.gz`](http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.en.gz)):
  57. | Tool | Command | Line count | Time |
  58. | ---- | ------- | ---------- | ---- |
  59. | ripgrep | `rg -w 'Sherlock [A-Z]\w+'` | 7882 | **2.769s** |
  60. | [ugrep](https://github.com/Genivia/ugrep) | `ugrep -w 'Sherlock [A-Z]\w+'` | 7882 | 6.802s |
  61. | [GNU grep](https://www.gnu.org/software/grep/) | `LC_ALL=en_US.UTF-8 egrep -w 'Sherlock [A-Z]\w+'` | 7882 | 9.027s |
  62. In the above benchmark, passing the `-n` flag (for showing line numbers)
  63. increases the times to `3.423s` for ripgrep and `13.031s` for GNU grep. ugrep
  64. times are unaffected by the presence or absence of `-n`.
  65. ### Why should I use ripgrep?
  66. * It can replace many use cases served by other search tools
  67. because it contains most of their features and is generally faster. (See
  68. [the FAQ](FAQ.md#posix4ever) for more details on whether ripgrep can truly
  69. replace grep.)
  70. * Like other tools specialized to code search, ripgrep defaults to recursive
  71. directory search and won't search files ignored by your
  72. `.gitignore`/`.ignore`/`.rgignore` files. It also ignores hidden and binary
  73. files by default. ripgrep also implements full support for `.gitignore`,
  74. whereas there are many bugs related to that functionality in other code
  75. search tools claiming to provide the same functionality.
  76. * ripgrep can search specific types of files. For example, `rg -tpy foo`
  77. limits your search to Python files and `rg -Tjs foo` excludes Javascript
  78. files from your search. ripgrep can be taught about new file types with
  79. custom matching rules.
  80. * ripgrep supports many features found in `grep`, such as showing the context
  81. of search results, searching multiple patterns, highlighting matches with
  82. color and full Unicode support. Unlike GNU grep, ripgrep stays fast while
  83. supporting Unicode (which is always on).
  84. * ripgrep has optional support for switching its regex engine to use PCRE2.
  85. Among other things, this makes it possible to use look-around and
  86. backreferences in your patterns, which are not supported in ripgrep's default
  87. regex engine. PCRE2 support can be enabled with `-P/--pcre2` (use PCRE2
  88. always) or `--auto-hybrid-regex` (use PCRE2 only if needed). An alternative
  89. syntax is provided via the `--engine (default|pcre2|auto-hybrid)` option.
  90. * ripgrep supports searching files in text encodings other than UTF-8, such
  91. as UTF-16, latin-1, GBK, EUC-JP, Shift_JIS and more. (Some support for
  92. automatically detecting UTF-16 is provided. Other text encodings must be
  93. specifically specified with the `-E/--encoding` flag.)
  94. * ripgrep supports searching files compressed in a common format (brotli,
  95. bzip2, gzip, lz4, lzma, xz, or zstandard) with the `-z/--search-zip` flag.
  96. * ripgrep supports
  97. [arbitrary input preprocessing filters](GUIDE.md#preprocessor)
  98. which could be PDF text extraction, less supported decompression, decrypting,
  99. automatic encoding detection and so on.
  100. In other words, use ripgrep if you like speed, filtering by default, fewer
  101. bugs and Unicode support.
  102. ### Why shouldn't I use ripgrep?
  103. Despite initially not wanting to add every feature under the sun to ripgrep,
  104. over time, ripgrep has grown support for most features found in other file
  105. searching tools. This includes searching for results spanning across multiple
  106. lines, and opt-in support for PCRE2, which provides look-around and
  107. backreference support.
  108. At this point, the primary reasons not to use ripgrep probably consist of one
  109. or more of the following:
  110. * You need a portable and ubiquitous tool. While ripgrep works on Windows,
  111. macOS and Linux, it is not ubiquitous and it does not conform to any
  112. standard such as POSIX. The best tool for this job is good old grep.
  113. * There still exists some other feature (or bug) not listed in this README that
  114. you rely on that's in another tool that isn't in ripgrep.
  115. * There is a performance edge case where ripgrep doesn't do well where another
  116. tool does do well. (Please file a bug report!)
  117. * ripgrep isn't possible to install on your machine or isn't available for your
  118. platform. (Please file a bug report!)
  119. ### Is it really faster than everything else?
  120. Generally, yes. A large number of benchmarks with detailed analysis for each is
  121. [available on my blog](https://blog.burntsushi.net/ripgrep/).
  122. Summarizing, ripgrep is fast because:
  123. * It is built on top of
  124. [Rust's regex engine](https://github.com/rust-lang/regex).
  125. Rust's regex engine uses finite automata, SIMD and aggressive literal
  126. optimizations to make searching very fast. (PCRE2 support can be opted into
  127. with the `-P/--pcre2` flag.)
  128. * Rust's regex library maintains performance with full Unicode support by
  129. building UTF-8 decoding directly into its deterministic finite automaton
  130. engine.
  131. * It supports searching with either memory maps or by searching incrementally
  132. with an intermediate buffer. The former is better for single files and the
  133. latter is better for large directories. ripgrep chooses the best searching
  134. strategy for you automatically.
  135. * Applies your ignore patterns in `.gitignore` files using a
  136. [`RegexSet`](https://docs.rs/regex/1/regex/struct.RegexSet.html).
  137. That means a single file path can be matched against multiple glob patterns
  138. simultaneously.
  139. * It uses a lock-free parallel recursive directory iterator, courtesy of
  140. [`crossbeam`](https://docs.rs/crossbeam) and
  141. [`ignore`](https://docs.rs/ignore).
  142. ### Feature comparison
  143. Andy Lester, author of [ack](https://beyondgrep.com/), has published an
  144. excellent table comparing the features of ack, ag, git-grep, GNU grep and
  145. ripgrep: https://beyondgrep.com/feature-comparison/
  146. Note that ripgrep has grown a few significant new features recently that
  147. are not yet present in Andy's table. This includes, but is not limited to,
  148. configuration files, passthru, support for searching compressed files,
  149. multiline search and opt-in fancy regex support via PCRE2.
  150. ### Installation
  151. The binary name for ripgrep is `rg`.
  152. **[Archives of precompiled binaries for ripgrep are available for Windows,
  153. macOS and Linux.](https://github.com/BurntSushi/ripgrep/releases)** Users of
  154. platforms not explicitly mentioned below are advised to download one of these
  155. archives.
  156. Linux binaries are static executables. Windows binaries are available either as
  157. built with MinGW (GNU) or with Microsoft Visual C++ (MSVC). When possible,
  158. prefer MSVC over GNU, but you'll need to have the [Microsoft VC++ 2015
  159. redistributable](https://www.microsoft.com/en-us/download/details.aspx?id=48145)
  160. installed.
  161. If you're a **macOS Homebrew** or a **Linuxbrew** user, then you can install
  162. ripgrep from homebrew-core:
  163. ```
  164. $ brew install ripgrep
  165. ```
  166. If you're a **MacPorts** user, then you can install ripgrep from the
  167. [official ports](https://www.macports.org/ports.php?by=name&substr=ripgrep):
  168. ```
  169. $ sudo port install ripgrep
  170. ```
  171. If you're a **Windows Chocolatey** user, then you can install ripgrep from the
  172. [official repo](https://chocolatey.org/packages/ripgrep):
  173. ```
  174. $ choco install ripgrep
  175. ```
  176. If you're a **Windows Scoop** user, then you can install ripgrep from the
  177. [official bucket](https://github.com/ScoopInstaller/Main/blob/master/bucket/ripgrep.json):
  178. ```
  179. $ scoop install ripgrep
  180. ```
  181. If you're an **Arch Linux** user, then you can install ripgrep from the official repos:
  182. ```
  183. $ pacman -S ripgrep
  184. ```
  185. If you're a **Gentoo** user, you can install ripgrep from the
  186. [official repo](https://packages.gentoo.org/packages/sys-apps/ripgrep):
  187. ```
  188. $ emerge sys-apps/ripgrep
  189. ```
  190. If you're a **Fedora** user, you can install ripgrep from official
  191. repositories.
  192. ```
  193. $ sudo dnf install ripgrep
  194. ```
  195. If you're an **openSUSE** user, ripgrep is included in **openSUSE Tumbleweed**
  196. and **openSUSE Leap** since 15.1.
  197. ```
  198. $ sudo zypper install ripgrep
  199. ```
  200. If you're a **RHEL/CentOS 7/8** user, you can install ripgrep from
  201. [copr](https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/):
  202. ```
  203. $ sudo yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/repo/epel-7/carlwgeorge-ripgrep-epel-7.repo
  204. $ sudo yum install ripgrep
  205. ```
  206. If you're a **Nix** user, you can install ripgrep from
  207. [nixpkgs](https://github.com/NixOS/nixpkgs/blob/master/pkgs/tools/text/ripgrep/default.nix):
  208. ```
  209. $ nix-env --install ripgrep
  210. $ # (Or using the attribute name, which is also ripgrep.)
  211. ```
  212. If you're a **Debian** user (or a user of a Debian derivative like **Ubuntu**),
  213. then ripgrep can be installed using a binary `.deb` file provided in each
  214. [ripgrep release](https://github.com/BurntSushi/ripgrep/releases).
  215. ```
  216. $ curl -LO https://github.com/BurntSushi/ripgrep/releases/download/11.0.2/ripgrep_11.0.2_amd64.deb
  217. $ sudo dpkg -i ripgrep_11.0.2_amd64.deb
  218. ```
  219. If you run Debian Buster (currently Debian stable) or Debian sid, ripgrep is
  220. [officially maintained by Debian](https://tracker.debian.org/pkg/rust-ripgrep).
  221. ```
  222. $ sudo apt-get install ripgrep
  223. ```
  224. If you're an **Ubuntu Cosmic (18.10)** (or newer) user, ripgrep is
  225. [available](https://launchpad.net/ubuntu/+source/rust-ripgrep) using the same
  226. packaging as Debian:
  227. ```
  228. $ sudo apt-get install ripgrep
  229. ```
  230. (N.B. Various snaps for ripgrep on Ubuntu are also available, but none of them
  231. seem to work right and generate a number of very strange bug reports that I
  232. don't know how to fix and don't have the time to fix. Therefore, it is no
  233. longer a recommended installation option.)
  234. If you're a **FreeBSD** user, then you can install ripgrep from the
  235. [official ports](https://www.freshports.org/textproc/ripgrep/):
  236. ```
  237. # pkg install ripgrep
  238. ```
  239. If you're an **OpenBSD** user, then you can install ripgrep from the
  240. [official ports](http://openports.se/textproc/ripgrep):
  241. ```
  242. $ doas pkg_add ripgrep
  243. ```
  244. If you're a **NetBSD** user, then you can install ripgrep from
  245. [pkgsrc](http://pkgsrc.se/textproc/ripgrep):
  246. ```
  247. # pkgin install ripgrep
  248. ```
  249. If you're a **Haiku x86_64** user, then you can install ripgrep from the
  250. [official ports](https://github.com/haikuports/haikuports/tree/master/sys-apps/ripgrep):
  251. ```
  252. $ pkgman install ripgrep
  253. ```
  254. If you're a **Haiku x86_gcc2** user, then you can install ripgrep from the
  255. same port as Haiku x86_64 using the x86 secondary architecture build:
  256. ```
  257. $ pkgman install ripgrep_x86
  258. ```
  259. If you're a **Rust programmer**, ripgrep can be installed with `cargo`.
  260. * Note that the minimum supported version of Rust for ripgrep is **1.34.0**,
  261. although ripgrep may work with older versions.
  262. * Note that the binary may be bigger than expected because it contains debug
  263. symbols. This is intentional. To remove debug symbols and therefore reduce
  264. the file size, run `strip` on the binary.
  265. ```
  266. $ cargo install ripgrep
  267. ```
  268. ### Building
  269. ripgrep is written in Rust, so you'll need to grab a
  270. [Rust installation](https://www.rust-lang.org/) in order to compile it.
  271. ripgrep compiles with Rust 1.34.0 (stable) or newer. In general, ripgrep tracks
  272. the latest stable release of the Rust compiler.
  273. To build ripgrep:
  274. ```
  275. $ git clone https://github.com/BurntSushi/ripgrep
  276. $ cd ripgrep
  277. $ cargo build --release
  278. $ ./target/release/rg --version
  279. 0.1.3
  280. ```
  281. If you have a Rust nightly compiler and a recent Intel CPU, then you can enable
  282. additional optional SIMD acceleration like so:
  283. ```
  284. RUSTFLAGS="-C target-cpu=native" cargo build --release --features 'simd-accel'
  285. ```
  286. The `simd-accel` feature enables SIMD support in certain ripgrep dependencies
  287. (responsible for transcoding). They are not necessary to get SIMD optimizations
  288. for search; those are enabled automatically. Hopefully, some day, the
  289. `simd-accel` feature will similarly become unnecessary. **WARNING:** Currently,
  290. enabling this option can increase compilation times dramatically.
  291. Finally, optional PCRE2 support can be built with ripgrep by enabling the
  292. `pcre2` feature:
  293. ```
  294. $ cargo build --release --features 'pcre2'
  295. ```
  296. (Tip: use `--features 'pcre2 simd-accel'` to also include compile time SIMD
  297. optimizations, which will only work with a nightly compiler.)
  298. Enabling the PCRE2 feature works with a stable Rust compiler and will
  299. attempt to automatically find and link with your system's PCRE2 library via
  300. `pkg-config`. If one doesn't exist, then ripgrep will build PCRE2 from source
  301. using your system's C compiler and then statically link it into the final
  302. executable. Static linking can be forced even when there is an available PCRE2
  303. system library by either building ripgrep with the MUSL target or by setting
  304. `PCRE2_SYS_STATIC=1`.
  305. ripgrep can be built with the MUSL target on Linux by first installing the MUSL
  306. library on your system (consult your friendly neighborhood package manager).
  307. Then you just need to add MUSL support to your Rust toolchain and rebuild
  308. ripgrep, which yields a fully static executable:
  309. ```
  310. $ rustup target add x86_64-unknown-linux-musl
  311. $ cargo build --release --target x86_64-unknown-linux-musl
  312. ```
  313. Applying the `--features` flag from above works as expected. If you want to
  314. build a static executable with MUSL and with PCRE2, then you will need to have
  315. `musl-gcc` installed, which might be in a separate package from the actual
  316. MUSL library, depending on your Linux distribution.
  317. ### Running tests
  318. ripgrep is relatively well-tested, including both unit tests and integration
  319. tests. To run the full test suite, use:
  320. ```
  321. $ cargo test --all
  322. ```
  323. from the repository root.
  324. ### Translations
  325. The following is a list of known translations of ripgrep's documentation. These
  326. are unofficially maintained and may not be up to date.
  327. * [Chinese](https://github.com/chinanf-boy/ripgrep-zh#%E6%9B%B4%E6%96%B0-)