groovysh - the Groovy repl-like shell

1. Groovy : Groovy Shell

The Groovy Shell, aka. groovysh is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.

1.1. Features

  • Rich cross-platform line editing, history and completion thanks to JLine3.

  • ANSI colors (prompt, exception traces, etc).

  • Simple, yet robust, command system with online help, user alias support and more.

1.2. Command-line Options and Arguments

The shell supports several options to control various features.

> groovysh --help

Usage: groovysh [options] [...]
The Groovy Shell, aka groovysh, is a command-line application which allows easy
access to evaluate Groovy expressions, define classes and run simple experiments.
  -c, --encoding=<CHARSET>   The encoding to use
  -C, --color[=<FLAG>]       Enable or disable use of ANSI colors
      -cp, -classpath, --classpath
                             Specify where to find the class files - must be
                               first argument
  -d, --debug                Enable debug output
  -D, --define=<name=value>  Define a system property
  -e, --evaluate=<CODE>      Evaluate the code first when starting interactive
                               session
  -h, --help                 Display this help message
      -pa, --parameters      Generate metadata for reflection on method
                               parameter names
      -pr, --enable-preview  Enable preview Java features (jdk12+ only) - must
                               be after classpath but before other arguments
  -q, --quiet                Suppress superfluous output
  -T, --terminal=<TYPE>      Specify the terminal TYPE to use
  -v, --verbose              Enable verbose output
  -V, --version              Display the version

Here is an example showing the -q|--quiet and -e|--evaluate options:

Exception

1.3. Repl model

The Groovy Shell is a Read-Eval-Print Loop (REPL) which allows you to interactively evaluate Groovy expressions and statements, define classes and other types, invoke commands, and run simple experiments.

When you input a line, the shell will try to determine if the input you have given is a complete valid expression, statement, or definition. If not complete, it will prompt you for more input. If it is complete, it will execute the input, and print the result, if any, to the console. Each input you enter is executed in isolation.

There are some exceptions to this conceptual model. Any import statements entered will be remembered and used for all subsequent evaluations. Similarly, with some caveats we’ll discuss next, any previously defined classes, methods, and potentially variables will be available.

The shell has the concept of shared variables. Given that subsequent statements are run in isolation, you should store any results needed for later use in shared variables.

Many Groovy tutorials and examples use the def keyword or a type to define variables. Script examples might distinguish between local variables with a type and script binding variables where no type, nor the def or var type placeholders, are given. The script binding is the exact equivalent to the shell’s shared variables.

Because such statements are so common, the shell has a special mode called interpreter mode which allows you to use typed variables. The following table summarizes the differences between the two modes:

interpreterMode off on

imports

remembered

types

available

methods

converted to closure shared variables

remembered

shared variables

available

local variables

forgotten

remembered

Conceptually, for things that are remembered in the above table, it is as if you included the related code at the start of each of your inputs.

The shell also has the concept of the "current buffer". This is the collection of all type, method, imports, and variable definitions. The ordering of these snippets is retained, which is important for the shell, since it is only executing one snippet at a time, it typically requires everything needed for a snippet to be pre-defined. This is different to a compiler which might compile multiple source files at once, and resolve references between types as needed.

1.4. Evaluating Expressions

1.4.1. Simple Expressions

groovy> println "Hello"
Hello

1.4.2. Evaluation Result

When a complete expression is found, it is compiled and evaluated. The result of the evaluation is stored into the _ variable.

1.4.3. Multi-line Expressions

Multi-line/complex expressions (like closure or class definitions) may be defined over several lines. When the shell detects that it has a complete expression it will compile and evaluate it.

1.4.4. Defining types

You can define all the normal types, e.g. a class:

groovy> class Foo {
add: } >   def bar() {
add: }}>     println "baz"
add: }}>   }
add: } > }

And use it in the normal way.

groovy> foo = new Foo()
groovy> foo.bar()
baz

Defined classes are known to the shell and can be used in completion:

Define Class

