By Alvin Alexander. Last updated: April 22, 2024
As a little note today, if you want to create a little startup file for the Scala Ammonite REPL, the way you do that on Mac and Unix/Linux systems is to create or edit this file:
~/.Ammonite/predef.sc
My current contents look like this, including an Ammonite REPL help
command (function) that I defined:
repl.prompt() = "amm> " import sys.process.* // or use CTRL-L def clear = val rez = "clear".! () def cmd(cmd: String) = cmd.!! def ls(dir: String) = println(cmd(s"ls -al $dir")) def help = println("\n=== MY CONFIG ===") "cat /Users/Al/.ammonite/predef.sc".! def reset = repl.sess.load() // HACK: keeps ammonite from crashing after 'reset' is called // def hack = repl.sess.save() /* # RESOURCES - https://ammonite.io - https://ammonite.io/#AmmoniteCookbook # CONFIGURATION ~/.ammonite/predef.sc # IMPORT A JAR import $cp.foo.`simpletest_3-0.3.0.jar` // in subdir 'foo' import $cp.`simpletest_3-0.3.0.jar` // in same dir import com.alvinalexander.simpletest.SimpleTest.* # LOAD A SCRIPT (MYSCRIPT.SC) import $file.MyScript // current dir import $file.myfolder.MyScript // myfolder subdir # PASTE MULTIPLE LINES { // your code here ... } # SHOW show(Seq.fill(20)(100)) show(Seq.fill(20)(100), height = 3) # IMPORT IVY import $ivy.`org.jsoup:jsoup:1.7.2` import org.jsoup.* import scala.jdk.CollectionConverters.* val doc = Jsoup.connect("http://en.wikipedia.org/").get() doc.select("h1") doc.select("h2").asScala.toSeq.map(_.text) # SHOW SOURCE source(Some(1).map _) // may be 'src' command # TIME time{ls!} */
My help
command just shows the contents of this predef file, which is why I include all of the comments in here
If you ever want to create a Scala Ammonite REPL startup file, I hope this predef example is helpful.