← All posts

CLI

watchexec: Run a Command Whenever Files Change

watchexec runs a command whenever files change - a simple, language-agnostic watcher for rebuilds and test loops.

W CLI

A tight feedback loop - save a file, see the result - is one of the biggest quiet productivity multipliers in development, and most build tools bake in their own watcher. watchexec gives you that loop for anything. It watches a directory and runs a command you specify whenever files change, respecting .gitignore, debouncing rapid saves, and optionally restarting long-running processes - all language- and tool-agnostic.

Any Command, on Every Save

You tell it what to run; it runs it whenever relevant files change:

# Re-run tests on any change watchexec -- npm test # Only watch PHP files, clear screen each run watchexec -e php -c -- vendor/bin/phpstan analyse # Restart a server on change watchexec -r -- php -S localhost:8000

Sensible by Default

watchexec ignores gitignored paths so it doesn't fire on build output, debounces bursts of file events into one run, and can restart (rather than stack) a long-lived process. Filters by extension or path let you watch exactly the files that matter for a given task.

Rough Edges and Limitations

  • It just runs commands. No built-in live-reload of a browser; pair it with a tool that does that if you need it.
  • Watcher tuning. Very large trees or network filesystems may need ignore rules to stay efficient.
  • Debounce trade-off. Rapid saves are batched, which is usually right but occasionally not.

For a rebuild-or-retest-on-save loop around any tool that doesn't ship one, watchexec is the simplest, most reliable option.

Internal link suggestion: link "a simple ... watcher" to the entr post, the classic minimal alternative.