1.4.5. Variables

Shell variables are all untyped (i.e. no def or other type information).

This will set a shell variable:

foo = "bar"

But, this will evaluate a local variable and will not be saved to the shell’s environment:

def foo = "bar"

This behavior can be changed by activating interpreter mode.

Variables definitions having the same name as existing definitions will replace the old definition with the new one.

1.4.6. Methods

Methods can be defined in the shell, and will be saved for later use.

Defining a method is easy:

groovy> def hello(name) {
add: }>   println("Hello $name")
add: }> }

And then using it is as one might expect:

groovy> hello "Jason"
Hello Jason

If a method definition has the same signature as an existing definition, the old definition will be replaced with the new one.

When in interpreterMode, methods are remembered and given as extra statements when executing the next input.

When not in interpreterMode, the shell internally creates a closure to encapsulate the method and stores it in the shared variables. In this case, variables and methods share the same namespace.

1.4.7. Exceptions

If an exception is thrown, the shell will print the exception message and set a special exception shared variable.

Exception

1.5. Commands

The shell has a number of different commands, which provide rich access to the shell’s environment.

Commands all have a name, e.g. /help and /prnt. Commands may also have some predefined system aliases, e.g. /h. Users may also create their own aliases. This section will list commands in alphabetical order, but you can also use the /help command to list the available commands:

groovy> /help
    /!           execute shell command
    /alias       create command alias
    /cat         concatenate and print FILES
    /cd          change directory
    /classloader display/manage Groovy classLoader data
    /clear       clear terminal
    /colors      view 256-color table and ANSI-styles
    /console     launch Groovy console
    /date        display date
    /del         delete console variables, methods, classes and imports
    /doc         open document on browser
    /echo        echos a value
    /exit        exit from app/script
    /grab        add maven repository dependencies to classpath
    /grep        search for PATTERN in each FILE or standard input.
    /head        display first lines of files
    /help        command help
    /highlighter manage nanorc theme system
    /history     list history of commands
    /imports     show/delete import statements
    /inspect     display/browse object info on terminal/object browser
    /keymap      manipulate keymaps
    /less        file pager
    /load        load state/a file into the buffer
    /ls          list files
    /methods     show/delete methods
    /nano        edit files
    /pipe        create/delete pipe operator
    /prnt        print object
    /pwd         print working directory
    /reset       clear the buffer
    /save        save state/the buffer to a file
    /setopt      set options
    /setvar      set lineReader variable value
    /show        list console variables
    /slurp       slurp file or string variable context to object
    /sort        writes sorted standard input to standard output.
    /tail        display last lines of files
    /ttop        display and update sorted information about threads
    /types       show/delete types
    /unalias     remove command alias
    /unsetopt    unset options
    /vars        show/delete variable declarations
    /wc          word, line, character, and byte count
    /widget      manipulate widgets

While in the interactive shell, you can ask for help for any command to get more details about its syntax or function. You can use /help <command> or <command> --help. Here is an example of what happens when you ask for help for the /help command:

groovy> /help /help
help -  command help
Usage: help [TOPIC...]
  -? --help                      Displays command help
     --groups                    Commands are grouped by registries
  -i --info                      List commands with a short command info

1.5.1. /alias

Create an alias for a commandline fragment. The fragment could be Groovy code or a shell command. When evaluating a commandline, the alias will be replaced with the fragment:

Usage of /alias

The /cat command is already available. As can be seen here, aliases are replaced before command selection, so with the above alias in place, we actually invoke /less -F command, which has syntax highlighting, instead of the builtin /cat which doesn’t.

The fragment is expected to be at the start of a line but other text may follow:

More /alias examples

Aliases are persisted in a .groovysh/aliases.json file in the user home directory.

See also the /unalias command, which allows aliases to be removed.

1.5.2. /cat

Concatenate and print files:

groovy> /cat answers.json answers.toml
{
    "answer": {
        "universe":
        42
    }
}
[answer]
universe = 42

