Catalog / Watchexec Cheatsheet
Watchexec Cheatsheet
A comprehensive cheat sheet for Watchexec, a tool that executes commands on file changes. Includes installation, basic usage, advanced options, and practical examples.
Installation & Basic Usage
Installation
Install Watchexec using your preferred package manager. macOS:
Rust (Cargo):
Linux/Windows: |
Basic Usage
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 |
Watching Specific Files/Directories
You can tell Watchexec to only watch specific files or directories using the Example:
This command watches the |
Advanced Options & Configuration
Filtering File Changes
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 |
Controlling Command Execution
You can control how Watchexec executes your commands using flags like Example (Restart):
If |
Example (Debounce):
This command debounces the execution of |
Signal Handling
Use Example:
This will send |
Practical Examples
Web Development Workflow
Automatically rebuild and reload your web application on code changes. Example (React):
Watches the |
Backend Development Workflow
Automatically restart your backend server on code changes. Example (Node.js):
Watches the |
Running Tests
Automatically run your tests when source files change. Example (Jest):
Watches the |
Troubleshooting & Tips
Common Issues
Too many restarts: Ignoring files: |
Tips and Tricks
Combine with other tools: Use environment variables: |
Configuration Files
For complex configurations, consider using a Watchexec configuration file (e.g., Example
Run with |