Giter Site home page Giter Site logo

replace-x's Introduction

Travis status Dependency status devDependency status npm version bettercodehub score Coverage Status

replace-x

replace-x is a command line utility for performing in place search-and-replace on files. It's similar to sed but there are a few differences:

  • Modifies files when matches are found
  • Recursive search on directories with -r
  • Uses JavaScript syntax for regular expressions and replacement strings.
  • Uses XRegExp to provide augmented (and extensible) JavaScript regular expressions.

Install

With node.js and npm:

npm install replace-x -g

You can now use replace-x and search-x from the command line.

Examples

Replace all occurrences of "foo" with "bar" in files in the current directory:

replace-x 'foo' 'bar' *

Replace in all files in a recursive search of the current directory:

replace-x 'foo' 'bar' . -r

Replace-x only in test/file1.js and test/file2.js:

replace-x 'foo' 'bar' test/file1.js test/file2.js

Replace-x all word pairs with "_" in middle with a "-":

replace-x '(\w+)_(\w+)' '$1-$2' *

Replace only in files with names matching *.js:

replace-x 'foo' 'bar' . -r --include="*.js"

Don't replace in files with names matching _.min.js and _.py:

replace-x 'foo' 'bar' . -r --exclude="*.min.js,*.py"

Preview the replacements without modifying any files:

replace-x 'foo' 'bar' . -r --preview

See all the options:

replace-x -h

Search

There's also a search-x command. It's like grep, but with replace-x's syntax.

search-x "setTimeout" . -r

Programmatic Usage

You can use replace from your JS program:

import replace from 'replace-x';

replace({
  regex: 'foo',
  replacement: 'bar',
  paths: ['.'],
  recursive: true,
  silent: true,
});

More Details

Excludes

By default, replace-x and search-x will exclude files (binaries, images, etc) that match patterns in the "defaultignore" located in this directory.

On huge directories

If replace-x 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.

Limitations

Line length

By default, replace-x works on files with lines of 400 characters or less. If you need to work on long lines, gnu sed supports unlimited line length.

Symbolic links

By default, replace-x does not traverse symbolic links for files in directories.

What it looks like

replace-x

replace-x's People

Contributors

xotic750 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

replace-x's Issues

Error using replace-x 1.7.2 in package.json node 10.16.0

in package json build command:

"ng-build": ng build --configuration=qa --project="app-web" --base-href=./ && replace-x 'ABC' 'XYZ' dist/app/index.html",

Error
fs.js:114
throw err;
^

Error: ENOENT: no such file or directory, lstat 'XYZ'
at Object.lstatSync (fs.js:845:3)
at replaceFileSync (/home/develop/repos/node_modules/replace-x/dist/replace-x.js:217:31)
at Array.forEach ()
at replace (/home/develop/repos/node_modules/replace-x/dist/replace-x.js:266:14)
at Object. (/home/develop/repos/node_modules/replace-x/bin/replace-x.js:42:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Feature request: literal "search" option

This is a very powerful and useful package. However, sometimes it is necessary to search for strings that require unpleasant amounts of escaping to make them valid as a regular expression.

Therefore, a simple feature request:

Offer a command line option --literal or something like that, which makes the search just for the exact literal string provided, not a regular expression.

Example of use inside code doesn't quite work

I tried to run the example below and didn't work:

var replace = require('replace-x');

replace({
    regex: 'foo',
    replacement: 'bar',
    paths: ['../test/File.txt']
});

I notice if you change to this works fine:

var replace = require('replace-x').default;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.