spacemod

spacemod is a text search-and-replace tool optimized towards refactoring code. It is very similar to fastmod but extends the regex syntax with support for matching parenthesized expressions.

spacemod ideally allows you to write more naive regexes and worry less about overzealous wildcard matching.

spacemod is still alpha software. Please report any bugs you find.

Example

Let's say you have the following piece of code:

rust vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]

You get the terrible idea of changing all of your x.to_string() calls to String::from(x) instead.

In fastmod, or any regex-based search-and-replace, you'd write:

bash fastmod '"(.*)"\.to_string\(\)' 'String::from("$1")'

You forgot about the greediness of .* and should've maybe written .*? instead. Now your code looks like:

rust vec![String::from(String::from(String::from("foo"), "bar"), "baz")]

Let's try that again. spacemod lets you write:

bash spacemod -S '" (.*) " \.to_string ( )' 'String::from("$1")'

The correct end result looks like:

rust vec![String::from("foo"), String::from("bar"), String::from("baz")]

Spacing out parenthesis and quotes tells spacemod that those tokens, "" and ():

Installation

Check out this repository, install Rust, and run:

bash cargo build --release

Your binary is in ./target/release/spacemod.

Parenthesis matching

spacemod's pattern syntax consists of tokens delimited by a single space each. A token can either be a parenthesis/quote, or a substring of a regex:

{ regex1 } regex2 { regex3 }

If you need to use a literal space anywhere in a regex substring, escape it as \. That also means escaping a regex like \ as \\. Backslashes followed by anything but a space within a regex do not need to be escaped.

spacemod knows the following parenthesis/quotes:

You can extend this list with -p ab where a is the opening parenthesis, and b the closing counterpart. See --help for more information.

Alternatives

You may find the following tools useful if spacemod is doing too much or too little for you. The primary focus of this list is on editor/IDE-independent tools, and preferably on those which can be composed into more complex shell-scripts.

Other tools in the same space

License

Licensed under MIT, see ./LICENSE.