4 Editing
JShell supports editing input at the jshell
prompt and editing in an external editor of your choice.
Shell editing enables you to edit snippets and commands as you enter them, and to retrieve and change previously entered snippets and commands. An external editor provides an alternative way to edit and create snippets, which is easier when you work with multiline snippets.
Topics
Shell Editing
Editing input at the command prompt makes it easy to correct your input and to retrieve and modify previously entered commands and snippets.
Shell editing in JShell is built on JLine2, which is functionally similar to BSD editline
and GNU readline
in Emacs mode. See JLine2 user information and GNU Readline documentation.
Input Line Navigation
Shell editing is supported for editing the current line, or accessing the history through previous sessions of JShell.
For navigating the input line, the Ctrl key and Meta key are used in key combinations. If your keyboard doesn’t have a Meta key, then the Alt key is often mapped to provide Meta key functionality.
For basic navigation within a line, use the right and left arrow keys or Ctrl+B for backwards and Ctrl+F for forward. For navigation between lines in the history, use the up and down arrow keys. Pressing the up arrow once replaces the current line with the previous command or snippet line. Pressing the up arrow again brings you to the line previous to that. The history contains both commands and snippet lines. If a snippet has multiple lines, then the up and down arrows navigate through each line of a snippet.
The following table identifies the keys used and the actions taken to navigate the input line.
Keys | Action |
---|---|
Return |
Enters the current line |
Left arrow |
Moves backward one character |
Right arrow |
Moves forward one character |
Up arrow |
Moves up one line, backward through history |
Down arrow |
Moves down one line, forward through history |
Ctrl+A |
Moves to the beginning of the line |
Ctrl+E |
Moves to the end of the line |
Meta+B |
Moves backward one word |
Meta+F |
Moves forward one word |
History Navigation
A history of snippets and commands is maintained across JShell sessions. This history provides you with access to items that you entered in the current and previous sessions.
To reenter or edit prior input, navigate the history using the up, down, left, and right arrows. Entered text is inserted at the cursor. The Delete key is used to delete text. Press the Enter key to reenter the history line, modified or not.
The up and down arrow keys move backward and forward through the history one line at a time, for example:
jshell> class C {
...> int x;
...> }
| created class
jshell> /list
1 : class C
int x;
}
jshell> <up arrow>
The up arrow key shows the following line:
jshell> /list
Pressing the up arrow again shows the last line of the class definition:
jshell> }
Pressing the down arrow returns to the /list
command. Pressing Enter executes it:
jshell> /list
1 : class C {
int x;
}
Ctrl+up arrow goes up by snippets. For single-line snippets, Ctrl+up arrow behaves the same as up arrow. For multiline snippets, such as class C
, Ctrl+up arrow skips the additional lines and goes to the top of the snippet.
Input Line Modification
Input lines retrieved from the history can be modified as needed and reentered, which saves you from having to retype a line just to make small changes.
Add text at the current cursor position simply by entering it. See Input Line Navigation for the keys used move the cursor within a line.
The following table identifies the keys used and the actions taken to modify the input line.
Keys | Action |
---|---|
Delete |
Deletes the character at or after the cursor, depending on the operating system. |
Backspace |
Deletes the character before the cursor. |
Ctrl+K |
Deletes the text from the cursor to the end of the line. |
Meta+D |
Deletes the text from the cursor to the end of the word. |
Ctrl+W |
Deletes the text from the cursor to the previous white space. |
Ctrl+Y |
Pastes the most recently deleted text into the line. |
Meta+Y |
After Ctrl+Y, Meta+Y cycles through previously deleted text. |
Search and More
Searching the history is a feature of JShell that makes it easier to find the line you want without going through the history one line at a time.
To start your search, press Ctrl+R. At the prompt, enter the search string. The search proceeds backward from your most-recent entry and includes previous sessions of JShell. The following example shows the prompt that is presented after pressing Ctrl+R:
jshell> <Ctrl+R>
((reverse-i-search)`':
Based on the example in History Navigation, entering class changes the display to show the most-recent line with the text class
:
(reverse-i-search)`class': class C {
The search is incremental, so this line is retrieved with just the first character c. You can continue to search earlier in the history by pressing Ctrl+R repeatedly. Ctrl+S moves the search forward towards the present.
You can define a keyboard macro by entering Ctrl+x (, then entering your text, and finally entering Ctrl+x ). To use your macro, enter Ctrl+x e.
The following table shows the key combinations for searching and creating macros.
Keys | Action |
---|---|
Ctrl+R |
Searches backward through history |
Ctrl+S |
Searches forwards through history |
Ctrl+X ( |
Starts a macro definition |
Ctrl+X ) |
Finishes a macro definition |
Ctrl+X e |
Executes a macro |
External Editor
An alternative to editing at the command prompt is to use an external editor. This editor can be used to edit and create snippets, and is especially helpful for multiline snippets. You can configure JShell to use the editor of your choice.
To edit all existing snippets at once in an editor, use /edit
without an option. To edit a specific snippet in an editor, use the /edit
command with the snippet name or ID. Use the /list
command to get the snippet IDs. The following example opens an editor to edit the snippet named volume
, which was defined in Forward References:
jshell> /edit volume
You can also enter new snippets in the editor. When you save in the editor, any snippet that is changed or new is entered into the JShell session. Feedback from the snippets is shown in the JShell window, however, no JShell prompt is shown. You can’t enter commands or snippets in the JShell window until the editor is closed.
If you don’t specify an editor, then the following environment variables are checked in order: JSHELLEDITOR
, VISUAL
, and EDITOR
. If none of those are set, then a simple default editor is used. To set up JShell to open the editor of your choice, use the /set editor
command. The argument to the /set editor
command is the command needed to start the external editor that you want to use. The following example sets kwrite
as the editor and opens the editor with all existing snippets:
jshell> /set editor kwrite
| Editor set to: kwrite
jshell> /edit
Defining x
in the external editor window and saving the change generates the following output in the JShell window:
| created variable x of type int with initial value 6
Closing the external editor restores the JShell prompt.
jshell>