Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a configuration file for Scala REPL / SBT Console?

Tags:

scala

sbt

I've been trying to find some sort of a dotfile to put Scala REPL settings and custom function in.

In particular I'm interested in passing it flags like -Dscala.color (enables syntax highlighting), as well as overriding settings like result string truncation:

scala> :power
scala> vals.isettings.maxPrintString = 10000

It would be nice to have these settings apply to both the simple Scala REPL sessions as well as sbt console sessions.

Does such a central configuration place exist for Scala?

like image 423
roldugin Avatar asked Oct 26 '15 05:10

roldugin


4 Answers

Maybe you can use a modernized Scala REPL:

https://lihaoyi.github.io/Ammonite/

like image 111
Twistleton Avatar answered Sep 28 '22 15:09

Twistleton


Poor man's solution: Set yourself an alias

alias myScala='scala -Dscala.repl.axPrintString = 10000' 
like image 33
Andreas Neumann Avatar answered Sep 28 '22 15:09

Andreas Neumann


As mentioned here ~/.sbt/0.13/global.sbt is the global configuration file for sbt. You can change your global settings here, this probably not going to effect REPL but should do work with SBT Console

like image 31
Hüseyin Zengin Avatar answered Sep 28 '22 15:09

Hüseyin Zengin


You mainly asked about property settings, this goes a little beyond that to consider loading a definitions file as well—and isn't much help for Windows—but I thought I'd share in case it's useful:

I've resorted to using a wrapper script saved as ~/bin/scala, to set config properties and load some utility functions:

#!/bin/sh

# The scala REPL doesn't have any config file, so this wrapper serves to set
# some property values and load an init file of utilities when run without
# arguments to enter REPL mode.
#
# If there are arguments, just assume we're running a .scala file in script
# mode, a class or jar, etc., and execute normally.

SCALA=${SCALA:-/usr/local/bin/scala}

if [ "$#" -eq 0 ] && [ -r ~/.config/scala/replinit.scala ]; then
    exec "$SCALA" -i ~/.config/scala/replinit.scala -Dscala.color
else
    exec "$SCALA" "$@"
fi

If you sometimes use Ammonite REPL, as another answer suggests, the utility definitions can be shared by loading them from ~/.ammonite/predef.scala:

try load.exec(ammonite.ops.home/".config"/'scala/"replinit.scala")
catch { case _: Exception => println("=== replrc not loaded! ===") }

I'm not sure about a way to load the init file for sbt console automatically, though—Seth Tisue's comment about the initialize setting is helpful for properties, but using a :load command in a value for initialCommands in console doesn't appear to work.

like image 43
ches Avatar answered Sep 28 '22 15:09

ches