UNPKG

rexreplace

Version:

Search & replace across files with a CLI tool that makes you trust what you are doing.

181 lines (125 loc) 88.7 kB
<h3 align="center"> <br> <img width="350" src="https://cloud.githubusercontent.com/assets/1063454/24127465/ed1a59a2-0e28-11e7-9546-160d7eb1d8b9.png" alt="RexReplace mascot Benny on the RexReplace logo" /> <br> <br> </h3> # RexReplace [![CI-test](https://github.com/mathiasrw/rexreplace/workflows/CI-test/badge.svg)](https://github.com/mathiasrw/rexreplace/actions) [![NPM downloads](https://img.shields.io/npm/dm/rexreplace.svg?style=flat&label=npm%20downloads)](https://npm-stat.com/charts.html?package=rexreplace) [![npm version](https://badge.fury.io/js/rexreplace.svg)](https://www.npmjs.com/package/rexreplace) [![FOSSA Status](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmathiasrw%2Frexreplace?ref=badge_shield) [![OPEN open source software](https://img.shields.io/badge/Open--OSS-%E2%9C%94-brightgreen.svg)](http://open-oss.com) > RexReplace is a versatile tool to search and replace text in files from the command line. It's inspired by how developers often need to do quick fixes or one-liners for build scripts. **Key features**: - Easy and intuitive notation makes you trust what you are doing - Replacement can be javascript code - giving you Turing complete flexibility - Pinpoint the exact files with [glob notation](https://www.tcl.tk/man/tcl8.5/tutorial/Tcl16a.html) (`docs/*.md` represents each markdown file in `docs/`) - No more brute-forcing the right combination of `find`, `cat`, `sed`, `tr`, and `awk` to replace a text pattern in the right files ## Install To use RexReplace from your command line ```bash > npm install -g rexreplace ``` To use RexReplace from an npm build script: ```bash > yarn add rexreplace --dev # or > npm install rexreplace --save-dev ``` ## Examples Let 'foobar' become 'xxxbar' in myfile.md ```bash > rexreplace 'Foo' 'xxx' myfile.md ``` Hard for your fingers to write on your keyboard? We got you covered with the `rr` alias for `rexreplace` ```bash > rr Foo xxx myfile.md ``` --- #### Catch the beginning Let all markdown files in the `docs/` dir get headlines moved one level deeper ```bash > rexreplace '^#' '##' docs/*.md ``` --- #### Using glob notation to pinpoint files Fix a spell error in all javascript and typescript files in the folders `src/` and `test/` recursively. ```bash > rexreplace 'foubar' 'foobar' '{src,test}/**/*.{js,ts}' ``` --- #### Dynamically generated content Let the version number from package.json get into your distribution js files (use the string `VERSION` in your source files). ```bash > rexreplace 'VERSION' 'require("./package.json").version' -j dist/*.js ``` Require have been given the alias `r` and both are expanded to understand relative paths even without `./` prepended. As the file extension is not needed eighter you will get the same result writing: ```bash > rexreplace 'VERSION' 'r("package").version' -j dist/*.js ``` #### Reference a matching group Let 'foobar' become 'barfoo' in myfile.md ```bash > rexreplace '(foo)(.*)' '$2$1' myfile.md ``` RexReplace defaults to treating `€` as an alias for `$` so the following will do the same as the previous example ```bash > rexreplace '(foo)(.*)' '€2€1' myfile.md ``` --- #### Surviving backslash-escape hell Let `foo[bar]` become `foo.0.[bar]` in myfile.md ```bash > rexreplace '\[' '.0.[' myfile.md ``` The `[` as a literate char must be escaped according to regex. If you run the command as a parameter (this could be from a script in package.json) you need to escape the escape: ```json "scripts":{ "fix": "rexreplace '\\[' '.0.[' myfile.md" } ``` RexReplace defaults to treating `§` as an alias for `\` so the following give same result: ```bash > rexreplace '§[' '.0.[' myfile.md ``` ```json "scripts":{ "fix": "rexreplace '§[' '.0.[' myfile.md" } ``` --- #### More relevant examples **Per file info** Add creation time, name of the file and human readable file size as the first line in each file in `test-run` recursively. ```bash > rexreplace '^' 'ctime_ + name_ + size + nl' -j -M 'dist/**/*.*' ``` **Matching pairs** Let both `"foo 'is' bar"` and `' foo "was" bar'` ' become `» foo ... bar «` independent of using `'` or `"` ```bash > rexreplace '(['"])([^\1]+)\1' '» $2 «' myfile.md ``` --- ## Usage ```bash > rexreplace pattern replacement [fileGlob|option]+ ``` | Flag | Effect | | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `-v` | **`--version`** Print rexreplace version (can be given as only argument) [boolean] | | `-V` | **`--verbose`** More chatty output [boolean] | | `-L` | **`--literal`** Literal string search (no regex used when searching) [boolean] | | `-I` | **`--void-ignore-case`** Void case insensitive search pattern. [boolean] | | `-G` | **`--void-global`** Void global search (stop looking after the first match). [boolean] | | `-s` | **`--dot-all`** Have `.` also match newline. [boolean] | | `-M` | **`--void-multiline`** Void multiline search pattern. Makes ^ and $ match start/end of whole content rather than each line. [boolean] | | `-u` | **`--unicode`** Treat pattern as a sequence of unicode code points. [boolean] | | `-e` | **`--encoding`** Encoding of files/piped data. [default: "utf8"] | | `-E` | **`--engine`** What regex engine to use: [choices: "V8"] [default: "V8"] | | `-q` | **`--quiet`** Only display errors (no other info) [boolean] | | `-Q` | **`--quiet-total`** Never display errors or info [boolean] | | `-H` | **`--halt`** Halt on first error [boolean] [default: false] | | `-d` | **`--debug`** Print debug info [boolean] | | `-€` | **`--void-euro`** Void having `€` as alias for `$` in pattern and replacement parameters [boolean] | | `-§` | **`--void-section`** Void having `§` as alias for `\` in pattern and replacement parameters [boolean]