See also the /less command, which can be used to display the contents of a file a page at a time with syntax highlighting.

1.5.3. /cd

Change the current working directory. groovysh keeps track of a current working directory. You can view it with /pwd and change it with /cd.

The groovysh file commands (/cat, /less, /head, /slurp, /load, /ls, etc.) will use the current working directory.

The JVM running groovysh also has its own concept of the current working directory, which is the directory in which groovysh was started, so /!ls and /ls may not show the same files. Using /!cd only changes the working directory within the context of that one shell command.

There is a predefined shared variable PWD which can assist with invoking shell commands. So, on unix platforms, /ls will list the same files as /!ls $PWD.

See also the /pwd command, which prints the current working directory.

Cd

1.5.4. /classloader

Display and manage the Groovy classloader data.

Let’s /grab a dependency, define a class using it, and then use the /classloader command to see the classloader data:

Classloader

1.5.5. /clear

Clears the screen.

1.5.6. /colors

Displays the available colors.

Colors

This can be useful when configuring the console options, for example the PRNT_COLORS:

Colors for /prnt

As this example shows, you can mix color names (like black and magenta), theme colors (like NUMBER), and color numbers (like 75).

Here is an example for HELP_COLORS:

Colors for --help

Here is an example for LS_COLORS:

Colors for --help

See the REPL Console Customization documentation for more details about such customization.

1.5.7. /console

Displays the current buffer in the GroovyConsole.

Console

1.5.8. /date

Groovy has numerous classes for working with dates and time.

Here are some examples using the legacy date/calendar classes:

groovy> println new Date()
Tue Aug 19 14:35:22 AEST 2025
groovy> printf '%1ta %1$tb %1$td %1$tT %1$tZ %1$tY%n', new Date()
Tue Aug 19 14:35:22 AEST 2025
groovy> println Calendar.instance.time
Tue Aug 19 14:35:22 AEST 2025
groovy> println new Date().format('dd-MMM-yyyy HH:mm')
19-Aug-2025 14:35

Here are some examples using the java.time classes:

groovy> println LocalDateTime.now()
2025-08-19T14:17:35.383665
groovy> println Instant.now()
2025-08-19T04:17:35.590829Z
groovy> printf '%1ta %1$tb %1$td %1$tT %1$tY%n', LocalDateTime.now()
Tue Aug 19 14:47:35 2025
groovy> println LocalDateTime.now().format('dd-MMM-yyyy HH:mm')
19-Aug-2025 14:47

The /date command is an alternative for those familiar with the unix date commandL:

groovy> /date -R
Tue, 19 Aug 2025 14:17:05 +10
groovy> /date --rfc-3339=seconds
2025-08-19 14:17:05+10
groovy> /date --rfc-3339=time
Aug Tue 19 14:17:05 2025

1.5.9. /del

Deletes objects from the shell. The command looks for any variable, method, type, shared data, or import with the given name and deletes it.

Here we create two variables, x and y, then delete the x variable:

Console

See also, the /vars, /methods, /types, and /imports commands. These offer a -d|--delete option to delete the respective objects with completion.

1.5.10. /doc

Open documentation in a browser.

Documentation can be found for Groovy classes like this:

groovy> /doc groovy.lang.Tuple

For Java classes, the /doc command will look for the class in the JDK documentation as well as in the groovy-jdk if Groovy has any extensions for that class:

groovy> /doc java.util.List

Documentation is also extensible, so you can add your own documentation using regex patterns. For example, if you add the following line to your ~/.groovy/groovysh_init.groovy file:

CONSOLE_OPTIONS.docs.groovy='https://groovy-lang.org/documentation.html'
CONSOLE_OPTIONS.docs.'org/jline/.*'='https://www.javadoc.io/doc/org.jline/jline/latest/'

Then, the following commands will open the Groovy documentation page and the JLine documentation for Terminal respectively in your browser:

groovy> /doc groovy
groovy> /doc org.jline.terminal.Terminal

