/README.md

https://github.com/TankerHQ/ruplacer · Markdown · 140 lines · 95 code · 45 blank · 0 comment · 0 complexity · 721867e801a8b3c57bc6562c2e9da131 MD5 · raw file

  1. [![crates.io image](https://img.shields.io/crates/v/ruplacer.svg)](https://crates.io/crates/ruplacer)
  2. # Ruplacer
  3. Find and replace text in source files:
  4. ```
  5. $ ruplacer old new src/
  6. Patching src/a_dir/sub/foo.txt
  7. -- old is everywhere, old is old
  8. ++ new is everywhere, new is new
  9. Patching src/top.txt
  10. -- old is nice
  11. ++ new is nice
  12. Would perform 2 replacements on 2 matching files.
  13. Re-run ruplacer with --go to write these changes to disk
  14. ```
  15. ## Note
  16. This project was originally hosted on the
  17. [TankerHQ](https://github.com/TankerHQ) GitHub organization, which was
  18. my employer from 2016 to 2021. They kindly agreed to give me back ownership
  19. of this project. Thanks!
  20. ## Installing with cargo
  21. Install `rust` and `cargo`, for example with [rustup](https://rustup.rs/).
  22. Then run:
  23. ```
  24. cargo install ruplacer
  25. ```
  26. ## Alternative installation methods
  27. * Pre-compiled binaries for Linux, macOS, and Windows are available as [assets of the latest release](
  28. https://github.com/dmerejkowsky/ruplacer/releases/tag/v0.6.2).
  29. * `ruplacer` can also be installed from `homebrew`:
  30. ```
  31. $ brew install TankerHQ/homebrew-repo/ruplacer
  32. ```
  33. * `ruplacer` is also on [the Arch Linux User Repository](https://aur.archlinux.org/packages/ruplacer/)
  34. ## Basic usage
  35. ```
  36. ruplacer pattern replacement [path]
  37. ```
  38. If the path is not given, it defaults to the current working directory.
  39. Ruplacer will then walk through every file in `<path>` while honoring `.gitignore` files found on the way.
  40. Binary files and text files containing non-UTF8 characters will be skipped. Then for
  41. every remaining file, it will read the contents, replace all lines matching the
  42. pattern by the replacement, and print the difference.
  43. If you are OK with the replacements, re-run `ruplacer` with the `--go` option to actually write the changes to disk.
  44. ## Regex
  45. By default, `pattern` will be compiled into a [Rust regex](https://docs.rs/regex/1.0.5/regex/).
  46. Note that it's slightly different from Perl-style regular expressions. Also, you must use `$1`, `$2` to reference
  47. groups captured from `pattern` inside `replacement`.
  48. For instance, this replaces 'last, first' by 'first last':
  49. ```
  50. $ ruplacer '(\w+), (\w+)' '$2 $1'
  51. ```
  52. (note the use of single quotes to avoid any processing by the shell)
  53. If you don't want the pattern to be used as a regex, use the `--no-regex` command line flag.
  54. This makes it possible to look for special characters without escaping them:
  55. ```
  56. # This is a regex that matches the letter a
  57. # or the letter o
  58. $ ruplacer '(a|o)' u
  59. - tata toto
  60. + tutu tutu
  61. - (a|o)
  62. + (u|u)
  63. # This is the literal string: '(a|o)'
  64. $ ruplacer --no-regex '(a|o)' u
  65. # or
  66. $ ruplacer '\(a\|o|)' u
  67. - (a|o)
  68. + u
  69. ```
  70. ## Subvert mode
  71. Ruplacer has a `--subvert` option which works across a variety of case styles (lower case, snake case, and so on):
  72. ```
  73. $ ruplacer --subvert foo_bar spam_eggs
  74. Patching src/foo.txt
  75. -- foo_bar, FooBar, and FOO_BAR!
  76. ++ spam_eggs, SpamEggs, and SPAM_EGGS!
  77. ```
  78. ## Filter files by type or glob patterns
  79. Inspired by [ripgrep](https://github.com/BurntSushi/ripgrep), you can also select or ignore certain "file types" or glob patterns:
  80. ```
  81. # Select only C++ files
  82. $ ruplacer old new --type cpp
  83. # Select only *.foo files
  84. $ ruplacer old new --type *.foo
  85. # Select only files that match foo*bar.c
  86. $ ruplacer old new --type foo*bar.c
  87. # Ignore all js files
  88. $ ruplacer old new --type-not js
  89. # Ignore all *.bar files
  90. $ ruplacer old new --type-not *.bar
  91. # Ignore all files that match foo*bar.c
  92. $ ruplacer old new --type-not foo*bar.c
  93. ```
  94. Each "file type" is just a list of glob pattern. For instance: the `cpp` file type matches `*.C`, `*.H`, `*.cc`, `*.cpp` and so on ...
  95. You can see the whole list by using `ruplacer --file-types`.