UNPKG

2.04 kBMarkdownView Raw
1# replace
2`replace` is a command line utility for performing search-and-replace on files. It's similar to sed but there are a few differences:
3
4* Modifies files when matches are found
5* Recursive search on directories with -r
6* Uses [JavaScript syntax](https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions#Using_Simple_Patterns) for regular expressions and [replacement strings](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter).
7
8# Install
9With [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
10
11 npm install replace -g
12
13
14## Examples
15
16Replace all occurrences of "var" with "let" in files in the current directory:
17
18```
19replace 'var' 'let' *
20```
21
22Replace in all files in a recursive search of the current directory:
23
24```
25replace 'var' 'let' . -r
26```
27
28Replace only in test/file1.js and test/file2.js:
29
30```
31replace 'var' 'let' test/file1.js test/file2.js
32```
33
34Replace all word pairs with "_" in middle with a "-":
35
36```
37replace '(\w+)_(\w+)' '$1-$2' *
38```
39
40Replace only in files with names matching *.js:
41
42```
43replace 'var' 'let' . -r --include="*.js"
44```
45
46Don't replace in files with names matching *.min.js and *.py:
47
48```
49replace 'var' 'let' . -r --exclude="*.min.js,*.py"
50```
51
52Preview the replacements without modifying any files:
53
54```
55replace 'var' 'let' . -r --preview
56```
57
58See all the options:
59
60```
61replace -h
62```
63
64# More Details
65
66### Search
67There's also a `search` command. It's like `grep`, but with `replace`'s syntax, and slower!
68
69```
70search "setTimeout" . -r
71```
72
73### Excludes
74By default, `replace` and `search` will exclude files (binaries, images, etc) that match patterns in the `"defaultignore"` located in this directory.
75
76### On huge directories
77If `replace` is taking too long on a large directory, try turning on the quiet flag with `-q`, only including the necessary file types with `--include` or limiting the lines shown in a preview with `-n`.
78
79
80### What it looks like
81![replace](http://i.imgur.com/qmJjS.png)
82