fastmod
Perform bulk code changes quickly, safely
TLDR
Replace a regex in all files of the current directory, ignoring files on .ignore and .gitignore
Replace a regex in case-insensitive mode in specific files or directories
Replace a regex in a specific directory in files filtered with a case-insensitive glob pattern
Replace for an exact string in .js or JSON files
Replace for an exact string without prompt for a confirmation (disables regex)
Replace for an exact string without prompt for a confirmation, printing changed files
SYNOPSIS
fastmod [OPTIONS] PATTERN REPLACEMENT [PATH...]
PARAMETERS
PATTERN
The regular expression or literal string to search for. Must be provided.
REPLACEMENT
The string to replace matches with. Backreferences like $1 or $P<name> are supported for regex patterns. Must be provided.
[PATH...]
One or more file or directory paths to search within. If omitted, fastmod searches recursively from the current directory.
-p, --preview
Show interactive diffs before applying changes, allowing per-file approval or rejection. Highly recommended for safety.
-d, --dry-run
Perform a trial run with no changes written to disk. Only shows which files would be modified.
-i, --ignore-case
Perform case-insensitive matching. Overrides --match-case.
-m, --match-case
Perform case-sensitive matching (default for regex, but useful to explicitly specify).
-s, --literal-string
Treat PATTERN as a literal string rather than a regular expression. Useful for simple exact matches.
--substitute-first
Replace only the first occurrence of PATTERN found on each line.
--substitute-all
Replace all occurrences of PATTERN found on each line (this is the default behavior).
-e, --extension <EXT>
Only process files with the specified file extension(s). Can be used multiple times (e.g., -e rs -e go
).
--exclude-dir <REGEX>
Exclude directories whose paths match the provided regular expression. For example, --exclude-dir 'build|node_modules'
.
-n, --no-backups
Do not create .fastmod_backup
files before modifying files. Use with caution.
DESCRIPTION
fastmod is a command-line tool developed by Google, written primarily in Rust, designed for quickly and safely applying large-scale, complex code modifications across extensive codebases. Unlike traditional line-by-line tools like sed or awk, fastmod processes files in memory, providing significant performance benefits for large projects. It offers a powerful find-and-replace mechanism based on regular expressions, crucially featuring an interactive "preview" mode to review proposed changes before committing them. This interactive approval process, coupled with its ability to handle multiple substitutions and its focus on correctness, makes it an ideal tool for large-scale refactoring, upgrading dependencies, or performing API migrations across thousands of files. Its efficiency and safety features are particularly valuable in automated code transformation workflows within CI/CD environments.
CAVEATS
While fastmod is designed for safety, especially with its --preview mode, it's still a powerful tool that makes irreversible changes to files. Always ensure you have backups or are working within a version-controlled environment (like Git) before executing changes. The regular expression syntax might differ slightly from other engines (e.g., PCRE) as it uses the Rust regex library.
INTERACTIVE PREVIEW MODE
The --preview flag is fastmod's most critical safety feature. When used, it presents a diff for each file that would be modified and prompts the user to accept (y), reject (n), or skip (s) the change, or quit (q). This allows for granular control and prevents unintended modifications, making it suitable for complex refactoring tasks.
BACKUP MECHANISM
By default, fastmod creates backup files (e.g., filename.ext.fastmod_backup
) alongside the original file before applying any changes. This provides a simple rollback mechanism in case of errors. This behavior can be disabled using the --no-backups flag, but it is generally recommended to keep backups enabled or rely on version control.
REGULAR EXPRESSION SYNTAX
fastmod leverages the Rust regex library. This library aims for compatibility with Perl-like regexes but has some differences and limitations. Users familiar with PCRE or GNU grep regexes should consult the Rust regex documentation for specific syntax details, especially regarding advanced features or edge cases.
HISTORY
fastmod was developed by Google as an internal tool to facilitate large-scale code transformations and refactoring across their extensive codebases. Recognizing its utility, Google open-sourced the project on GitHub, making it available to the wider development community. Its development in Rust reflects a focus on performance, memory safety, and concurrency, making it particularly efficient for modern, large-scale software projects.