1.5.11. /echo

The /echo command outputs its arguments to the console. Arguments are output verbatim, but variable expansion is also supported.

Echo

See also the /prnt command, which is similar but may perform additional formatting on the output(s).

1.5.12. /exit

Exit the shell.

This is the only way to exit the shell. Well, you can still CTRL-Z on unix platforms, but things like CTRL_C are trapped. (See JLine3 documentation for more details.)

1.5.13. /grab

Grab a dependency (Maven, Ivy, etc.) from Internet sources or cache, and add it to the Groovy Shell environment.

groovy> /grab org.apache.commons:commons-collections4:4.5.0
groovy> import org.apache.commons.collections4.bidimap.TreeBidiMap
groovy> TreeBidiMap t = [apple: 'red']
{apple=red}
groovy> t.inverseBidiMap()
{red=apple}

Completion is available. Currently, completion options are populated by known artifacts in the local Maven (~/.m2) and Grape (~/.groovy/grapes) repositories. In the future, completion from a remote repositories may be supported.

groovy> /grab org.apache.commons:commons-<TAB>
commons-collections4:     commons-dbcp2:            commons-lang3:
commons-compress:         commons-digester3:        commons-math3:
commons-configuration2:   commons-email:            commons-parent:
commons-crypto:           commons-exec:             commons-pool2:
commons-csv:              commons-imaging:          commons-text:

This command can be given at any time to add new dependencies.

1.5.14. /grep

The /grep command searches for patterns in files or standard input.

Grep usage

1.5.15. /head

The /head command displays the start of a file.

groovy> /head -n2 fruit.txt
apple
banana

1.5.16. /history

Display, manage and recall edit-line history. The /history command has numerous options which let you list (with various options), save, read, and clear the edit-line history.

groovy> /history -?
history -  list history of commands
Usage: history [-dnrfEie] [-m match] [first] [last]
       history -ARWI [filename]
       history -s [old=new] [command]
       history --clear
       history --save
  -? --help                      Displays command help
     --clear                     Clear history
     --save                      Save history
  -m match                       If option -m is present the first argument is taken as a pattern
                                 and only the history events matching the pattern will be shown
  -d                             Print timestamps for each event
  -f                             Print full time date stamps in the US format
  -E                             Print full time date stamps in the European format
  -i                             Print full time date stamps in ISO8601 format
  -n                             Suppresses command numbers
  -r                             Reverses the order of the commands
  -A                             Appends the history out to the given file
  -R                             Reads the history from the given file
  -W                             Writes the history out to the given file
  -I                             If added to -R, only the events that are not contained within the internal list are added
                                 If added to -W or -A, only the events that are new since the last incremental operation
                                 to the file are added
  [first] [last]                 These optional arguments may be specified as a number or as a string. A negative number
                                 is used as an offset to the current history event number. A string specifies the most
                                 recent event beginning with the given string.
  -e                             Uses the nano editor to edit the commands before executing
  -s                             Re-executes the command without invoking an editor

Here is an example of using the /history command:

Usage of the /history command

1.5.17. import

Add a custom import which will be included for all shell evaluations.

groovy> import java.util.concurrent.BlockingDeque

This command can be given at any time to add new imports.

Completion is available and prompts a level at a time using the package structure of all known classes.

groovy> import java.util.concurrent.<TAB>
others
atomic                        locks
Classes
AbstractExecutorService       ConcurrentSkipListMap         ForkJoinPool
ArrayBlockingQueue            ConcurrentSkipListSet         ForkJoinTask
...

Once an import statement has been executed, relevant classes will become available for completion:

Import

1.5.18. /imports

You can use this to list and delete existing imports.

groovy> /imports
import java.util.concurrent.BlockingQueue

1.5.19. /inspect

Display or browse object info on the terminal or object browser.

Usage of the /inspect command

Using the --gui/-g option displays the object in Groovy’s object browser:

Usage of the /inspect command

1.5.20. /less

