项目作者: dmerejkowsky

项目描述 :
Find and replace text in source files
高级语言: Rust
项目地址: git://github.com/dmerejkowsky/ruplacer.git
创建时间: 2017-11-08T12:49:30Z
项目社区:https://github.com/dmerejkowsky/ruplacer

开源协议:BSD 3-Clause "New" or "Revised" License

下载


crates.io image

Ruplacer

Find and replace text in source files:

  1. $ ruplacer old new src/
  2. Patching src/a_dir/sub/foo.txt
  3. -- old is everywhere, old is old
  4. ++ new is everywhere, new is new
  5. Patching src/top.txt
  6. -- old is nice
  7. ++ new is nice
  8. Would perform 2 replacements on 2 matching files.
  9. Re-run ruplacer with --go to write these changes to disk

Note

This project was originally hosted on the
TankerHQ GitHub organization, which was
my employer from 2016 to 2021. They kindly agreed to give me back ownership
of this project. Thanks!

Installing with cargo

Install rust and cargo, for example with rustup.

Then run:

  1. cargo install ruplacer

Alternative installation methods

  • Pre-compiled binaries for Linux, macOS, and Windows are available as assets of the latest release.

  • ruplacer can also be installed from homebrew:

  1. brew install TankerHQ/homebrew-repo/ruplacer

Basic usage

  1. ruplacer pattern replacement [path]

If the path is not given, it defaults to the current working directory.

Ruplacer will then walk through every file in <path> while honoring .gitignore files found on the way.

Binary files and text files containing non-UTF8 characters will be skipped. Then for
every remaining file, it will read the contents, replace all lines matching the
pattern by the replacement, and print the difference.

If you are OK with the replacements, re-run ruplacer with the --go option to actually write the changes to disk.

Regex

By default, pattern will be compiled into a Rust regex.

Note that it’s slightly different from Perl-style regular expressions. Also, you must use $1, $2 to reference
groups captured from pattern inside replacement.

For instance, this replaces ‘last, first’ by ‘first last’:

  1. ruplacer '(\w+), (\w+)' '$2 $1'

(note the use of single quotes to avoid any processing by the shell)

If you don’t want the pattern to be used as a regex, use the --no-regex command line flag.

This makes it possible to look for special characters without escaping them:

  1. # This is a regex that matches the letter a
  2. # or the letter o
  3. $ ruplacer '(a|o)' u
  4. - tata toto
  5. + tutu tutu
  6. - (a|o)
  7. + (u|u)
  8. # This is the literal string: '(a|o)'
  9. $ ruplacer --no-regex '(a|o)' u
  10. # or
  11. $ ruplacer '\(a\|o|)' u
  12. - (a|o)
  13. + u

Preserving case while replacing

Ruplacer has a --preserve-case option which works across a variety of case styles (lower case, snake case, and so on):

  1. $ ruplacer --preserve-case foo_bar spam_eggs
  2. Patching src/foo.txt
  3. -- foo_bar, FooBar, and FOO_BAR!
  4. ++ spam_eggs, SpamEggs, and SPAM_EGGS!

Filter files by type or glob patterns

Inspired by ripgrep, you can also select or ignore certain “file types” or glob patterns:

  1. # Select only C++ files
  2. $ ruplacer old new --type cpp
  3. # Select only *.foo files
  4. $ ruplacer old new --type *.foo
  5. # Select only files that match foo*bar.c
  6. $ ruplacer old new --type foo*bar.c
  7. # Ignore all js files
  8. $ ruplacer old new --type-not js
  9. # Ignore all *.bar files
  10. $ ruplacer old new --type-not *.bar
  11. # Ignore all files that match foo*bar.c
  12. $ ruplacer old new --type-not foo*bar.c

Each “file type” is just a list of glob pattern. For instance: the cpp file type matches *.C, *.H, *.cc, *.cpp and so on …

You can see the whole list by using ruplacer --type-list.