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
|
9 | With [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
|
10 |
|
11 | npm install replace -g
|
12 |
|
13 |
|
14 | ## Examples
|
15 |
|
16 | Replace all occurrences of "var" with "let" in files in the current directory:
|
17 |
|
18 | ```
|
19 | replace 'var' 'let' *
|
20 | ```
|
21 |
|
22 | Replace in all files in a recursive search of the current directory:
|
23 |
|
24 | ```
|
25 | replace 'var' 'let' . -r
|
26 | ```
|
27 |
|
28 | Replace only in test/file1.js and test/file2.js:
|
29 |
|
30 | ```
|
31 | replace 'var' 'let' test/file1.js test/file2.js
|
32 | ```
|
33 |
|
34 | Replace all word pairs with "_" in middle with a "-":
|
35 |
|
36 | ```
|
37 | replace '(\w+)_(\w+)' '$1-$2' *
|
38 | ```
|
39 |
|
40 | Replace only in files with names matching *.js:
|
41 |
|
42 | ```
|
43 | replace 'var' 'let' . -r --include="*.js"
|
44 | ```
|
45 |
|
46 | Don't replace in files with names matching *.min.js and *.py:
|
47 |
|
48 | ```
|
49 | replace 'var' 'let' . -r --exclude="*.min.js,*.py"
|
50 | ```
|
51 |
|
52 | Preview the replacements without modifying any files:
|
53 |
|
54 | ```
|
55 | replace 'var' 'let' . -r --preview
|
56 | ```
|
57 |
|
58 | See all the options:
|
59 |
|
60 | ```
|
61 | replace -h
|
62 | ```
|
63 |
|
64 | # More Details
|
65 |
|
66 | ### Search
|
67 | There's also a `search` command. It's like `grep`, but with `replace`'s syntax, and slower!
|
68 |
|
69 | ```
|
70 | search "setTimeout" . -r
|
71 | ```
|
72 |
|
73 | ### Excludes
|
74 | By 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
|
77 | If `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 |
|