Display the contents of a file (usually a page at a time). Formatting of common file types is supported.

Usage of the /less command

If no filename is given, the contents of the current buffer are displayed.

1.5.21. /load

Load a file into the buffer.

If no filename is given as an argument, the current shared variables are loaded from the .groovy/groovysh.ser file in the user home directory.

1.5.22. /ls

The /ls command lists files.

groovy> /ls subprojects
                     groovy-dateutil      groovy-swing
..                   groovy-docgenerator  groovy-templates
binary-compatibility groovy-ginq          groovy-test
groovy-all           groovy-groovydoc     groovy-test-junit5
groovy-ant           groovy-groovysh      groovy-testng
groovy-astbuilder    groovy-jmx           groovy-toml
groovy-binary        groovy-json          groovy-typecheckers
groovy-bom           groovy-jsr223        groovy-xml
groovy-cli-commons   groovy-macro         groovy-yaml
groovy-cli-picocli   groovy-macro-library performance
groovy-console       groovy-nio           stress
groovy-contracts     groovy-servlet       tests-preview
groovy-datetime      groovy-sql

Wildcard support is available, so you can use /ls *.groovy to list all Groovy files in the current directory. File patterns which involve asterisks as well as slashes can sometimes be confused with Groovy’s comment syntax, so quote the pattern if in doubt:

groovy> /ls '**/bu*.gradle'
bootstrap/build.gradle                        subprojects/groovy-datetime/build.gradle      subprojects/groovy-swing/build.gradle
build-logic/build.gradle                      subprojects/groovy-dateutil/build.gradle      subprojects/groovy-templates/build.gradle
build.gradle                                  subprojects/groovy-docgenerator/build.gradle  subprojects/groovy-test-junit5/build.gradle
gradle/build-scans.gradle                     subprojects/groovy-ginq/build.gradle          subprojects/groovy-test/build.gradle
subprojects/binary-compatibility/build.gradle subprojects/groovy-groovydoc/build.gradle     subprojects/groovy-testng/build.gradle
subprojects/groovy-all/build.gradle           subprojects/groovy-groovysh/build.gradle      subprojects/groovy-toml/build.gradle
subprojects/groovy-ant/build.gradle           subprojects/groovy-jmx/build.gradle           subprojects/groovy-typecheckers/build.gradle
subprojects/groovy-astbuilder/build.gradle    subprojects/groovy-json/build.gradle          subprojects/groovy-xml/build.gradle
subprojects/groovy-binary/build.gradle        subprojects/groovy-jsr223/build.gradle        subprojects/groovy-yaml/build.gradle
subprojects/groovy-bom/build.gradle           subprojects/groovy-macro-library/build.gradle subprojects/performance/build.gradle
subprojects/groovy-cli-commons/build.gradle   subprojects/groovy-macro/build.gradle         subprojects/stress/build.gradle
subprojects/groovy-cli-picocli/build.gradle   subprojects/groovy-nio/build.gradle           subprojects/tests-preview/build.gradle
subprojects/groovy-console/build.gradle       subprojects/groovy-servlet/build.gradle
subprojects/groovy-contracts/build.gradle     subprojects/groovy-sql/build.gradle

1.5.23. /nano

Edit files or the current buffer.

The /nano command has numerous options:

Help for the /nano command

You can use the /nano command to edit files or the current buffer:

Using the /nano command

If editing the current buffer, when you exit and then save, the buffer will be reloaded with the edited contents.

1.5.24. /pipe

The /pipe command lets you create and delete custom pipe operators.

There are several builtin pipe and pipe-like operators:

  • The pipe and operator (|&&) executes the right hand side only if the execution of the left hand side succeeds

  • The pipe or operator (|||) executes the right hand side only if the execution of the left hand side fails

  • The pipe flip operator (|;) flips the left and right hand side expressions

  • The output redirect operator (|>) redirects the output to a file

  • The append output redirect operator (|>>) redirects the output by appending to a file

