USAGE: rg [OPTIONS] PATTERN [PATH ...] rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...] rg [OPTIONS] --files [PATH ...] rg [OPTIONS] --type-list command | rg [OPTIONS] PATTERN ARGS: <PATTERN> A regular expression used for searching. To match a pattern beginning with a dash, use the -e/--regexp flag. For example, to search for the literal '-foo', you can use this flag: rg -e -foo You can also use the special '--' delimiter to indicate that no more flags will be provided. Namely, the following is equivalent to the above: rg -- -foo <PATH>... A file or directory to search. Directories are searched recursively. Paths specified on the command line override glob and ignore rules.
$ rg 'github.com' README.md 1:<h1 align="center"><a title="New «NexT» 6.0.0 version [Reloaded]" href="https://github.com/theme-next/hexo-theme-next">NexT</a></h1>
6:[![mnt-image]](https://github.com/theme-next/hexo-theme-next)
21:More NexT examples [here](https://github.com/iissnan/hexo-theme-next/issues/119). 41: $ curl -s https://api.github.com/repos/iissnan/hexo-theme-next/releases/latest | grep tarball_url | cut -d '"' -f 4 | wget -i - -O- | tar -zx -C themes/next --strip-components=1 51: $ curl -L https://api.github.com/repos/iissnan/hexo-theme-next/tarball/v5.1.2 | tar -zxv -C themes/next --strip-components=1 57: $ git clone --branch v5.1.2 https://github.com/iissnan/hexo-theme-next themes/next 67: $ curl -L https://api.github.com/repos/iissnan/hexo-theme-next/tarball | tar -zxv -C themes/next --strip-components=1 73: $ git clone https://github.com/iissnan/hexo-theme-next themes/next 110:For those who also encounter **Error: Cannot find module 'hexo-util'** [issue](https://github.com/iissnan/hexo-theme-next/issues/1490), please check your NPM version. 128:282:NexT uses [Tomorrow Theme](https://github.com/chriskempson/tomorrow-theme) with 5 themes for you to choose from. 288:Head over to [Tomorrow Theme](https://github.com/chriskempson/tomorrow-theme) for more details. 367:[download-latest-url]: https://github.com/iissnan/hexo-theme-next/archive/master.zip 368:[releases-latest-url]: https://github.com/iissnan/hexo-theme-next/releases/latest 369:[releases-url]: https://github.com/iissnan/hexo-theme-next/releases 370:[tags-url]: https://github.com/iissnan/hexo-theme-next/tags 371:[commits-url]: https://github.com/iissnan/hexo-theme-next/commits/master
搜索指定文件中包含以关键字开头的单词的内容
$ rg 'lang\w+' README.md 154:168:Default language is English. 171:language: en 172:173:174:175:176:177:178:179:180:181:
搜索指定文件中包含以关键字开头的内容
$ rg 'hexo\w*' README.md 1:<h1 align="center"><a title="New «NexT» 6.0.0 version [Reloaded]" href="https://github.com/theme-next/hexo-theme-next">NexT</a></h1>
3:<p align="center">NexT is a high quality elegant <a href="http://hexo.io">Hexo</a> theme. It is crafted from scratch, with love.</p>
6:[![mnt-image]](https://github.com/theme-next/hexo-theme-next)
9:[![hexo-image]][hexo-url]
21:More NexT examples [here](https://github.com/iissnan/hexo-theme-next/issues/119). 25:**1.** Change dir to **hexo root** directory. There must be `node_modules`, `source`, `themes` and other directories: 27: $ cd hexo 41: $ curl -s https://api.github.com/repos/iissnan/hexo-theme-next/releases/latest | grep tarball_url | cut -d '"' -f 4 | wget -i - -O- | tar -zx -C themes/next --strip-components=1 51: $ curl -L https://api.github.com/repos/iissnan/hexo-theme-next/tarball/v5.1.2 | tar -zxv -C themes/next --strip-components=1 57: $ git clone --branch v5.1.2 https://github.com/iissnan/hexo-theme-next themes/next
搜索指定目录及子目中包含关键字的内容
$ rg 'github.com' ./ ./src/scrollspy.js 6:* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)./src/affix.js 6: * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)./src/js.cookie.js 3: * https://github.com/js-cookie/js-cookie
搜索以关键字为独立单词的内容
$ rg -w 'github.com' ./ ./bower.json 36: "url" : "http://github.com/julianshapiro/velocity.git"./velocity.ui.js 58: var abortError = "Velocity UI Pack: You need to update Velocity (jquery.velocity.js) to a newer version. Visit http://github.com/julianshapiro/velocity.";./velocity.js 442: /* IE detection. Gist: https://gist.github.com/julianshapiro/9098609 */ 463: /* rAF shim. Gist: https://gist.github.com/julianshapiro/9497513 */ 472: /* Technique by Erik Moller. MIT license: https://gist.github.com/paulirish/1579671 */ 480: /* Array compacting. Copyright Lo-Dash. MIT License: https://github.com/lodash/lodash/blob/master/LICENSE.txt */ 522: /* Copyright Martin Bohm. MIT License: https://gist.github.com/Tomalak/818a78a226a0738eaade */
$ rg -ie "Return.*" merge.js 103:var root = freeGlobal || freeSelf || Function('return this')(); 120: return freeProcess && freeProcess.binding('util'); 134: * @returns {Object} Returns `map`. 137: // Don't return `map.set` because it's not chainable in IE 11. 139: return map; 148: * @returns {Object} Returns `set`. 151: // Don't return `set.add` because it's not chainable in IE 11.
把关键字当成常量字符进行搜索
关键字中包含 .(){}*+ 类似字符时,不需要手动转义。
$ rg -F "i++)" ./ ./tags/exturl.js 27: for (; i < len; i++) {./tags/group-pictures.js 795: for (var i = 0; i < rows; i++) {
805: for (var i = 0; i < rows.length; i++) {
825: for (var i = 0; i < pictures.length; i++) {
如果要搜索的字符是以 - 开头时,要用 -- 来作为分隔符。
$ rg -- -1 merge.js 190: var index = -1, 210: var index = -1, 233: var index = -1, 255: var index = -1, 317: var index = -1, 348: var index = -1, 435: var index = -1, 533: var index = -1, 605: return assocIndexOf(this.__data__, key) > -1; 645: var index = -1, 889: * @returns {number} Returns the index of the matched value, else `-1`. 898: return -1;
$ rg browse README.md --replace Browse 305:322:![Browser-image]
324:[![Browser Stack](.github/Browserstack_logo.png)](https://www.Browserstack.com/)
325:>**BrowserStack** is a cloud-based cross-Browser testing tool that enables developers to test their websites across various Browsers on different operating systems and mobile devices, without requiring users to install virtual machines, devices or emulators. 343:[Browser-image]: https://img.shields.io/badge/Browser-%20chrome%20%7C%20firefox%20%7C%20opera%20%7C%20safari%20%7C%20ie%20%3E%3D%209-lightgrey.svg 344:[Browser-url]: https://www.Browserstack.com
上面的结果实质上只是在标准输出中进行替换,并不会对实际文件进行修改。如果你需要对实际文件进行修改,你可以结合 Sed 命令来达到目的。
如果你使用 GNU Sed (CentOS、Ubuntu 等各种 Linux 发行版),可以使用以下命令。
$ rg browse --files-with-matches | xargs sed -i 's/browse/Browse/g'
如果您使用 BSD Sed( macOS 和 FreeBSD),则必须将以上命令修改为以下命令。
$ rg browse --files-with-matches | xargs sed -i '' 's/browse/Browse/g'
BSD Sed 中的 -i 标志需要提供文件扩展名以对所有已修改的文件进行备份,这里指定空字符串可防止进行文件备份。