Supercharge Kscript with Github Copilot
One of the gifts we got from Kotlin 1.3.70 was the ability to effortlessly run a kscript (Kotlin script) from the command line. All you need to do is add the suffix
Seriously, this is all you need to do:
brew install kotlin echo '#!/usr/bin/env kotlin' > hello-world.main.kts chmod +x hello.main.kts ./hello.main.kts # 💥
With this 0 friction setup, there’s little reason for me to ever write a bash script again. I get to write my script in Kotlin — a modern & beautiful language. But slap on Github Copilot and I think we’ve unlocked the holy grail of quick scripting.
To prove my point, I recorded a video live coding a script tackling a moderately involved task. Here’s what I wanted to do:
- Take a directory path as argument
- Make sure it’s valid and has at least one .md (markdown) file in it
- Search the front matter for the “categories” array
- pluck all the categories from all the files
- de-duplicate, sort and print them out
The entire thing took less than 4½ minutes. This is including me figuring out how to get autocomplete working in my IDE, restarting it etc.
Sure I could try to wield a one-line command and do the same thing for this use case but it’s so much nicer in Kotlin.
If you’re intrigued, feel free to checkout my github repo of kscripts. I try to tackle a variety of common use cases in there
P.S: The text snippet I use in there
kscript;; basically gives me a starter hello world template. Not cheating, I promise.