For the pipe and and or operators, succeeds means executing without throwing an exception and returning a non-null, non-empty, non-zero result, i.e. true according to Groovy truth.

Here are some examples for and and or:

usage of the builtin pipes

Pipe operators are handled by the shell. The left and right hand sides will be either repl commands or code passed to the Groovy engine for execution.

The pipe flip operator is most commonly used with commands:

groovy> /widget -l |; /prnt -s JSON
[
    "_autosuggest-end-of-line (_autosuggest-end-of-line)",
    "_autosuggest-forward-char (_autosuggest-forward-char)",
    ...
]

It is less common, but it can also be used with code:

groovy> 4 |; 8 -
4
groovy> 4 |; 8 *
32
groovy> 4 |; 8 /
2
groovy> 4 |; 8 +
12
groovy> 4 |; 8 %
0
groovy> 4 |; 8 **
4096

You can also define your own custom pipe operators, defined using /pipe [OPERATOR] [PREFIX] [POSTFIX], as these examples show:

usage of the /pipe command

JLine functionality includes support for named pipes and pipe aliases.

Here is a named pipe for finding fruit with small names:

groovy> /pipe smallFruit '.findAll{ it.size() <' '}'
groovy> fruit | smallFruit 6
[apple, date]

Here is a pipe alias:

groovy> /alias sizes '|. it.size()'
groovy> fruit | sizes
[5, 6, 6, 4, 10]

The pipe alias always has the pipe it is aliasing at the start, |. in the above example.

Care should be taken when using named pipes along with Groovy code that uses Groovy’s pipe operator (|), AKA binary OR operator, as this can lead to conflicts.

Is the expression x | y the binary OR of two variables, or making use of a named pipe with name y?

Spaces in Groovy are normally not significant, but the Groovy Shell is using JLine console functionality which requires spaces around the pipe operator before a named pipe and is bracket aware. So, x| y, x |y, x|y, and (x | y) will be passed to Groovy for execution. The Groovy Shell also passes x | y to Groovy if y is not a known named pipe.

groovy> x = 4
groovy> y = 8
groovy> x | y
12
groovy> /pipe y '+1' ''
groovy> x | y
5

Getting back to how you can avoid conflicts, one convention you could use to avoid conflicts is to use brackets or avoid spaces when wanting the binary OR operator, and use spaces and no brackets when wanting to use a named pipe.

Another alternative, is to define a custom pipe name instead of a named pipe:

groovy> /pipe |smallFruit '.findAll{ it.size() <' '}'
groovy> fruit |smallFruit 6
[apple, date, fig, grape]
groovy> /pipe
smallFruit  [.findAll{ it.size() <, }]
|smallFruit [.findAll{ it.size() <, }]

Here, the custom pipe named |smallFruit, is different to the named pipe smallFruit. The former mustn’t have a space, the latter must have one. If you use only the first of these, you can have a convention of using spaces when you want a binary OR.

As a final example, let’s use a custom pipe with the output redirection operators to find fruit with large names:

usage of the /pipe command

1.5.25. /prnt

The /prnt command outputs its argument to the console. Both variable expansion and formatting are supported.

usage of the /prnt command

See also the /echo command, which is similar but takes multiple arguments. It also supports variable expansion but doesn’t support formatting.

1.5.26. /pwd

The /pwd command prints the current working directory.

groovy> /pwd
/Users/paulk/Projects/groovy

1.5.27. /reset

Clears the current buffer and shared variables.

1.5.28. /save

Saves the buffer’s contents to a file.

If no filename is given as an argument, the current shared variables are saved into the .groovy/groovysh.ser file in the user home directory.

1.5.29. /setopt

Set options.

groovy> /setopt -?
setopt -  set options
Usage: setopt [-m] option ...
setopt
-? --help                      Displays command help
-m                             Use pattern matching

If no option is given, the current set options are displayed.

groovy> /setopt
disable-event-expansion
use-forward-slash
insert-bracket
no-empty-word-options
groovy>

