JShell guide - history

created onJanuary 28, 2022

history

jshell maintains a history that includes all previously entered commands and snippets. This includes failed commands and snippets, as well as overwritten an dropped snippets. without parameter lists all commands and snippets that have been entered in the current session. lists all commands and snippets of all sessions.

rerunning snippets from history

reruns the last snippet. runs the snippet with id . Several ids can be provided, separated by space. Ids can also be given as an id range.

reruns the nth previous snippet. Commands are not included in the count.

searching through the history

jshell offers forward and backward incremental search of the history of the current session. Pressing at the jshell prompt replaces the jshell prompt with

(reverse-i-search)`':

Typing the search string at the prompt will display the last recent history entry that contains the search string:

(reverse-i-search)`Off': OffsetDateTime nowODT = OffsetDateTime.now()

Pressing again switches the displayed text to the second last entry of the history that matches the search string. You can use several times to loop through the history entries that match the search string.

(reverse-i-search)`Off': OffsetDateTime nowODT3 = nowODT2.plusDays(2)

I didn’t find the forward incremental search particularly useful, but rather cumbersome to use. You can try that one out with

persisting of history

The history is stored in the file (see section ‘the jshell configuration file’).

Entries of the history are stored like in the example below:

<entry key="HISTORY_LINE_01" value="Instant now = Instant.now()"/>