Weichu Liu
Weichu Liu

Reputation: 143

Predefine routine in scala sbt console?

I am recently writing scala, using sublimetext to write *.scala and run sbt in another window.

When I use sbt console to debug, every time I need to manually import packages and do routines.

It's really annoying to repeat copying codes again and again after I recompile and restart console.

I wonder is there a way to set a predefined environment to do it manually?

Another question I want to know is in console REPL can I auto complete lines by the initial characters? For example in python IDLE one can use Alt+p to search history. It's really convenience.

Upvotes: 0

Views: 959

Answers (2)

Rob Starling
Rob Starling

Reputation: 3908

Also probably worth noting is that you can load scripts into the repl, too:

$ cat > guessing.txt
import scala.util.Random.nextInt
def guess() {
  val r = nextInt()
  println("I randomly picked %d.".format(r))
}

$ sbt console
scala> :load guessing.txt
Loading guessing.txt...
import scala.util.Random.nextInt
guess: ()Unit

scala> guess
I randomly picked -630907258.

:help is your friend.

Upvotes: 4

senia
senia

Reputation: 38045

Initial commands

You could add this line to your build.sbt:

initialCommands in console := "import scalaz._, Scalaz._"

Result:

$ sbt console
[info] Starting scala interpreter...
[info]
import scalaz._
import Scalaz._
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

REPL auto complete

You could tap Tab key to get limited auto complete:

scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)

scala> res0.ap

First Tab - auto complete and options:

scala> res0.apply
apply         applyOrElse

Second Tab - method signature:

scala> res0.apply
                       def apply(n: Int): A

History

You could use Ctrl+R to search in history (including history from previous REPL sessions). Use addition Ctrl+R to get next result.

Upvotes: 6

Related Questions