See also /unsetopt to unset options.

1.5.30. /setvar

Set linereader variable values.

groovy> /setvar
history-file: /Users/paulk/.groovy/groovysh_history
indentation: 2
list-max: 100
secondary-prompt-pattern: %M%P >
groovy>

1.5.31. /show

Show the shared variables (the binding). These include your shared variables as well as a few used for configuring various shell features.

Usage of the /show command

1.5.32. /slurp

Slurp files to shared variables. Groovy has a bunch of slurpers for various formats like XML, JSON, YAML, etc. You can use those in your code if you like, but the /slurp command can be a convenience shortcut. It supports most of the common formats, including JSON, XML, YAML, CSV, TOML and property files.

Usage of the /slurp command

As you can see in the usage information at the end of the above image, you can also provide an encoding and a format. If no format is given, UTF_8 is the default. If no format is given, the shell will try to determine the format from the extension of the file (if given).

Although the details of the exact object returned are an implementation detail which may change in the future, the current behavior is as follows:

Format Notes

JSON

Uses JsonSlurper, returns a lazy Map.

TOML

Uses TomlSlurper, returns a lazy Map.

YAML

Uses YamlSlurper, returns a lazy Map.

XML

Uses XmlParser, returns a Node.

PROPERTIES

Returns a Properties file, which is a Map-like object.

CSV

Uses Apache Commons CSV if on the classpath. Assumes a header row which is used to create a list (the rows) of maps from column name to value.

TEXT

Reads the file as raw lines (or argument as a line).

NONE

If you want the raw text rather than parsed content.

If these do change in the future, the replacement will be a compatible implementation, that follows the normal GPath conventions.

If you want more advanced control over the slurping, you can use Groovy’s "Slurper" classes directly, e.g. new XmlSlurper().parseText(…​).

1.5.33. /sort

The /sort command sorts files or standard input.

Deleting types

1.5.34. /tail

The /tail command displays the end of a file.

groovy> /tail -n2 fruit.txt
date
elderberry

1.5.35. /types

Show the declared types (enums, interfaces, classes, traits, annotation definitions, and records).

Usage of the /types command

Types can be deleted using /types -d (completion is available) but see also the /del command:

Deleting types

1.5.36. /ttop

Display information about threads.

Usage of the /ttop command

Various options are available:

groovy> /ttop --help
ttop -  display and update sorted information about threads
Usage: ttop [OPTIONS]
  -? --help                   Show help
  -o --order=ORDER            Comma separated list of sorting keys
  -t --stats=STATS            Comma separated list of stats to display
  -s --seconds=SECONDS        Delay between updates in seconds
  -m --millis=MILLIS          Delay between updates in milliseconds
  -n --nthreads=NTHREADS      Only display up to NTHREADS threads

1.5.37. /unsetopt

Unset options.

groovy> /unsetopt -?
unsetopt -  unset options
Usage: unsetopt [-m] option ...
       unsetopt
  -? --help                      Displays command help
  -m                             Use pattern matching

If no option is given, the current unset options are displayed.

groovy> /unsetopt
complete-in-word
complete-matcher-camelcase
no-complete-matcher-typo
history-verify
no-history-ignore-space
no-history-ignore-dups
no-history-reduce-blanks
no-history-beep
no-history-incremental
no-history-timestamped
no-auto-group
no-auto-menu
no-auto-list
auto-menu-list
recognize-exact
no-group
group-persist
case-insensitive
list-ambiguous
list-packed
list-rows-first
glob-complete
menu-complete
auto-fresh-line
delay-line-wrap
no-auto-param-slash
no-auto-remove-slash
insert-tab
mouse
disable-highlighter
no-bracketed-paste
erase-line-on-finish
case-insensitive-search
disable-undo
groovy>

See also /setopt to set options.

1.5.38. /wc

The /wc command displays word, line, character, and byte counts.

groovy> /wc LICENSE
     262    1754   13493 LICENSE

