TIL: entr - Run commands when files change

As I described in the TIL about Vim and environment variables with :make when I am programming I usually prefer to stay focused on writing the code, reading and rereading it, finishing a step and hitting <F4> or <F5> in Vim to then fix the errors found.

In that TIL I mentioned bacon and cargo-watch (the repository was archived), which the Rust community usually uses to check the code as files change.

I know they have many features, work on several operating systems, etc... But for those who, like me, like simplicity there is entr.

I have used entr since 2015, when I needed to send some files from one server to another whenever they were modified.

On OpenBSD just pkg_add entr =]

Examples

Rust

Check the project when saving any .rs file:

find src -name '*.rs' | entr cargo check

Run the project:

find src -name '*.rs' | entr cargo run

Run the tests:

find src -name '*.rs' | entr cargo test

OCaml

Build and run with dune:

find . -name '*.ml' -o -name '*.mli' | entr dune build

Run the tests:

find . -name '*.ml' | entr dune test

Shell Script

Check the script with shellcheck:

echo meu_script.sh | entr shellcheck /_

The /_ is replaced by the name of the file that triggered the event.

TypeScript

Compile with tsc:

find src -name '*.ts' | entr npx tsc --noEmit

Run the tests:

find src -name '*.ts' -o -name '*.tsx' | entr npm test

Useful flag: -c

Use -c to clear the screen before each run:

find src -name '*.rs' | entr -c cargo check

Useful flag: -r

Use -r to restart the process (useful for servers):

find src -name '*.rs' | entr -r cargo run

For more information man entr