Interactive Crystal in Terminal using fswatch

Adler @ 2015-09-27

We love using Crystal, but a compiled language is hard to be as dynamic as Ruby. This makes developers hard to test out basic ideas with REPL or tools like Ruby irb. However, we can use a well-known C++ tool fswatch to watch and auto-execute the file for us.

Without fswatch

The ordinary way to execute a .cr file is to create a file like and add:

puts "hello world"

and execute:

$ crystal

This process is simple, but will become a problem if it repeats more than 100 times when you are dealing with an algorithm. It can be helpful if we let it run automatically after save. Let get started with fswatch!


I'm using Mac so according to the installation guide it is easy to install with Homebrew:

$ brew install fswatch

Installations on other platforms are also available in the guide.

Starting a watch server

After installation, create file:

$ touch

And watch its change using fswatch:

$ fswatch -o | xargs -n1 -I{} crystal

It will wait for file change and run the crystal command, so keep the window opened.

Edit the file

Go to your text edior and start messing around with the file:

puts "hello world"

On each save, you will see the terminal output.

hello world

Note that it is not Ruby irb so nothing will be displayed if we don't use puts and print methods.

Quick Recap

The formula:

$ fswatch -o [filename] | xargs -n1 -I{} [command]

You don't have to specify a path. Run it like you're in the working directory. Just replace filename and command and it will go well.

And we're done!

More configuration to find in its wiki page. Hope it improves your workflow in programming in Crystal.