This week’s post is a bit of an experiment: It’s even nerdier than usual, and has a companion video on YouTube. Please let me know whether you like or dislike those things.
Coding is faster, less error prone, and frankly a lot more fun when you can interact with the functions and objects you’re developing as you develop them. Seeing your code come to life, and correcting its behavior as it grows—teaching it not to make the same mistake twice—is a joyous and rewarding experience, as well as a powerful tool in rapid development. That’s the insight behind everything from read-eval-print loops (REPLs) to Smalltalk-80 to Hot Module Replacement (HMR).
It’s alive!
—Victor Frankenstein
Unix has a general purpose tool that lets you see real time updates to the output of anything you can express as a simple run-and-done pipeline with plain text output, and it’s called watch. (“Plain text” is a bit of an archaic phrase. Watch’s greatest weakness is its poor support for non-ASCII characters.) Watch runs a given snippet of shell script and shows the output in the terminal; then waits a couple of seconds (by default), runs the command again, and smoothly updates the terminal output, optionally highlighting parts of the output that changed since the previous run. You could use it for any number of nifty little tasks that are probably better served by special purpose tools (like ‘watch tree’ to show a real-time display of folder contents or ‘watch date’ to make a simple clock), but the killer app is showing you the output of your code as you’re writing it. It’s like having a conversation with your code.
If your code won’t compile, the watch output shows you the error; not as a little warning icon or stop sign or squiggly underline that you’re supposed to mouse over, but as full glorious detail in a dedicated “watch window.” If the code compiles but hits a runtime error, watch shows you that, too. If all goes according to plan, you see your code’s output immediately, as you change it. Some build tools now have ‘watch’ subcommands, and of course IDEs are constantly checking your code in the background, but the feedback loop is (in my anecdotal experience) dramatically tighter with a plain old watch command.
It’s tough to explain in writing why I like this tool so much, so I’ve shared a twelve minute screencast showing how I typically use it. If you code but aren’t familiar with watch, give it a shot, or try an alternative like Watchman. If you think of other real-world uses for watch, please add them to the comments.
How did your coerece vim to write with every letter change? out-of-the-box autowrite doesn't do that ....