1.5.39. /unalias

Allows aliases to be removed.

Usage of the /unalias command

Removed aliases will also be removed from persistent storage.

1.6. State

1.6.1. History

History is stored in the file $HOME/.groovy/groovysh_history.

1.7. Widgets

JLine provides a powerful widget system that lets you extend the functionality of its line reader. A number of builtin widgets are available including end-of-line, beginning-of-line, forward-word, backward-word, kill-word, backward-kill-word, capitalize-word, transpose-words, and yank-pop, just to name a few. You can use the /keymap command to see the key bindings for these widgets.

Groovy also includes JLine’s tailtip and autosuggest widget functionality. You can see the related widgets by using the /widget -l command, which lists custom widgets.

groovy> /widget -l
_autosuggest-end-of-line (_autosuggest-end-of-line)
_autosuggest-forward-char (_autosuggest-forward-char)
_autosuggest-forward-word (_autosuggest-forward-word)
_tailtip-accept-line (_tailtip-accept-line)
_tailtip-backward-delete-char (_tailtip-backward-delete-char)
_tailtip-delete-char (_tailtip-delete-char)
_tailtip-expand-or-complete (_tailtip-expand-or-complete)
_tailtip-kill-line (_tailtip-kill-line)
_tailtip-kill-whole-line (_tailtip-kill-whole-line)
_tailtip-redisplay (_tailtip-redisplay)
_tailtip-self-insert (_tailtip-self-insert)
autosuggest-toggle (autosuggest-toggle)
tailtip-toggle (tailtip-toggle)
tailtip-window (tailtip-window)

These are available but not enabled by default. You can enable them using the related toggle widgets. You can see what key bindings are associated with these widgets by using the /keymap command.

groovy> /keymap
...
"^[s" tailtip-toggle
"^[v" autosuggest-toggle
...

Normally, completions are shown when you hit the 'TAB' key, but with the tailtip widget enabled, you can see completions as you type., as well as additional usage information given in the tailtip window as seen here for a command:

Tailtip

And here for some code:

Tailtip

With the autosuggest widget enabled, you can see suggestions for what to type next as you type, based on your history, as seen here:

Tailtip

You can accept the entire suggestion or a word at a time. Both widgets can be enabled.

1.8. Advanced Topics

1.8.1. REPL Model Details

The style of coding you use in a REPL is slightly different from what you might use in a script. The style you use may also depend on whether you are using the Groovy Shell in interpreter mode or not.

Using binding (shared) variables

Regardless of interpreter mode you can use shared variables:

groovy> fruit = []
groovy> fruit << 'peach'
[peach]
groovy> fruit << 'pear'
[peach, pear]
groovy> assert fruit == ['peach', 'pear']

Use the /show command to see the shared variables.

Using local variables

You should treat local variables as if you were using immutable data structures. An input which mutates a local variable will likely be undone by subsequent statements.

Only in interpreter mode:

groovy> def noFruit = []
[]
groovy> def oneFruit = noFruit << 'peach'
[peach]
groovy> def twoFruit = oneFruit << 'pear'
[peach, pear]
groovy> assert twoFruit == ['peach', 'pear']

By the time you get to the last statement, the previous three local variable definitions are remembered, so the assertion will pass.

Avoid this (relevant to interpreter mode):

groovy> def fruit = []
[]
groovy> fruit << 'peach'
[peach]
groovy> fruit << 'pear'
[pear]
groovy> assert fruit == []

The def fruit = [] will be remembered before executing each of the next three statements.

1.8.2. Gotchas

The Groovy Shell attempts to blend a Groovy programming environment with a shell-like scripting environment. Sometimes the two are at odds. As one example, don’t try /ls /tmp/.txt. The / will be interpreted as the start of a comment. Instead, quote the argument like this /ls '/tmp/*.txt'.

1.8.3. Platform Problems

The Groovy Shell relies heavily on the JLine3 library for its platform support. If you have specific platform problems, please refer to the JLine documentation.