PageRenderTime 143ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/FAQ.md

https://github.com/BurntSushi/ripgrep
Markdown | 1003 lines | 783 code | 220 blank | 0 comment | 0 complexity | 16f5c9ecb330d026743062f9dbefed17 MD5 | raw file
Possible License(s): MIT, Unlicense
  1. ## FAQ
  2. * [Does ripgrep support configuration files?](#config)
  3. * [What's changed in ripgrep recently?](#changelog)
  4. * [When is the next release?](#release)
  5. * [Does ripgrep have a man page?](#manpage)
  6. * [Does ripgrep have support for shell auto-completion?](#complete)
  7. * [How do I use lookaround and/or backreferences?](#fancy)
  8. * [How do I configure ripgrep's colors?](#colors)
  9. * [How do I enable true colors on Windows?](#truecolors-windows)
  10. * [How do I stop ripgrep from messing up colors when I kill it?](#stop-ripgrep)
  11. * [How can I get results in a consistent order?](#order)
  12. * [How do I search files that aren't UTF-8?](#encoding)
  13. * [How do I search compressed files?](#compressed)
  14. * [How do I search over multiple lines?](#multiline)
  15. * [How do I get around the regex size limit?](#size-limit)
  16. * [How do I make the `-f/--file` flag faster?](#dfa-size)
  17. * [How do I make the output look like The Silver Searcher's output?](#silver-searcher-output)
  18. * [Why does ripgrep get slower when I enabled PCRE2 regexes?](#pcre2-slow)
  19. * [When I run `rg`, why does it execute some other command?](#rg-other-cmd)
  20. * [How do I create an alias for ripgrep on Windows?](#rg-alias-windows)
  21. * [How do I create a PowerShell profile?](#powershell-profile)
  22. * [How do I pipe non-ASCII content to ripgrep on Windows?](#pipe-non-ascii-windows)
  23. * [How can I search and replace with ripgrep?](#search-and-replace)
  24. * [How is ripgrep licensed?](#license)
  25. * [Can ripgrep replace grep?](#posix4ever)
  26. * [What does the "rip" in ripgrep mean?](#intentcountsforsomething)
  27. * [How can I donate to ripgrep or its maintainers?](#donations)
  28. <h3 name="config">
  29. Does ripgrep support configuration files?
  30. </h3>
  31. Yes. See the
  32. [guide's section on configuration files](GUIDE.md#configuration-file).
  33. <h3 name="changelog">
  34. What's changed in ripgrep recently?
  35. </h3>
  36. Please consult ripgrep's [CHANGELOG](CHANGELOG.md).
  37. <h3 name="release">
  38. When is the next release?
  39. </h3>
  40. ripgrep is a project whose contributors are volunteers. A release schedule
  41. adds undue stress to said volunteers. Therefore, releases are made on a best
  42. effort basis and no dates **will ever be given**.
  43. An exception to this _can be_ high impact bugs. If a ripgrep release contains
  44. a significant regression, then there will generally be a strong push to get a
  45. patch release out with a fix. However, no promises are made.
  46. <h3 name="manpage">
  47. Does ripgrep have a man page?
  48. </h3>
  49. Yes! Whenever ripgrep is compiled on a system with `asciidoctor` or `asciidoc`
  50. present, then a man page is generated from ripgrep's argv parser. After
  51. compiling ripgrep, you can find the man page like so from the root of the
  52. repository:
  53. ```
  54. $ find ./target -name rg.1 -print0 | xargs -0 ls -t | head -n1
  55. ./target/debug/build/ripgrep-79899d0edd4129ca/out/rg.1
  56. ```
  57. Running `man -l ./target/debug/build/ripgrep-79899d0edd4129ca/out/rg.1` will
  58. show the man page in your normal pager.
  59. Note that the man page's documentation for options is equivalent to the output
  60. shown in `rg --help`. To see more condensed documentation (one line per flag),
  61. run `rg -h`.
  62. The man page is also included in all
  63. [ripgrep binary releases](https://github.com/BurntSushi/ripgrep/releases).
  64. <h3 name="complete">
  65. Does ripgrep have support for shell auto-completion?
  66. </h3>
  67. Yes! Shell completions can be found in the
  68. [same directory as the man page](#manpage)
  69. after building ripgrep. Zsh completions are maintained separately and committed
  70. to the repository in `complete/_rg`.
  71. Shell completions are also included in all
  72. [ripgrep binary releases](https://github.com/BurntSushi/ripgrep/releases).
  73. For **bash**, move `rg.bash` to
  74. `$XDG_CONFIG_HOME/bash_completion` or `/etc/bash_completion.d/`.
  75. For **fish**, move `rg.fish` to `$HOME/.config/fish/completions/`.
  76. For **zsh**, move `_rg` to one of your `$fpath` directories.
  77. For **PowerShell**, add `. _rg.ps1` to your PowerShell
  78. [profile](https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx)
  79. (note the leading period). If the `_rg.ps1` file is not on your `PATH`, do
  80. `. /path/to/_rg.ps1` instead.
  81. <h3 name="order">
  82. How can I get results in a consistent order?
  83. </h3>
  84. By default, ripgrep uses parallelism to execute its search because this makes
  85. the search much faster on most modern systems. This in turn means that ripgrep
  86. has a non-deterministic aspect to it, since the interleaving of threads during
  87. the execution of the program is itself non-deterministic. This has the effect
  88. of printing results in a somewhat arbitrary order, and this order can change
  89. from run to run of ripgrep.
  90. The only way to make the order of results consistent is to ask ripgrep to
  91. sort the output. Currently, this will disable all parallelism. (On smaller
  92. repositories, you might not notice much of a performance difference!) You
  93. can achieve this with the `--sort path` flag.
  94. There is more discussion on this topic here:
  95. https://github.com/BurntSushi/ripgrep/issues/152
  96. <h3 name="encoding">
  97. How do I search files that aren't UTF-8?
  98. </h3>
  99. See the [guide's section on file encoding](GUIDE.md#file-encoding).
  100. <h3 name="compressed">
  101. How do I search compressed files?
  102. </h3>
  103. ripgrep's `-z/--search-zip` flag will cause it to search compressed files
  104. automatically. Currently, this supports gzip, bzip2, xz, lzma, lz4, Brotli and
  105. Zstd. Each of these requires requires the corresponding `gzip`, `bzip2`, `xz`,
  106. `lz4`, `brotli` and `zstd` binaries to be installed on your system. (That is,
  107. ripgrep does decompression by shelling out to another process.)
  108. ripgrep currently does not search archive formats, so `*.tar.gz` files, for
  109. example, are skipped.
  110. <h3 name="multiline">
  111. How do I search over multiple lines?
  112. </h3>
  113. The `-U/--multiline` flag enables ripgrep to report results that span over
  114. multiple lines.
  115. <h3 name="fancy">
  116. How do I use lookaround and/or backreferences?
  117. </h3>
  118. ripgrep's default regex engine does not support lookaround or backreferences.
  119. This is primarily because the default regex engine is implemented using finite
  120. state machines in order to guarantee a linear worst case time complexity on all
  121. inputs. Backreferences are not possible to implement in this paradigm, and
  122. lookaround appears difficult to do efficiently.
  123. However, ripgrep optionally supports using PCRE2 as the regex engine instead of
  124. the default one based on finite state machines. You can enable PCRE2 with the
  125. `-P/--pcre2` flag. For example, in the root of the ripgrep repo, you can easily
  126. find all palindromes:
  127. ```
  128. $ rg -P '(\w{10})\1'
  129. tests/misc.rs
  130. 483: cmd.arg("--max-filesize").arg("44444444444444444444");
  131. globset/src/glob.rs
  132. 1206: matches!(match7, "a*a*a*a*a*a*a*a*a", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  133. ```
  134. If your version of ripgrep doesn't support PCRE2, then you'll get an error
  135. message when you try to use the `-P/--pcre2` flag:
  136. ```
  137. $ rg -P '(\w{10})\1'
  138. PCRE2 is not available in this build of ripgrep
  139. ```
  140. Most of the releases distributed by the ripgrep project here on GitHub will
  141. come bundled with PCRE2 enabled. If you installed ripgrep through a different
  142. means (like your system's package manager), then please reach out to the
  143. maintainer of that package to see whether it's possible to enable the PCRE2
  144. feature.
  145. <h3 name="colors">
  146. How do I configure ripgrep's colors?
  147. </h3>
  148. ripgrep has two flags related to colors:
  149. * `--color` controls *when* to use colors.
  150. * `--colors` controls *which* colors to use.
  151. The `--color` flag accepts one of the following possible values: `never`,
  152. `auto`, `always` or `ansi`. The `auto` value is the default and will cause
  153. ripgrep to only enable colors when it is printing to a terminal. But if you
  154. pipe ripgrep to a file or some other process, then it will suppress colors.
  155. The --colors` flag is a bit more complicated. The general format is:
  156. ```
  157. --colors '{type}:{attribute}:{value}'
  158. ```
  159. * `{type}` should be one of `path`, `line`, `column` or `match`. Each of these
  160. correspond to the four different types of things that ripgrep will add color
  161. to in its output. Select the type whose color you want to change.
  162. * `{attribute}` should be one of `fg`, `bg` or `style`, corresponding to
  163. foreground color, background color, or miscellaneous styling (such as whether
  164. to bold the output or not).
  165. * `{value}` is determined by the value of `{attribute}`. If
  166. `{attribute}` is `style`, then `{value}` should be one of `nobold`,
  167. `bold`, `nointense`, `intense`, `nounderline` or `underline`. If
  168. `{attribute}` is `fg` or `bg`, then `{value}` should be a color.
  169. A color is specified by either one of eight of English names, a single 256-bit
  170. number or an RGB triple (with over 16 million possible values, or "true
  171. color").
  172. The color names are `red`, `blue`, `green`, `cyan`, `magenta`, `yellow`,
  173. `white` or `black`.
  174. A single 256-bit number is a value in the range 0-255 (inclusive). It can
  175. either be in decimal format (e.g., `62`) or hexadecimal format (e.g., `0x3E`).
  176. An RGB triple corresponds to three numbers (decimal or hexadecimal) separated
  177. by commas.
  178. As a special case, `--colors '{type}:none'` will clear all colors and styles
  179. associated with `{type}`, which lets you start with a clean slate (instead of
  180. building on top of ripgrep's default color settings).
  181. Here's an example that makes highlights the matches with a nice blue background
  182. with bolded white text:
  183. ```
  184. $ rg somepattern \
  185. --colors 'match:none' \
  186. --colors 'match:bg:0x33,0x66,0xFF' \
  187. --colors 'match:fg:white' \
  188. --colors 'match:style:bold'
  189. ```
  190. Colors are an ideal candidate to set in your
  191. [configuration file](GUIDE.md#configuration-file). See the
  192. [question on emulating The Silver Searcher's output style](#silver-searcher-output)
  193. for an example specific to colors.
  194. <h3 name="truecolors-windows">
  195. How do I enable true colors on Windows?
  196. </h3>
  197. First, see the previous question's
  198. [answer on configuring colors](#colors).
  199. Secondly, coloring on Windows is a bit complicated. If you're using a terminal
  200. like Cygwin, then it's likely true color support already works out of the box.
  201. However, if you are using a normal Windows console (`cmd` or `PowerShell`) and
  202. a version of Windows prior to 10, then there is no known way to get true
  203. color support. If you are on Windows 10 and using a Windows console, then
  204. true colors should work out of the box with one caveat: you might need to
  205. clear ripgrep's default color settings first. That is, instead of this:
  206. ```
  207. $ rg somepattern --colors 'match:fg:0x33,0x66,0xFF'
  208. ```
  209. you should do this
  210. ```
  211. $ rg somepattern --colors 'match:none' --colors 'match:fg:0x33,0x66,0xFF'
  212. ```
  213. This is because ripgrep might set the default style for `match` to `bold`, and
  214. it seems like Windows 10's VT100 support doesn't permit bold and true color
  215. ANSI escapes to be used simultaneously. The work-around above will clear
  216. ripgrep's default styling, allowing you to craft it exactly as desired.
  217. <h3 name="stop-ripgrep">
  218. How do I stop ripgrep from messing up colors when I kill it?
  219. </h3>
  220. Type in `color` in cmd.exe (Command Prompt) and `echo -ne "\033[0m"` on
  221. Unix-like systems to restore your original foreground color.
  222. In PowerShell, you can add the following code to your profile which will
  223. restore the original foreground color when `Reset-ForegroundColor` is called.
  224. Including the `Set-Alias` line will allow you to call it with simply `color`.
  225. ```powershell
  226. $OrigFgColor = $Host.UI.RawUI.ForegroundColor
  227. function Reset-ForegroundColor {
  228. $Host.UI.RawUI.ForegroundColor = $OrigFgColor
  229. }
  230. Set-Alias -Name color -Value Reset-ForegroundColor
  231. ```
  232. PR [#187](https://github.com/BurntSushi/ripgrep/pull/187) fixed this, and it
  233. was later deprecated in
  234. [#281](https://github.com/BurntSushi/ripgrep/issues/281). A full explanation is
  235. available
  236. [here](https://github.com/BurntSushi/ripgrep/issues/281#issuecomment-269093893).
  237. <h3 name="size-limit">
  238. How do I get around the regex size limit?
  239. </h3>
  240. If you've given ripgrep a particularly large pattern (or a large number of
  241. smaller patterns), then it is possible that it will fail to compile because it
  242. hit a pre-set limit. For example:
  243. ```
  244. $ rg '\pL{1000}'
  245. Compiled regex exceeds size limit of 10485760 bytes.
  246. ```
  247. (Note: `\pL{1000}` may look small, but `\pL` is the character class containing
  248. all Unicode letters, which is quite large. *And* it's repeated 1000 times.)
  249. In this case, you can work around by simply increasing the limit:
  250. ```
  251. $ rg '\pL{1000}' --regex-size-limit 1G
  252. ```
  253. Increasing the limit to 1GB does not necessarily mean that ripgrep will use
  254. that much memory. The limit just says that it's allowed to (approximately) use
  255. that much memory for constructing the regular expression.
  256. <h3 name="dfa-size">
  257. How do I make the <code>-f/--file</code> flag faster?
  258. </h3>
  259. The `-f/--file` permits one to give a file to ripgrep which contains a pattern
  260. on each line. ripgrep will then report any line that matches any of the
  261. patterns.
  262. If this pattern file gets too big, then it is possible ripgrep will slow down
  263. dramatically. *Typically* this is because an internal cache is too small, and
  264. will cause ripgrep to spill over to a slower but more robust regular expression
  265. engine. If this is indeed the problem, then it is possible to increase this
  266. cache and regain speed. The cache can be controlled via the `--dfa-size-limit`
  267. flag. For example, using `--dfa-size-limit 1G` will set the cache size to 1GB.
  268. (Note that this doesn't mean ripgrep will use 1GB of memory automatically, but
  269. it will allow the regex engine to if it needs to.)
  270. <h3 name="silver-searcher-output">
  271. How do I make the output look like The Silver Searcher's output?
  272. </h3>
  273. Use the `--colors` flag, like so:
  274. ```
  275. rg --colors line:fg:yellow \
  276. --colors line:style:bold \
  277. --colors path:fg:green \
  278. --colors path:style:bold \
  279. --colors match:fg:black \
  280. --colors match:bg:yellow \
  281. --colors match:style:nobold \
  282. foo
  283. ```
  284. Alternatively, add your color configuration to your ripgrep config file (which
  285. is activated by setting the `RIPGREP_CONFIG_PATH` environment variable to point
  286. to your config file). For example:
  287. ```
  288. $ cat $HOME/.config/ripgrep/rc
  289. --colors=line:fg:yellow
  290. --colors=line:style:bold
  291. --colors=path:fg:green
  292. --colors=path:style:bold
  293. --colors=match:fg:black
  294. --colors=match:bg:yellow
  295. --colors=match:style:nobold
  296. $ RIPGREP_CONFIG_PATH=$HOME/.config/ripgrep/rc rg foo
  297. ```
  298. <h3 name="pcre2-slow">
  299. Why does ripgrep get slower when I enable PCRE2 regexes?
  300. </h3>
  301. When you use the `--pcre2` (`-P` for short) flag, ripgrep will use the PCRE2
  302. regex engine instead of the default. Both regex engines are quite fast,
  303. but PCRE2 provides a number of additional features such as look-around and
  304. backreferences that many enjoy using. This is largely because PCRE2 uses
  305. a backtracking implementation where as the default regex engine uses a finite
  306. automaton based implementation. The former provides the ability to add lots of
  307. bells and whistles over the latter, but the latter executes with worst case
  308. linear time complexity.
  309. With that out of the way, if you've used `-P` with ripgrep, you may have
  310. noticed that it can be slower. The reasons for why this is are quite complex,
  311. and they are complex because the optimizations that ripgrep uses to implement
  312. fast search are complex.
  313. The task ripgrep has before it is somewhat simple; all it needs to do is search
  314. a file for occurrences of some pattern and then print the lines containing
  315. those occurrences. The problem lies in what is considered a valid match and how
  316. exactly we read the bytes from a file.
  317. In terms of what is considered a valid match, remember that ripgrep will only
  318. report matches spanning a single line by default. The problem here is that
  319. some patterns can match across multiple lines, and ripgrep needs to prevent
  320. that from happening. For example, `foo\sbar` will match `foo\nbar`. The most
  321. obvious way to achieve this is to read the data from a file, and then apply
  322. the pattern search to that data for each line. The problem with this approach
  323. is that it can be quite slow; it would be much faster to let the pattern
  324. search across as much data as possible. It's faster because it gets rid of the
  325. overhead of finding the boundaries of every line, and also because it gets rid
  326. of the overhead of starting and stopping the pattern search for every single
  327. line. (This is operating under the general assumption that matching lines are
  328. much rarer than non-matching lines.)
  329. It turns out that we can use the faster approach by applying a very simple
  330. restriction to the pattern: *statically prevent* the pattern from matching
  331. through a `\n` character. Namely, when given a pattern like `foo\sbar`,
  332. ripgrep will remove `\n` from the `\s` character class automatically. In some
  333. cases, a simple removal is not so easy. For example, ripgrep will return an
  334. error when your pattern includes a `\n` literal:
  335. ```
  336. $ rg '\n'
  337. the literal '"\n"' is not allowed in a regex
  338. ```
  339. So what does this have to do with PCRE2? Well, ripgrep's default regex engine
  340. exposes APIs for doing syntactic analysis on the pattern in a way that makes
  341. it quite easy to strip `\n` from the pattern (or otherwise detect it and report
  342. an error if stripping isn't possible). PCRE2 seemingly does not provide a
  343. similar API, so ripgrep does not do any stripping when PCRE2 is enabled. This
  344. forces ripgrep to use the "slow" search strategy of searching each line
  345. individually.
  346. OK, so if enabling PCRE2 slows down the default method of searching because it
  347. forces matches to be limited to a single line, then why is PCRE2 also sometimes
  348. slower when performing multiline searches? Well, that's because there are
  349. *multiple* reasons why using PCRE2 in ripgrep can be slower than the default
  350. regex engine. This time, blame PCRE2's Unicode support, which ripgrep enables
  351. by default. In particular, PCRE2 cannot simultaneously enable Unicode support
  352. and search arbitrary data. That is, when PCRE2's Unicode support is enabled,
  353. the data **must** be valid UTF-8 (to do otherwise is to invoke undefined
  354. behavior). This is in contrast to ripgrep's default regex engine, which can
  355. enable Unicode support and still search arbitrary data. ripgrep's default
  356. regex engine simply won't match invalid UTF-8 for a pattern that can otherwise
  357. only match valid UTF-8. Why doesn't PCRE2 do the same? This author isn't
  358. familiar with its internals, so we can't comment on it here.
  359. The bottom line here is that we can't enable PCRE2's Unicode support without
  360. simultaneously incurring a performance penalty for ensuring that we are
  361. searching valid UTF-8. In particular, ripgrep will transcode the contents
  362. of each file to UTF-8 while replacing invalid UTF-8 data with the Unicode
  363. replacement codepoint. ripgrep then disables PCRE2's own internal UTF-8
  364. checking, since we've guaranteed the data we hand it will be valid UTF-8. The
  365. reason why ripgrep takes this approach is because if we do hand PCRE2 invalid
  366. UTF-8, then it will report a match error if it comes across an invalid UTF-8
  367. sequence. This is not good news for ripgrep, since it will stop it from
  368. searching the rest of the file, and will also print potentially undesirable
  369. error messages to users.
  370. All right, the above is a lot of information to swallow if you aren't already
  371. familiar with ripgrep internals. Let's make this concrete with some examples.
  372. First, let's get some data big enough to magnify the performance differences:
  373. ```
  374. $ curl -O 'https://burntsushi.net/stuff/subtitles2016-sample.gz'
  375. $ gzip -d subtitles2016-sample
  376. $ md5sum subtitles2016-sample
  377. e3cb796a20bbc602fbfd6bb43bda45f5 subtitles2016-sample
  378. ```
  379. To search this data, we will use the pattern `^\w{42}$`, which contains exactly
  380. one hit in the file and has no literals. Having no literals is important,
  381. because it ensures that the regex engine won't use literal optimizations to
  382. speed up the search. In other words, it lets us reason coherently about the
  383. actual task that the regex engine is performing.
  384. Let's now walk through a few examples in light of the information above. First,
  385. let's consider the default search using ripgrep's default regex engine and
  386. then the same search with PCRE2:
  387. ```
  388. $ time rg '^\w{42}$' subtitles2016-sample
  389. 21225780:EverymajordevelopmentinthehistoryofAmerica
  390. real 0m1.783s
  391. user 0m1.731s
  392. sys 0m0.051s
  393. $ time rg -P '^\w{42}$' subtitles2016-sample
  394. 21225780:EverymajordevelopmentinthehistoryofAmerica
  395. real 0m2.458s
  396. user 0m2.419s
  397. sys 0m0.038s
  398. ```
  399. In this particular example, both pattern searches are using a Unicode aware
  400. `\w` character class and both are counting lines in order to report line
  401. numbers. The key difference here is that the first search will not search
  402. line by line, but the second one will. We can observe which strategy ripgrep
  403. uses by passing the `--trace` flag:
  404. ```
  405. $ rg '^\w{42}$' subtitles2016-sample --trace
  406. [... snip ...]
  407. TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:622: Some("subtitles2016-sample"): searching via memory map
  408. TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:712: slice reader: searching via slice-by-line strategy
  409. TRACE|grep_searcher::searcher::core|grep-searcher/src/searcher/core.rs:61: searcher core: will use fast line searcher
  410. [... snip ...]
  411. $ rg -P '^\w{42}$' subtitles2016-sample --trace
  412. [... snip ...]
  413. TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:622: Some("subtitles2016-sample"): searching via memory map
  414. TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:705: slice reader: needs transcoding, using generic reader
  415. TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:685: generic reader: searching via roll buffer strategy
  416. TRACE|grep_searcher::searcher::core|grep-searcher/src/searcher/core.rs:63: searcher core: will use slow line searcher
  417. [... snip ...]
  418. ```
  419. The first says it is using the "fast line searcher" where as the latter says
  420. it is using the "slow line searcher." The latter also shows that we are
  421. decoding the contents of the file, which also impacts performance.
  422. Interestingly, in this case, the pattern does not match a `\n` and the file
  423. we're searching is valid UTF-8, so neither the slow line-by-line search
  424. strategy nor the decoding are necessary. We could fix the former issue with
  425. better PCRE2 introspection APIs. We can actually fix the latter issue with
  426. ripgrep's `--no-encoding` flag, which prevents the automatic UTF-8 decoding,
  427. but will enable PCRE2's own UTF-8 validity checking. Unfortunately, it's slower
  428. in my build of ripgrep:
  429. ```
  430. $ time rg -P '^\w{42}$' subtitles2016-sample --no-encoding
  431. 21225780:EverymajordevelopmentinthehistoryofAmerica
  432. real 0m3.074s
  433. user 0m3.021s
  434. sys 0m0.051s
  435. ```
  436. (Tip: use the `--trace` flag to verify that no decoding in ripgrep is
  437. happening.)
  438. A possible reason why PCRE2's UTF-8 checking is slower is because it might
  439. not be better than the highly optimized UTF-8 checking routines found in the
  440. [`encoding_rs`](https://github.com/hsivonen/encoding_rs) library, which is what
  441. ripgrep uses for UTF-8 decoding. Moreover, my build of ripgrep enables
  442. `encoding_rs`'s SIMD optimizations, which may be in play here.
  443. Also, note that using the `--no-encoding` flag can cause PCRE2 to report
  444. invalid UTF-8 errors, which causes ripgrep to stop searching the file:
  445. ```
  446. $ cat invalid-utf8
  447. foobar
  448. $ xxd invalid-utf8
  449. 00000000: 666f 6fff 6261 720a foo.bar.
  450. $ rg foo invalid-utf8
  451. 1:foobar
  452. $ rg -P foo invalid-utf8
  453. 1:foobar
  454. $ rg -P foo invalid-utf8 --no-encoding
  455. invalid-utf8: PCRE2: error matching: UTF-8 error: illegal byte (0xfe or 0xff)
  456. ```
  457. All right, so at this point, you might think that we could remove the penalty
  458. for line-by-line searching by enabling multiline search. After all, our
  459. particular pattern can't match across multiple lines anyway, so we'll still get
  460. the results we want. Let's try it:
  461. ```
  462. $ time rg -U '^\w{42}$' subtitles2016-sample
  463. 21225780:EverymajordevelopmentinthehistoryofAmerica
  464. real 0m1.803s
  465. user 0m1.748s
  466. sys 0m0.054s
  467. $ time rg -P -U '^\w{42}$' subtitles2016-sample
  468. 21225780:EverymajordevelopmentinthehistoryofAmerica
  469. real 0m2.962s
  470. user 0m2.246s
  471. sys 0m0.713s
  472. ```
  473. Search times remain the same with the default regex engine, but the PCRE2
  474. search gets _slower_. What happened? The secrets can be revealed with the
  475. `--trace` flag once again. In the former case, ripgrep actually detects that
  476. the pattern can't match across multiple lines, and so will fall back to the
  477. "fast line search" strategy as with our search without `-U`.
  478. However, for PCRE2, things are much worse. Namely, since Unicode mode is still
  479. enabled, ripgrep is still going to decode UTF-8 to ensure that it hands only
  480. valid UTF-8 to PCRE2. Unfortunately, one key downside of multiline search is
  481. that ripgrep cannot do it incrementally. Since matches can be arbitrarily long,
  482. ripgrep actually needs the entire file in memory at once. Normally, we can use
  483. a memory map for this, but because we need to UTF-8 decode the file before
  484. searching it, ripgrep winds up reading the entire contents of the file on to
  485. the heap before executing a search. Owch.
  486. OK, so Unicode is killing us here. The file we're searching is _mostly_ ASCII,
  487. so maybe we're OK with missing some data. (Try `rg '[\w--\p{ascii}]'` to see
  488. non-ASCII word characters that an ASCII-only `\w` character class would miss.)
  489. We can disable Unicode in both searches, but this is done differently depending
  490. on the regex engine we use:
  491. ```
  492. $ time rg '(?-u)^\w{42}$' subtitles2016-sample
  493. 21225780:EverymajordevelopmentinthehistoryofAmerica
  494. real 0m1.714s
  495. user 0m1.669s
  496. sys 0m0.044s
  497. $ time rg -P '^\w{42}$' subtitles2016-sample --no-pcre2-unicode
  498. 21225780:EverymajordevelopmentinthehistoryofAmerica
  499. real 0m1.997s
  500. user 0m1.958s
  501. sys 0m0.037s
  502. ```
  503. For the most part, ripgrep's default regex engine performs about the same.
  504. PCRE2 does improve a little bit, and is now almost as fast as the default
  505. regex engine. If you look at the output of `--trace`, you'll see that ripgrep
  506. will no longer perform UTF-8 decoding, but it does still use the slow
  507. line-by-line searcher.
  508. At this point, we can combine all of our insights above: let's try to get off
  509. of the slow line-by-line searcher by enabling multiline mode, and let's stop
  510. UTF-8 decoding by disabling Unicode support:
  511. ```
  512. $ time rg -U '(?-u)^\w{42}$' subtitles2016-sample
  513. 21225780:EverymajordevelopmentinthehistoryofAmerica
  514. real 0m1.714s
  515. user 0m1.655s
  516. sys 0m0.058s
  517. $ time rg -P -U '^\w{42}$' subtitles2016-sample --no-pcre2-unicode
  518. 21225780:EverymajordevelopmentinthehistoryofAmerica
  519. real 0m1.121s
  520. user 0m1.071s
  521. sys 0m0.048s
  522. ```
  523. Ah, there's PCRE2's JIT shining! ripgrep's default regex engine once again
  524. remains about the same, but PCRE2 no longer needs to search line-by-line and it
  525. no longer needs to do any kind of UTF-8 checks. This allows the file to get
  526. memory mapped and passed right through PCRE2's JIT at impressive speeds. (As
  527. a brief and interesting historical note, the configuration of "memory map +
  528. multiline + no-Unicode" is exactly the configuration used by The Silver
  529. Searcher. This analysis perhaps sheds some reasoning as to why that
  530. configuration is useful!)
  531. In summary, if you want PCRE2 to go as fast as possible and you don't care
  532. about Unicode and you don't care about matches possibly spanning across
  533. multiple lines, then enable multiline mode with `-U` and disable PCRE2's
  534. Unicode support with the `--no-pcre2-unicode` flag.
  535. Caveat emptor: This author is not a PCRE2 expert, so there may be APIs that can
  536. improve performance that the author missed. Similarly, there may be alternative
  537. designs for a searching tool that are more amenable to how PCRE2 works.
  538. <h3 name="rg-other-cmd">
  539. When I run <code>rg</code>, why does it execute some other command?
  540. </h3>
  541. It's likely that you have a shell alias or even another tool called `rg` which
  542. is interfering with ripgrep. Run `which rg` to see what it is.
  543. (Notably, the Rails plug-in for
  544. [Oh My Zsh](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#rails) sets
  545. up an `rg` alias for `rails generate`.)
  546. Problems like this can be resolved in one of several ways:
  547. * If you're using the OMZ Rails plug-in, disable it by editing the `plugins`
  548. array in your zsh configuration.
  549. * Temporarily bypass an existing `rg` alias by calling ripgrep as
  550. `command rg`, `\rg`, or `'rg'`.
  551. * Temporarily bypass an existing alias or another tool named `rg` by calling
  552. ripgrep by its full path (e.g., `/usr/bin/rg` or `/usr/local/bin/rg`).
  553. * Permanently disable an existing `rg` alias by adding `unalias rg` to the
  554. bottom of your shell configuration file (e.g., `.bash_profile` or `.zshrc`).
  555. * Give ripgrep its own alias that doesn't conflict with other tools/aliases by
  556. adding a line like the following to the bottom of your shell configuration
  557. file: `alias ripgrep='command rg'`.
  558. <h3 name="rg-alias-windows">
  559. How do I create an alias for ripgrep on Windows?
  560. </h3>
  561. Often you can find a need to make alias for commands you use a lot that set
  562. certain flags. But PowerShell function aliases do not behave like your typical
  563. linux shell alias. You always need to propagate arguments and `stdin` input.
  564. But it cannot be done simply as
  565. `function grep() { $input | rg.exe --hidden $args }`
  566. Use below example as reference to how setup alias in PowerShell.
  567. ```powershell
  568. function grep {
  569. $count = @($input).Count
  570. $input.Reset()
  571. if ($count) {
  572. $input | rg.exe --hidden $args
  573. }
  574. else {
  575. rg.exe --hidden $args
  576. }
  577. }
  578. ```
  579. PowerShell special variables:
  580. * input - is powershell `stdin` object that allows you to access its content.
  581. * args - is array of arguments passed to this function.
  582. This alias checks whether there is `stdin` input and propagates only if there
  583. is some lines. Otherwise empty `$input` will make powershell to trigger `rg` to
  584. search empty `stdin`.
  585. <h3 name="powershell-profile">
  586. How do I create a PowerShell profile?
  587. </h3>
  588. To customize powershell on start-up, there is a special PowerShell script that
  589. has to be created. In order to find its location, type `$profile`.
  590. See
  591. [Microsoft's documentation](https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx)
  592. for more details.
  593. Any PowerShell code in this file gets evaluated at the start of console. This
  594. way you can have own aliases to be created at start.
  595. <h3 name="pipe-non-ascii-windows">
  596. How do I pipe non-ASCII content to ripgrep on Windows?
  597. </h3>
  598. When piping input into native executables in PowerShell, the encoding of the
  599. input is controlled by the `$OutputEncoding` variable. By default, this is set
  600. to US-ASCII, and any characters in the pipeline that don't have encodings in
  601. US-ASCII are converted to `?` (question mark) characters.
  602. To change this setting, set `$OutputEncoding` to a different encoding, as
  603. represented by a .NET encoding object. Some common examples are below. The
  604. value of this variable is reset when PowerShell restarts, so to make this
  605. change take effect every time PowerShell is started add a line setting the
  606. variable into your PowerShell profile.
  607. Example `$OutputEncoding` settings:
  608. * UTF-8 without BOM: `$OutputEncoding = [System.Text.UTF8Encoding]::new()`
  609. * The console's output encoding:
  610. `$OutputEncoding = [System.Console]::OutputEncoding`
  611. If you continue to have encoding problems, you can also force the encoding
  612. that the console will use for printing to UTF-8 with
  613. `[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8`. This
  614. will also reset when PowerShell is restarted, so you can add that line
  615. to your profile as well if you want to make the setting permanent.
  616. <h3 name="search-and-replace">
  617. How can I search and replace with ripgrep?
  618. </h3>
  619. Using ripgrep alone, you can't. ripgrep is a search tool that will never
  620. touch your files. However, the output of ripgrep can be piped to other tools
  621. that do modify files on disk. See
  622. [this issue](https://github.com/BurntSushi/ripgrep/issues/74) for more
  623. information.
  624. sed is one such tool that can modify files on disk. sed can take a filename
  625. and a substitution command to search and replace in the specified file.
  626. Files containing matching patterns can be provided to sed using
  627. ```
  628. rg foo --files-with-matches
  629. ```
  630. The output of this command is a list of filenames that contain a match for
  631. the `foo` pattern.
  632. This list can be piped into `xargs`, which will split the filenames from
  633. standard input into arguments for the command following xargs. You can use this
  634. combination to pipe a list of filenames into sed for replacement. For example:
  635. ```
  636. rg foo --files-with-matches | xargs sed -i 's/foo/bar/g'
  637. ```
  638. will replace all instances of 'foo' with 'bar' in the files in which
  639. ripgrep finds the foo pattern. The `-i` flag to sed indicates that you are
  640. editing files in place, and `s/foo/bar/g` says that you are performing a
  641. **s**ubstitution of the pattren `foo` for `bar`, and that you are doing this
  642. substitution **g**lobally (all occurrences of the pattern in each file).
  643. Note: the above command assumes that you are using GNU sed. If you are using
  644. BSD sed (the default on macOS and FreeBSD) then you must modify the above
  645. command to be the following:
  646. ```
  647. rg foo --files-with-matches | xargs sed -i '' 's/foo/bar/g'
  648. ```
  649. The `-i` flag in BSD sed requires a file extension to be given to make backups
  650. for all modified files. Specifying the empty string prevents file backups from
  651. being made.
  652. Finally, if any of your file paths contain whitespace in them, then you might
  653. need to delimit your file paths with a NUL terminator. This requires telling
  654. ripgrep to output NUL bytes between each path, and telling xargs to read paths
  655. delimited by NUL bytes:
  656. ```
  657. rg foo --files-with-matches -0 | xargs -0 sed -i 's/foo/bar/g'
  658. ```
  659. To learn more about sed, see the sed manual
  660. [here](https://www.gnu.org/software/sed/manual/sed.html).
  661. Additionally, Facebook has a tool called
  662. [fastmod](https://github.com/facebookincubator/fastmod)
  663. that uses some of the same libraries as ripgrep and might provide a more
  664. ergonomic search-and-replace experience.
  665. <h3 name="license">
  666. How is ripgrep licensed?
  667. </h3>
  668. ripgrep is dual licensed under the
  669. [Unlicense](https://unlicense.org/)
  670. and MIT licenses. Specifically, you may use ripgrep under the terms of either
  671. license.
  672. The reason why ripgrep is dual licensed this way is two-fold:
  673. 1. I, as ripgrep's author, would like to participate in a small bit of
  674. ideological activism by promoting the Unlicense's goal: to disclaim
  675. copyright monopoly interest.
  676. 2. I, as ripgrep's author, would like as many people to use rigprep as
  677. possible. Since the Unlicense is not a proven or well known license, ripgrep
  678. is also offered under the MIT license, which is ubiquitous and accepted by
  679. almost everyone.
  680. More specifically, ripgrep and all its dependencies are compatible with this
  681. licensing choice. In particular, ripgrep's dependencies (direct and transitive)
  682. will always be limited to permissive licenses. That is, ripgrep will never
  683. depend on code that is not permissively licensed. This means rejecting any
  684. dependency that uses a copyleft license such as the GPL, LGPL, MPL or any of
  685. the Creative Commons ShareAlike licenses. Whether the license is "weak"
  686. copyleft or not does not matter; ripgrep will **not** depend on it.
  687. <h3 name="posix4ever">
  688. Can ripgrep replace grep?
  689. </h3>
  690. Yes and no.
  691. If, upon hearing that "ripgrep can replace grep," you *actually* hear, "ripgrep
  692. can be used in every instance grep can be used, in exactly the same way, for
  693. the same use cases, with exactly the same bug-for-bug behavior," then no,
  694. ripgrep trivially *cannot* replace grep. Moreover, ripgrep will *never* replace
  695. grep.
  696. If, upon hearing that "ripgrep can replace grep," you *actually* hear, "ripgrep
  697. can replace grep in some cases and not in other use cases," then yes, that is
  698. indeed true!
  699. Let's go over some of those use cases in favor of ripgrep. Some of these may
  700. not apply to you. That's OK. There may be other use cases not listed here that
  701. do apply to you. That's OK too.
  702. (For all claims related to performance in the following words, see my
  703. [blog post](https://blog.burntsushi.net/ripgrep/)
  704. introducing ripgrep.)
  705. * Are you frequently searching a repository of code? If so, ripgrep might be a
  706. good choice since there's likely a good chunk of your repository that you
  707. don't want to search. grep, can, of course, be made to filter files using
  708. recursive search, and if you don't mind writing out the requisite `--exclude`
  709. rules or writing wrapper scripts, then grep might be sufficient. (I'm not
  710. kidding, I myself did this with grep for almost a decade before writing
  711. ripgrep.) But if you instead enjoy having a search tool respect your
  712. `.gitignore`, then ripgrep might be perfect for you!
  713. * Are you frequently searching non-ASCII text that is UTF-8 encoded? One of
  714. ripgrep's key features is that it can handle Unicode features in your
  715. patterns in a way that tends to be faster than GNU grep. Unicode features
  716. in ripgrep are enabled by default; there is no need to configure your locale
  717. settings to use ripgrep properly because ripgrep doesn't respect your locale
  718. settings.
  719. * Do you need to search UTF-16 files and you don't want to bother explicitly
  720. transcoding them? Great. ripgrep does this for you automatically. No need
  721. to enable it.
  722. * Do you need to search a large directory of large files? ripgrep uses
  723. parallelism by default, which tends to make it faster than a standard
  724. `grep -r` search. However, if you're OK writing the occasional
  725. `find ./ -print0 | xargs -P8 -0 grep` command, then maybe grep is good
  726. enough.
  727. Here are some cases where you might *not* want to use ripgrep. The same caveats
  728. for the previous section apply.
  729. * Are you writing portable shell scripts intended to work in a variety of
  730. environments? Great, probably not a good idea to use ripgrep! ripgrep has
  731. nowhere near the ubiquity of grep, so if you do use ripgrep, you might need
  732. to futz with the installation process more than you would with grep.
  733. * Do you care about POSIX compatibility? If so, then you can't use ripgrep
  734. because it never was, isn't and never will be POSIX compatible.
  735. * Do you hate tools that try to do something smart? If so, ripgrep is all about
  736. being smart, so you might prefer to just stick with grep.
  737. * Is there a particular feature of grep you rely on that ripgrep either doesn't
  738. have or never will have? If the former, file a bug report, maybe ripgrep can
  739. do it! If the latter, well, then, just use grep.
  740. <h3 name="intentcountsforsomething">
  741. What does the "rip" in ripgrep mean?
  742. </h3>
  743. When I first started writing ripgrep, I called it `rep`, intending it to be a
  744. shorter variant of `grep`. Soon after, I renamed it to `xrep` since `rep`
  745. wasn't obvious enough of a name for my taste. And also because adding `x` to
  746. anything always makes it better, right?
  747. Before ripgrep's first public release, I decided that I didn't like `xrep`. I
  748. thought it was slightly awkward to type, and despite my previous praise of the
  749. letter `x`, I kind of thought it was pretty lame. Being someone who really
  750. likes Rust, I wanted to call it "rustgrep" or maybe "rgrep" for short. But I
  751. thought that was just as lame, and maybe a little too in-your-face. But I
  752. wanted to continue using `r` so I could at least pretend Rust had something to
  753. do with it.
  754. I spent a couple of days trying to think of very short words that began with
  755. the letter `r` that were even somewhat related to the task of searching. I
  756. don't remember how it popped into my head, but "rip" came up as something that
  757. meant "fast," as in, "to rip through your text." The fact that RIP is also
  758. an initialism for "Rest in Peace" (as in, "ripgrep kills grep") never really
  759. dawned on me. Perhaps the coincidence is too striking to believe that, but
  760. I didn't realize it until someone explicitly pointed it out to me after the
  761. initial public release. I admit that I found it mildly amusing, but if I had
  762. realized it myself before the public release, I probably would have pressed on
  763. and chose a different name. Alas, renaming things after a release is hard, so I
  764. decided to mush on.
  765. Given the fact that
  766. [ripgrep never was, is or will be a 100% drop-in replacement for
  767. grep](#posix4ever),
  768. ripgrep is neither actually a "grep killer" nor was it ever intended to be. It
  769. certainly does eat into some of its use cases, but that's nothing that other
  770. tools like ack or The Silver Searcher weren't already doing.
  771. <h3 name="donations">
  772. How can I donate to ripgrep or its maintainers?
  773. </h3>
  774. As of now, you can't. While I believe the various efforts that are being
  775. undertaken to help fund FOSS are extremely important, they aren't a good fit
  776. for me. ripgrep is and I hope will remain a project of love that I develop in
  777. my free time. As such, involving money---even in the form of donations given
  778. without expectations---would severely change that dynamic for me personally.
  779. Instead, I'd recommend donating to something else that is doing work that you
  780. find meaningful. If you would like suggestions, then my favorites are:
  781. * [The Internet Archive](https://archive.org/donate/)
  782. * [Rails Girls](https://railsgirlssummerofcode.org/campaign/)
  783. * [Wikipedia](https://wikimediafoundation.org/support/)