FAQ.md
This file
Path
FAQ.md
1,063 lines · 41.3 KiB · Markdown
Last changed by
colors: add `highlight` type support for matching linesemrebenguebefore adoption99fe884
History of this fileTake it
1## FAQ23* [Does ripgrep support configuration files?](#config)4* [What's changed in ripgrep recently?](#changelog)5* [When is the next release?](#release)6* [Does ripgrep have a man page?](#manpage)7* [Does ripgrep have support for shell auto-completion?](#complete)8* [How can I get results in a consistent order?](#order)9* [How do I search files that aren't UTF-8?](#encoding)10* [How do I search compressed files?](#compressed)11* [How do I search over multiple lines?](#multiline)12* [How do I use lookaround and/or backreferences?](#fancy)13* [How do I configure ripgrep's colors?](#colors)14* [How do I enable true colors on Windows?](#truecolors-windows)15* [How do I stop ripgrep from messing up colors when I kill it?](#stop-ripgrep)16* [Why does using a leading `/` on Windows fail?](#because-cygwin)17* [How do I get around the regex size limit?](#size-limit)18* [How do I make the `-f/--file` flag faster?](#dfa-size)19* [How do I make the output look like The Silver Searcher's output?](#silver-searcher-output)20* [Why does ripgrep get slower when I enabled PCRE2 regexes?](#pcre2-slow)21* [When I run `rg`, why does it execute some other command?](#rg-other-cmd)22* [How do I create an alias for ripgrep on Windows?](#rg-alias-windows)23* [How do I create a PowerShell profile?](#powershell-profile)24* [How do I pipe non-ASCII content to ripgrep on Windows?](#pipe-non-ascii-windows)25* [How can I search and replace with ripgrep?](#search-and-replace)26* [How is ripgrep licensed?](#license)27* [Can ripgrep replace grep?](#posix4ever)28* [What does the "rip" in ripgrep mean?](#intentcountsforsomething)29* [How can I donate to ripgrep or its maintainers?](#donations)303132<h3 name="config">33Does ripgrep support configuration files?34</h3>3536Yes. See the37[guide's section on configuration files](GUIDE.md#configuration-file).383940<h3 name="changelog">41What's changed in ripgrep recently?42</h3>4344Please consult ripgrep's [CHANGELOG](CHANGELOG.md).454647<h3 name="release">48When is the next release?49</h3>5051ripgrep is a project whose contributors are volunteers. A release schedule52adds undue stress to said volunteers. Therefore, releases are made on a best53effort basis and no dates **will ever be given**.5455An exception to this _can be_ high impact bugs. If a ripgrep release contains56a significant regression, then there will generally be a strong push to get a57patch release out with a fix. However, no promises are made.585960<h3 name="manpage">61Does ripgrep have a man page?62</h3>6364Yes. If you installed ripgrep through a package manager on a Unix system, then65it would have ideally been installed for you in the proper location. In which66case, `man rg` should just work.6768Otherwise, you can ask ripgrep to generate the man page:6970```71$ mkdir -p man/man172$ rg --generate man > man/man1/rg.173$ MANPATH="$PWD/man" man rg74```7576Or, if your version of `man` supports the `-l/--local-file` flag, then this77will suffice:7879```80$ rg --generate man | man -l -81```8283Note that the man page's documentation for options is equivalent to the output84shown in `rg --help`. To see more condensed documentation (one line per flag),85run `rg -h`.8687The man page is also included in all88[ripgrep binary releases](https://github.com/BurntSushi/ripgrep/releases).899091<h3 name="complete">92Does ripgrep have support for shell auto-completion?93</h3>9495Yes! If you installed ripgrep through a package manager on a Unix system, then96the shell completion files included in the release archive should have been97installed for you automatically. If not, you can generate completions using98ripgrep's command line interface.99100For **bash**:101102```103$ dir="$XDG_CONFIG_HOME/bash_completion"104$ mkdir -p "$dir"105$ rg --generate complete-bash > "$dir/rg.bash"106```107108For **fish**:109110```111$ dir="$XDG_CONFIG_HOME/fish/completions"112$ mkdir -p "$dir"113$ rg --generate complete-fish > "$dir/rg.fish"114```115116For **zsh**, the recommended approach is:117118```zsh119$ dir="$HOME/.zsh-complete"120$ mkdir -p "$dir"121$ rg --generate complete-zsh > "$dir/_rg"122```123124And then add `$HOME/.zsh-complete` to your `fpath` in, e.g., your125`$HOME/.zshrc` file:126127```zsh128fpath=($HOME/.zsh-complete $fpath)129```130131Or if you'd prefer to load and generate completions at the same time, you can132add the following to your `$HOME/.zshrc` file:133134```zsh135$ source <(rg --generate complete-zsh)136```137138Note though that while this approach is easier to setup, is generally slower139than the previous method, and will add more time to loading your shell prompt.140141For **PowerShell**, create the completions:142143```144$ rg --generate complete-powershell > _rg.ps1145```146147And then add `. _rg.ps1` to your PowerShell148[profile](https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx)149(note the leading period). If the `_rg.ps1` file is not on your `PATH`, do150`. /path/to/_rg.ps1` instead.151152153<h3 name="order">154How can I get results in a consistent order?155</h3>156157By default, ripgrep uses parallelism to execute its search because this makes158the search much faster on most modern systems. This in turn means that ripgrep159has a non-deterministic aspect to it, since the interleaving of threads during160the execution of the program is itself non-deterministic. This has the effect161of printing results in a somewhat arbitrary order, and this order can change162from run to run of ripgrep.163164The only way to make the order of results consistent is to ask ripgrep to165sort the output. Currently, this will disable all parallelism. (On smaller166repositories, you might not notice much of a performance difference!) You167can achieve this with the `--sort path` flag.168169There is more discussion on this topic here:170https://github.com/BurntSushi/ripgrep/issues/152171172173<h3 name="encoding">174How do I search files that aren't UTF-8?175</h3>176177See the [guide's section on file encoding](GUIDE.md#file-encoding).178179180<h3 name="compressed">181How do I search compressed files?182</h3>183184ripgrep's `-z/--search-zip` flag will cause it to search compressed files185automatically. Currently, this supports gzip, bzip2, xz, lzma, lz4, Brotli and186Zstd. Each of these requires the corresponding `gzip`, `bzip2`, `xz`,187`lz4`, `brotli` and `zstd` binaries to be installed on your system. (That is,188ripgrep does decompression by shelling out to another process.)189190ripgrep currently does not search archive formats, so `*.tar.gz` files, for191example, are skipped.192193194<h3 name="multiline">195How do I search over multiple lines?196</h3>197198The `-U/--multiline` flag enables ripgrep to report results that span over199multiple lines.200201202<h3 name="fancy">203How do I use lookaround and/or backreferences?204</h3>205206ripgrep's default regex engine does not support lookaround or backreferences.207This is primarily because the default regex engine is implemented using finite208state machines in order to guarantee a linear worst case time complexity on all209inputs. Backreferences are not possible to implement in this paradigm, and210lookaround appears difficult to do efficiently.211212However, ripgrep optionally supports using PCRE2 as the regex engine instead of213the default one based on finite state machines. You can enable PCRE2 with the214`-P/--pcre2` flag. For example, in the root of the ripgrep repo, you can easily215find all palindromes:216217```218$ rg -P '(\w{10})\1'219tests/misc.rs220483: cmd.arg("--max-filesize").arg("44444444444444444444");221globset/src/glob.rs2221206: matches!(match7, "a*a*a*a*a*a*a*a*a", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");223```224225If your version of ripgrep doesn't support PCRE2, then you'll get an error226message when you try to use the `-P/--pcre2` flag:227228```229$ rg -P '(\w{10})\1'230PCRE2 is not available in this build of ripgrep231```232233Most of the releases distributed by the ripgrep project here on GitHub will234come bundled with PCRE2 enabled. If you installed ripgrep through a different235means (like your system's package manager), then please reach out to the236maintainer of that package to see whether it's possible to enable the PCRE2237feature.238239240<h3 name="colors">241How do I configure ripgrep's colors?242</h3>243244ripgrep has two flags related to colors:245246* `--color` controls *when* to use colors.247* `--colors` controls *which* colors to use.248249The `--color` flag accepts one of the following possible values: `never`,250`auto`, `always` or `ansi`. The `auto` value is the default and will cause251ripgrep to only enable colors when it is printing to a terminal. But if you252pipe ripgrep to a file or some other process, then it will suppress colors.253254The `--colors` flag is a bit more complicated. The general format is:255256```257--colors '{type}:{attribute}:{value}'258```259260* `{type}` should be one of `path`, `line`, `column` or `match`. Each of these261 correspond to the four different types of things that ripgrep will add color262 to in its output. Select the type whose color you want to change.263* `{attribute}` should be one of `fg`, `bg` or `style`, corresponding to264 foreground color, background color, or miscellaneous styling (such as whether265 to bold the output or not).266* `{value}` is determined by the value of `{attribute}`. If267 `{attribute}` is `style`, then `{value}` should be one of `nobold`,268 `bold`, `nointense`, `intense`, `nounderline`, `underline`, `noitalic` or269 `italic`. If `{attribute}` is `fg` or `bg`, then `{value}` should be a color.270271A color is specified by either one of eight of English names, a single 256-bit272number or an RGB triple (with over 16 million possible values, or "true273color").274275The color names are `red`, `blue`, `green`, `cyan`, `magenta`, `yellow`,276`white` or `black`.277278A single 256-bit number is a value in the range 0-255 (inclusive). It can279either be in decimal format (e.g., `62`) or hexadecimal format (e.g., `0x3E`).280281An RGB triple corresponds to three numbers (decimal or hexadecimal) separated282by commas.283284As a special case, `--colors '{type}:none'` will clear all colors and styles285associated with `{type}`, which lets you start with a clean slate (instead of286building on top of ripgrep's default color settings).287288Here's an example that highlights the matches with a nice blue background with289bolded white text:290291```292$ rg somepattern \293 --colors 'match:none' \294 --colors 'match:bg:0x33,0x66,0xFF' \295 --colors 'match:fg:white' \296 --colors 'match:style:bold'297```298299Colors are an ideal candidate to set in your300[configuration file](GUIDE.md#configuration-file). See the301[question on emulating The Silver Searcher's output style](#silver-searcher-output)302for an example specific to colors.303304305<h3 name="truecolors-windows">306How do I enable true colors on Windows?307</h3>308309First, see the previous question's310[answer on configuring colors](#colors).311312Secondly, coloring on Windows is a bit complicated. If you're using a terminal313like Cygwin, then it's likely true color support already works out of the box.314However, if you are using a normal Windows console (`cmd` or `PowerShell`) and315a version of Windows prior to 10, then there is no known way to get true316color support. If you are on Windows 10 and using a Windows console, then317true colors should work out of the box with one caveat: you might need to318clear ripgrep's default color settings first. That is, instead of this:319320```321$ rg somepattern --colors 'match:fg:0x33,0x66,0xFF'322```323324you should do this325326```327$ rg somepattern --colors 'match:none' --colors 'match:fg:0x33,0x66,0xFF'328```329330This is because ripgrep might set the default style for `match` to `bold`, and331it seems like Windows 10's VT100 support doesn't permit bold and true color332ANSI escapes to be used simultaneously. The work-around above will clear333ripgrep's default styling, allowing you to craft it exactly as desired.334335336<h3 name="stop-ripgrep">337How do I stop ripgrep from messing up colors when I kill it?338</h3>339340Type in `color` in cmd.exe (Command Prompt) and `echo -ne "\033[0m"` on341Unix-like systems to restore your original foreground color.342343In PowerShell, you can add the following code to your profile which will344restore the original foreground color when `Reset-ForegroundColor` is called.345Including the `Set-Alias` line will allow you to call it with simply `color`.346347```powershell348$OrigFgColor = $Host.UI.RawUI.ForegroundColor349function Reset-ForegroundColor {350 $Host.UI.RawUI.ForegroundColor = $OrigFgColor351}352Set-Alias -Name color -Value Reset-ForegroundColor353```354355PR [#187](https://github.com/BurntSushi/ripgrep/pull/187) fixed this, and it356was later deprecated in357[#281](https://github.com/BurntSushi/ripgrep/issues/281). A full explanation is358available359[here](https://github.com/BurntSushi/ripgrep/issues/281#issuecomment-269093893).360361362<h3 name="because-cygwin">363Why does using a leading `/` on Windows fail?364</h3>365366If you're using cygwin on Windows and try to search for a pattern beginning367with a `/`, then it's possible that cygwin is mangling that pattern without368your knowledge. For example, if you tried running `rg /foo` in a cygwin shell369on Windows, then cygwin might mistakenly perform path translation on `/foo`,370which would result in `rg C:/msys64/foo` being searched instead.371372You can fix this in one of three ways:3733741. Stop using cygwin.3752. Escape the leading slash with an additional slash. e.g., `rg //foo`.3763. Temporarily disable path translation by setting `MSYS_NO_PATHCONV=1`. e.g.,377 `MSYS_NO_PATHCONV=1 rg /foo`.378379For more details, see https://github.com/BurntSushi/ripgrep/issues/1277380381382<h3 name="size-limit">383How do I get around the regex size limit?384</h3>385386If you've given ripgrep a particularly large pattern (or a large number of387smaller patterns), then it is possible that it will fail to compile because it388hit a pre-set limit. For example:389390```391$ rg '\pL{1000}'392Compiled regex exceeds size limit of 10485760 bytes.393```394395(Note: `\pL{1000}` may look small, but `\pL` is the character class containing396all Unicode letters, which is quite large. *And* it's repeated 1000 times.)397398In this case, you can work around by simply increasing the limit:399400```401$ rg '\pL{1000}' --regex-size-limit 1G402```403404Increasing the limit to 1GB does not necessarily mean that ripgrep will use405that much memory. The limit just says that it's allowed to (approximately) use406that much memory for constructing the regular expression.407408409<h3 name="dfa-size">410How do I make the <code>-f/--file</code> flag faster?411</h3>412413The `-f/--file` permits one to give a file to ripgrep which contains a pattern414on each line. ripgrep will then report any line that matches any of the415patterns.416417If this pattern file gets too big, then it is possible ripgrep will slow down418dramatically. *Typically* this is because an internal cache is too small, and419will cause ripgrep to spill over to a slower but more robust regular expression420engine. If this is indeed the problem, then it is possible to increase this421cache and regain speed. The cache can be controlled via the `--dfa-size-limit`422flag. For example, using `--dfa-size-limit 1G` will set the cache size to 1GB.423(Note that this doesn't mean ripgrep will use 1GB of memory automatically, but424it will allow the regex engine to if it needs to.)425426427<h3 name="silver-searcher-output">428How do I make the output look like The Silver Searcher's output?429</h3>430431Use the `--colors` flag, like so:432433```434rg --colors line:fg:yellow \435 --colors line:style:bold \436 --colors path:fg:green \437 --colors path:style:bold \438 --colors match:fg:black \439 --colors match:bg:yellow \440 --colors match:style:nobold \441 foo442```443444Alternatively, add your color configuration to your ripgrep config file (which445is activated by setting the `RIPGREP_CONFIG_PATH` environment variable to point446to your config file). For example:447448```449$ cat $HOME/.config/ripgrep/rc450--colors=line:fg:yellow451--colors=line:style:bold452--colors=path:fg:green453--colors=path:style:bold454--colors=match:fg:black455--colors=match:bg:yellow456--colors=match:style:nobold457$ RIPGREP_CONFIG_PATH=$HOME/.config/ripgrep/rc rg foo458```459460461<h3 name="pcre2-slow">462Why does ripgrep get slower when I enable PCRE2 regexes?463</h3>464465When you use the `--pcre2` (`-P` for short) flag, ripgrep will use the PCRE2466regex engine instead of the default. Both regex engines are quite fast,467but PCRE2 provides a number of additional features such as look-around and468backreferences that many enjoy using. This is largely because PCRE2 uses469a backtracking implementation where as the default regex engine uses a finite470automaton based implementation. The former provides the ability to add lots of471bells and whistles over the latter, but the latter executes with worst case472linear time complexity.473474With that out of the way, if you've used `-P` with ripgrep, you may have475noticed that it can be slower. The reasons for why this is are quite complex,476and they are complex because the optimizations that ripgrep uses to implement477fast search are complex.478479The task ripgrep has before it is somewhat simple; all it needs to do is search480a file for occurrences of some pattern and then print the lines containing481those occurrences. The problem lies in what is considered a valid match and how482exactly we read the bytes from a file.483484In terms of what is considered a valid match, remember that ripgrep will only485report matches spanning a single line by default. The problem here is that486some patterns can match across multiple lines, and ripgrep needs to prevent487that from happening. For example, `foo\sbar` will match `foo\nbar`. The most488obvious way to achieve this is to read the data from a file, and then apply489the pattern search to that data for each line. The problem with this approach490is that it can be quite slow; it would be much faster to let the pattern491search across as much data as possible. It's faster because it gets rid of the492overhead of finding the boundaries of every line, and also because it gets rid493of the overhead of starting and stopping the pattern search for every single494line. (This is operating under the general assumption that matching lines are495much rarer than non-matching lines.)496497It turns out that we can use the faster approach by applying a very simple498restriction to the pattern: *statically prevent* the pattern from matching499through a `\n` character. Namely, when given a pattern like `foo\sbar`,500ripgrep will remove `\n` from the `\s` character class automatically. In some501cases, a simple removal is not so easy. For example, ripgrep will return an502error when your pattern includes a `\n` literal:503504```505$ rg '\n'506the literal '"\n"' is not allowed in a regex507```508509So what does this have to do with PCRE2? Well, ripgrep's default regex engine510exposes APIs for doing syntactic analysis on the pattern in a way that makes511it quite easy to strip `\n` from the pattern (or otherwise detect it and report512an error if stripping isn't possible). PCRE2 seemingly does not provide a513similar API, so ripgrep does not do any stripping when PCRE2 is enabled. This514forces ripgrep to use the "slow" search strategy of searching each line515individually.516517OK, so if enabling PCRE2 slows down the default method of searching because it518forces matches to be limited to a single line, then why is PCRE2 also sometimes519slower when performing multiline searches? Well, that's because there are520*multiple* reasons why using PCRE2 in ripgrep can be slower than the default521regex engine. This time, blame PCRE2's Unicode support, which ripgrep enables522by default. In particular, PCRE2 cannot simultaneously enable Unicode support523and search arbitrary data. That is, when PCRE2's Unicode support is enabled,524the data **must** be valid UTF-8 (to do otherwise is to invoke undefined525behavior). This is in contrast to ripgrep's default regex engine, which can526enable Unicode support and still search arbitrary data. ripgrep's default527regex engine simply won't match invalid UTF-8 for a pattern that can otherwise528only match valid UTF-8. Why doesn't PCRE2 do the same? This author isn't529familiar with its internals, so we can't comment on it here.530531The bottom line here is that we can't enable PCRE2's Unicode support without532simultaneously incurring a performance penalty for ensuring that we are533searching valid UTF-8. In particular, ripgrep will transcode the contents534of each file to UTF-8 while replacing invalid UTF-8 data with the Unicode535replacement codepoint. ripgrep then disables PCRE2's own internal UTF-8536checking, since we've guaranteed the data we hand it will be valid UTF-8. The537reason why ripgrep takes this approach is because if we do hand PCRE2 invalid538UTF-8, then it will report a match error if it comes across an invalid UTF-8539sequence. This is not good news for ripgrep, since it will stop it from540searching the rest of the file, and will also print potentially undesirable541error messages to users.542543All right, the above is a lot of information to swallow if you aren't already544familiar with ripgrep internals. Let's make this concrete with some examples.545First, let's get some data big enough to magnify the performance differences:546547```548$ curl -O 'https://burntsushi.net/stuff/subtitles2016-sample.gz'549$ gzip -d subtitles2016-sample550$ md5sum subtitles2016-sample551e3cb796a20bbc602fbfd6bb43bda45f5 subtitles2016-sample552```553554To search this data, we will use the pattern `^\w{42}$`, which contains exactly555one hit in the file and has no literals. Having no literals is important,556because it ensures that the regex engine won't use literal optimizations to557speed up the search. In other words, it lets us reason coherently about the558actual task that the regex engine is performing.559560Let's now walk through a few examples in light of the information above. First,561let's consider the default search using ripgrep's default regex engine and562then the same search with PCRE2:563564```565$ time rg '^\w{42}$' subtitles2016-sample56621225780:EverymajordevelopmentinthehistoryofAmerica567568real 0m1.783s569user 0m1.731s570sys 0m0.051s571572$ time rg -P '^\w{42}$' subtitles2016-sample57321225780:EverymajordevelopmentinthehistoryofAmerica574575real 0m2.458s576user 0m2.419s577sys 0m0.038s578```579580In this particular example, both pattern searches are using a Unicode aware581`\w` character class and both are counting lines in order to report line582numbers. The key difference here is that the first search will not search583line by line, but the second one will. We can observe which strategy ripgrep584uses by passing the `--trace` flag:585586```587$ rg '^\w{42}$' subtitles2016-sample --trace588[... snip ...]589TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:622: Some("subtitles2016-sample"): searching via memory map590TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:712: slice reader: searching via slice-by-line strategy591TRACE|grep_searcher::searcher::core|grep-searcher/src/searcher/core.rs:61: searcher core: will use fast line searcher592[... snip ...]593594$ rg -P '^\w{42}$' subtitles2016-sample --trace595[... snip ...]596TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:622: Some("subtitles2016-sample"): searching via memory map597TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:705: slice reader: needs transcoding, using generic reader598TRACE|grep_searcher::searcher|grep-searcher/src/searcher/mod.rs:685: generic reader: searching via roll buffer strategy599TRACE|grep_searcher::searcher::core|grep-searcher/src/searcher/core.rs:63: searcher core: will use slow line searcher600[... snip ...]601```602603The first says it is using the "fast line searcher" where as the latter says604it is using the "slow line searcher." The latter also shows that we are605decoding the contents of the file, which also impacts performance.606607Interestingly, in this case, the pattern does not match a `\n` and the file608we're searching is valid UTF-8, so neither the slow line-by-line search609strategy nor the decoding are necessary. We could fix the former issue with610better PCRE2 introspection APIs. We can actually fix the latter issue with611ripgrep's `--no-encoding` flag, which prevents the automatic UTF-8 decoding,612but will enable PCRE2's own UTF-8 validity checking. Unfortunately, it's slower613in my build of ripgrep:614615```616$ time rg -P '^\w{42}$' subtitles2016-sample --no-encoding61721225780:EverymajordevelopmentinthehistoryofAmerica618619real 0m3.074s620user 0m3.021s621sys 0m0.051s622```623624(Tip: use the `--trace` flag to verify that no decoding in ripgrep is625happening.)626627A possible reason why PCRE2's UTF-8 checking is slower is because it might628not be better than the highly optimized UTF-8 checking routines found in the629[`encoding_rs`](https://github.com/hsivonen/encoding_rs) library, which is what630ripgrep uses for UTF-8 decoding. Moreover, my build of ripgrep enables631`encoding_rs`'s SIMD optimizations, which may be in play here.632633Also, note that using the `--no-encoding` flag can cause PCRE2 to report634invalid UTF-8 errors, which causes ripgrep to stop searching the file:635636```637$ cat invalid-utf8638foobar639640$ xxd invalid-utf864100000000: 666f 6fff 6261 720a foo.bar.642643$ rg foo invalid-utf86441:foobar645646$ rg -P foo invalid-utf86471:foo�bar648649$ rg -P foo invalid-utf8 --no-encoding650invalid-utf8: PCRE2: error matching: UTF-8 error: illegal byte (0xfe or 0xff)651```652653All right, so at this point, you might think that we could remove the penalty654for line-by-line searching by enabling multiline search. After all, our655particular pattern can't match across multiple lines anyway, so we'll still get656the results we want. Let's try it:657658```659$ time rg -U '^\w{42}$' subtitles2016-sample66021225780:EverymajordevelopmentinthehistoryofAmerica661662real 0m1.803s663user 0m1.748s664sys 0m0.054s665666$ time rg -P -U '^\w{42}$' subtitles2016-sample66721225780:EverymajordevelopmentinthehistoryofAmerica668669real 0m2.962s670user 0m2.246s671sys 0m0.713s672```673674Search times remain the same with the default regex engine, but the PCRE2675search gets _slower_. What happened? The secrets can be revealed with the676`--trace` flag once again. In the former case, ripgrep actually detects that677the pattern can't match across multiple lines, and so will fall back to the678"fast line search" strategy as with our search without `-U`.679680However, for PCRE2, things are much worse. Namely, since Unicode mode is still681enabled, ripgrep is still going to decode UTF-8 to ensure that it hands only682valid UTF-8 to PCRE2. Unfortunately, one key downside of multiline search is683that ripgrep cannot do it incrementally. Since matches can be arbitrarily long,684ripgrep actually needs the entire file in memory at once. Normally, we can use685a memory map for this, but because we need to UTF-8 decode the file before686searching it, ripgrep winds up reading the entire contents of the file on to687the heap before executing a search. Owch.688689OK, so Unicode is killing us here. The file we're searching is _mostly_ ASCII,690so maybe we're OK with missing some data. (Try `rg '[\w--\p{ascii}]'` to see691non-ASCII word characters that an ASCII-only `\w` character class would miss.)692We can disable Unicode in both searches, but this is done differently depending693on the regex engine we use:694695```696$ time rg '(?-u)^\w{42}$' subtitles2016-sample69721225780:EverymajordevelopmentinthehistoryofAmerica698699real 0m1.714s700user 0m1.669s701sys 0m0.044s702703$ time rg -P '^\w{42}$' subtitles2016-sample --no-pcre2-unicode70421225780:EverymajordevelopmentinthehistoryofAmerica705706real 0m1.997s707user 0m1.958s708sys 0m0.037s709```710711For the most part, ripgrep's default regex engine performs about the same.712PCRE2 does improve a little bit, and is now almost as fast as the default713regex engine. If you look at the output of `--trace`, you'll see that ripgrep714will no longer perform UTF-8 decoding, but it does still use the slow715line-by-line searcher.716717At this point, we can combine all of our insights above: let's try to get off718of the slow line-by-line searcher by enabling multiline mode, and let's stop719UTF-8 decoding by disabling Unicode support:720721```722$ time rg -U '(?-u)^\w{42}$' subtitles2016-sample72321225780:EverymajordevelopmentinthehistoryofAmerica724725real 0m1.714s726user 0m1.655s727sys 0m0.058s728729$ time rg -P -U '^\w{42}$' subtitles2016-sample --no-pcre2-unicode73021225780:EverymajordevelopmentinthehistoryofAmerica731732real 0m1.121s733user 0m1.071s734sys 0m0.048s735```736737Ah, there's PCRE2's JIT shining! ripgrep's default regex engine once again738remains about the same, but PCRE2 no longer needs to search line-by-line and it739no longer needs to do any kind of UTF-8 checks. This allows the file to get740memory mapped and passed right through PCRE2's JIT at impressive speeds. (As741a brief and interesting historical note, the configuration of "memory map +742multiline + no-Unicode" is exactly the configuration used by The Silver743Searcher. This analysis perhaps sheds some reasoning as to why that744configuration is useful!)745746In summary, if you want PCRE2 to go as fast as possible and you don't care747about Unicode and you don't care about matches possibly spanning across748multiple lines, then enable multiline mode with `-U` and disable PCRE2's749Unicode support with the `--no-pcre2-unicode` flag.750751Caveat emptor: This author is not a PCRE2 expert, so there may be APIs that can752improve performance that the author missed. Similarly, there may be alternative753designs for a searching tool that are more amenable to how PCRE2 works.754755756<h3 name="rg-other-cmd">757When I run <code>rg</code>, why does it execute some other command?758</h3>759760It's likely that you have a shell alias or even another tool called `rg` which761is interfering with ripgrep. Run `which rg` to see what it is.762763(Notably, the Rails plug-in for764[Oh My Zsh](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#rails) sets765up an `rg` alias for `rails generate`.)766767Problems like this can be resolved in one of several ways:768769* If you're using the OMZ Rails plug-in, disable it by editing the `plugins`770 array in your zsh configuration.771* Temporarily bypass an existing `rg` alias by calling ripgrep as772 `command rg`, `\rg`, or `'rg'`.773* Temporarily bypass an existing alias or another tool named `rg` by calling774 ripgrep by its full path (e.g., `/usr/bin/rg` or `/usr/local/bin/rg`).775* Permanently disable an existing `rg` alias by adding `unalias rg` to the776 bottom of your shell configuration file (e.g., `.bash_profile` or `.zshrc`).777* Give ripgrep its own alias that doesn't conflict with other tools/aliases by778 adding a line like the following to the bottom of your shell configuration779 file: `alias ripgrep='command rg'`.780781782<h3 name="rg-alias-windows">783How do I create an alias for ripgrep on Windows?784</h3>785786Often you can find a need to make alias for commands you use a lot that set787certain flags. But PowerShell function aliases do not behave like your typical788linux shell alias. You always need to propagate arguments and `stdin` input.789But it cannot be done simply as790`function grep() { $input | rg.exe --hidden $args }`791792Use below example as reference to how setup alias in PowerShell.793794```powershell795function grep {796 $count = @($input).Count797 $input.Reset()798799 if ($count) {800 $input | rg.exe --hidden $args801 }802 else {803 rg.exe --hidden $args804 }805}806```807808PowerShell special variables:809810* input - is powershell `stdin` object that allows you to access its content.811* args - is array of arguments passed to this function.812813This alias checks whether there is `stdin` input and propagates only if there814is some lines. Otherwise empty `$input` will make powershell to trigger `rg` to815search empty `stdin`.816817818<h3 name="powershell-profile">819How do I create a PowerShell profile?820</h3>821822To customize powershell on start-up, there is a special PowerShell script that823has to be created. In order to find its location, type `$profile`.824See825[Microsoft's documentation](https://technet.microsoft.com/en-us/library/bb613488(v=vs.85).aspx)826for more details.827828Any PowerShell code in this file gets evaluated at the start of console. This829way you can have own aliases to be created at start.830831832<h3 name="pipe-non-ascii-windows">833How do I pipe non-ASCII content to ripgrep on Windows?834</h3>835836When piping input into native executables in PowerShell, the encoding of the837input is controlled by the `$OutputEncoding` variable. By default, this is set838to US-ASCII, and any characters in the pipeline that don't have encodings in839US-ASCII are converted to `?` (question mark) characters.840841To change this setting, set `$OutputEncoding` to a different encoding, as842represented by a .NET encoding object. Some common examples are below. The843value of this variable is reset when PowerShell restarts, so to make this844change take effect every time PowerShell is started add a line setting the845variable into your PowerShell profile.846847Example `$OutputEncoding` settings:848849* UTF-8 without BOM: `$OutputEncoding = [System.Text.UTF8Encoding]::new()`850* The console's output encoding:851 `$OutputEncoding = [System.Console]::OutputEncoding`852853If you continue to have encoding problems, you can also force the encoding854that the console will use for printing to UTF-8 with855`[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8`. This856will also reset when PowerShell is restarted, so you can add that line857to your profile as well if you want to make the setting permanent.858859<h3 name="search-and-replace">860How can I search and replace with ripgrep?861</h3>862863Using ripgrep alone, you can't. ripgrep is a search tool that will never864touch your files. However, the output of ripgrep can be piped to other tools865that do modify files on disk. See866[this issue](https://github.com/BurntSushi/ripgrep/issues/74) for more867information.868869sed is one such tool that can modify files on disk. sed can take a filename870and a substitution command to search and replace in the specified file.871Files containing matching patterns can be provided to sed using872873```874rg foo --files-with-matches875```876877The output of this command is a list of filenames that contain a match for878the `foo` pattern.879880This list can be piped into `xargs`, which will split the filenames from881standard input into arguments for the command following xargs. You can use this882combination to pipe a list of filenames into sed for replacement. For example:883884```885rg foo --files-with-matches | xargs sed -i 's/foo/bar/g'886```887888will replace all instances of 'foo' with 'bar' in the files in which889ripgrep finds the foo pattern. The `-i` flag to sed indicates that you are890editing files in place, and `s/foo/bar/g` says that you are performing a891**s**ubstitution of the pattern `foo` for `bar`, and that you are doing this892substitution **g**lobally (all occurrences of the pattern in each file).893894Note: the above command assumes that you are using GNU sed. If you are using895BSD sed (the default on macOS and FreeBSD) then you must modify the above896command to be the following:897898```899rg foo --files-with-matches | xargs sed -i '' 's/foo/bar/g'900```901902The `-i` flag in BSD sed requires a file extension to be given to make backups903for all modified files. Specifying the empty string prevents file backups from904being made.905906Finally, if any of your file paths contain whitespace in them, then you might907need to delimit your file paths with a NUL terminator. This requires telling908ripgrep to output NUL bytes between each path, and telling xargs to read paths909delimited by NUL bytes:910911```912rg foo --files-with-matches -0 | xargs -0 sed -i 's/foo/bar/g'913```914915To learn more about sed, see the sed manual916[here](https://www.gnu.org/software/sed/manual/sed.html).917918Additionally, Facebook has a tool called919[fastmod](https://github.com/facebookincubator/fastmod)920that uses some of the same libraries as ripgrep and might provide a more921ergonomic search-and-replace experience.922923924<h3 name="license">925How is ripgrep licensed?926</h3>927928ripgrep is dual licensed under the929[Unlicense](https://unlicense.org/)930and MIT licenses. Specifically, you may use ripgrep under the terms of either931license.932933The reason why ripgrep is dual licensed this way is two-fold:9349351. I, as ripgrep's author, would like to participate in a small bit of936 ideological activism by promoting the Unlicense's goal: to disclaim937 copyright monopoly interest.9382. I, as ripgrep's author, would like as many people to use ripgrep as939 possible. Since the Unlicense is not a proven or well known license, ripgrep940 is also offered under the MIT license, which is ubiquitous and accepted by941 almost everyone.942943More specifically, ripgrep and all its dependencies are compatible with this944licensing choice. In particular, ripgrep's dependencies (direct and transitive)945will always be limited to permissive licenses. That is, ripgrep will never946depend on code that is not permissively licensed. This means rejecting any947dependency that uses a copyleft license such as the GPL, LGPL, MPL or any of948the Creative Commons ShareAlike licenses. Whether the license is "weak"949copyleft or not does not matter; ripgrep will **not** depend on it.950951952<h3 name="posix4ever">953Can ripgrep replace grep?954</h3>955956Yes and no.957958If, upon hearing that "ripgrep can replace grep," you *actually* hear, "ripgrep959can be used in every instance grep can be used, in exactly the same way, for960the same use cases, with exactly the same bug-for-bug behavior," then no,961ripgrep trivially *cannot* replace grep. Moreover, ripgrep will *never* replace962grep.963964If, upon hearing that "ripgrep can replace grep," you *actually* hear, "ripgrep965can replace grep in some cases and not in other use cases," then yes, that is966indeed true!967968Let's go over some of those use cases in favor of ripgrep. Some of these may969not apply to you. That's OK. There may be other use cases not listed here that970do apply to you. That's OK too.971972(For all claims related to performance in the following words, see my973[blog post](https://blog.burntsushi.net/ripgrep/)974introducing ripgrep.)975976* Are you frequently searching a repository of code? If so, ripgrep might be a977 good choice since there's likely a good chunk of your repository that you978 don't want to search. grep, can, of course, be made to filter files using979 recursive search, and if you don't mind writing out the requisite `--exclude`980 rules or writing wrapper scripts, then grep might be sufficient. (I'm not981 kidding, I myself did this with grep for almost a decade before writing982 ripgrep.) But if you instead enjoy having a search tool respect your983 `.gitignore`, then ripgrep might be perfect for you!984* Are you frequently searching non-ASCII text that is UTF-8 encoded? One of985 ripgrep's key features is that it can handle Unicode features in your986 patterns in a way that tends to be faster than GNU grep. Unicode features987 in ripgrep are enabled by default; there is no need to configure your locale988 settings to use ripgrep properly because ripgrep doesn't respect your locale989 settings.990* Do you need to search UTF-16 files and you don't want to bother explicitly991 transcoding them? Great. ripgrep does this for you automatically. No need992 to enable it.993* Do you need to search a large directory of large files? ripgrep uses994 parallelism by default, which tends to make it faster than a standard995 `grep -r` search. However, if you're OK writing the occasional996 `find ./ -print0 | xargs -P8 -0 grep` command, then maybe grep is good997 enough.998999Here are some cases where you might *not* want to use ripgrep. The same caveats1000for the previous section apply.10011002* Are you writing portable shell scripts intended to work in a variety of1003 environments? Great, probably not a good idea to use ripgrep! ripgrep has1004 nowhere near the ubiquity of grep, so if you do use ripgrep, you might need1005 to futz with the installation process more than you would with grep.1006* Do you care about POSIX compatibility? If so, then you can't use ripgrep1007 because it never was, isn't and never will be POSIX compatible.1008* Do you hate tools that try to do something smart? If so, ripgrep is all about1009 being smart, so you might prefer to just stick with grep.1010* Is there a particular feature of grep you rely on that ripgrep either doesn't1011 have or never will have? If the former, file a bug report, maybe ripgrep can1012 do it! If the latter, well, then, just use grep.101310141015<h3 name="intentcountsforsomething">1016What does the "rip" in ripgrep mean?1017</h3>10181019When I first started writing ripgrep, I called it `rep`, intending it to be a1020shorter variant of `grep`. Soon after, I renamed it to `xrep` since `rep`1021wasn't obvious enough of a name for my taste. And also because adding `x` to1022anything always makes it better, right?10231024Before ripgrep's first public release, I decided that I didn't like `xrep`. I1025thought it was slightly awkward to type, and despite my previous praise of the1026letter `x`, I kind of thought it was pretty lame. Being someone who really1027likes Rust, I wanted to call it "rustgrep" or maybe "rgrep" for short. But I1028thought that was just as lame, and maybe a little too in-your-face. But I1029wanted to continue using `r` so I could at least pretend Rust had something to1030do with it.10311032I spent a couple of days trying to think of very short words that began with1033the letter `r` that were even somewhat related to the task of searching. I1034don't remember how it popped into my head, but "rip" came up as something that1035meant "fast," as in, "to rip through your text." The fact that RIP is also1036an initialism for "Rest in Peace" (as in, "ripgrep kills grep") never really1037dawned on me. Perhaps the coincidence is too striking to believe that, but1038I didn't realize it until someone explicitly pointed it out to me after the1039initial public release. I admit that I found it mildly amusing, but if I had1040realized it myself before the public release, I probably would have pressed on1041and chose a different name. Alas, renaming things after a release is hard, so I1042decided to mush on.10431044Given the fact that1045[ripgrep never was, is or will be a 100% drop-in replacement for1046grep](#posix4ever),1047ripgrep is neither actually a "grep killer" nor was it ever intended to be. It1048certainly does eat into some of its use cases, but that's nothing that other1049tools like ack or The Silver Searcher weren't already doing.105010511052<h3 name="donations">1053How can I donate to ripgrep or its maintainers?1054</h3>10551056I welcome [sponsorship](https://github.com/sponsors/BurntSushi/).10571058Or if you'd prefer, donating to a charitable organization that you like would1059also be most welcome. My favorites are:10601061* [The Internet Archive](https://archive.org/donate/)1062* [Rails Girls](https://railsgirlssummerofcode.org/)1063* [Wikipedia](https://wikimediafoundation.org/support/)