Install Watchexec using your preferred package manager.
macOS:
brew install watchexec
Rust (Cargo):
cargo install watchexec
Linux/Windows:
Download pre-built binaries from the GitHub releases page.
A comprehensive cheat sheet for Watchexec, a tool that executes commands on file changes. Includes installation, basic usage, advanced options, and practical examples.
Install Watchexec using your preferred package manager. macOS:
Rust (Cargo):
Linux/Windows: |
The most basic use case: running a command when any file in the current directory changes. Example:
This will print ‘File changed!’ to the console every time a file in the current directory is modified. |
Specifying the command to run
Runs |
You can tell Watchexec to only watch specific files or directories using the Example:
This command watches the |
Watchexec allows you to filter which file changes trigger a command execution using extensions or ignore patterns. Example (Extensions):
This command only triggers the |
Example (Ignore Patterns):
This command ignores changes within the |
You can control how Watchexec executes your commands using flags like Example (Restart):
If |
Example (Debounce):
This command debounces the execution of |
Use Example:
This will send |
Automatically rebuild and reload your web application on code changes. Example (React):
Watches the |
Automatically restart your backend server on code changes. Example (Node.js):
Watches the |
Automatically run your tests when source files change. Example (Jest):
Watches the |
Too many restarts: Ignoring files: |
Combine with other tools: Use environment variables: |
For complex configurations, consider using a Watchexec configuration file (e.g., Example
Run with |