Sub topic 11
Tab completion and editing with /edit
This chapter assumes you can start a JShell session, enter expressions and declarations, and use basic slash commands such as /list and /vars. It covers three things: Tab completion, the line-editing keys at the prompt, and the /edit command for changing earlier snippets.
Tab completion
Press Tab at the prompt and JShell completes what you typed. If only one completion fits, JShell inserts it. If several fit, the first Tab inserts their shared prefix. A second Tab lists the candidates.
jshell> String s = "hello"
s ==> "hello"
jshell> s.sub<Tab>
subSequence( substring(
jshell> s.subs<Tab>
jshell> s.substring(Completion works on several kinds of input:
- Variable, method, and class names declared in the session.
- Members after a dot, such as the methods of
s. - Class names from the imports in effect and from the class path.
- Slash commands and their arguments.
/edcompletes to/edit, and/dropfollowed by Tab offers the snippet names you can drop.
Signatures and documentation
Type a method name and an opening parenthesis, then press Tab. JShell shows the signatures of the overloads. Press Tab again to show the Javadoc for the method.
jshell> Math.max(<Tab>
int Math.max(int a, int b)
long Math.max(long a, long b)
float Math.max(float a, float b)
double Math.max(double a, double b)The Javadoc view needs the JDK source archive (lib/src.zip). Some distributions leave it out, and in that case you see signatures only.
Shift+Tab shortcuts
Shift+Tab followed by a letter applies a fix to the line you are typing.
| Keys | Effect |
|---|---|
| Shift+Tab, then `v` | Turns an expression into a variable declaration with an inferred type. |
| Shift+Tab, then `m` | Turns an expression into a method declaration. |
| Shift+Tab, then `i` | Offers `import` statements for an unresolved class name. |
For example, type new ArrayList<String>() and press Shift+Tab, then v. JShell rewrites the line as a declaration such as ArrayList<String> arrayList = new ArrayList<String>(). It leaves the cursor on the suggested name so you can change it. If you type Pattern without an import, Shift+Tab then i lists java.util.regex.Pattern and other matches, and adds the one you pick.
Editing at the prompt
JShell reads input through a line editor with Emacs-style keys. These are the ones worth memorizing:
| Keys | Action |
|---|---|
| Up and Down arrows | Move through the history of earlier input. |
| Ctrl+R | Search backward through history as you type. |
| Ctrl+A and Ctrl+E | Jump to the start or end of the line. |
| Alt+B and Alt+F | Move back or forward one word. |
| Ctrl+K | Delete from the cursor to the end of the line. |
| Ctrl+W | Delete the word before the cursor. |
History is saved between sessions. When a snippet spans several lines, such as a method body, the Up arrow recalls the whole snippet as one unit, so you can fix one line without retyping the rest.
You can also type a snippet across several lines. If the input is not yet complete, JShell prints a continuation prompt and waits for more:
jshell> int cube(int x) {
...> return x * x * x;
...> }
| created method cube(int)Three related history shortcuts re-run input without retyping it: /! re-runs the last snippet, /-2 re-runs the snippet two back, and /3 re-runs snippet 3 from /list.
Changing snippets with /edit
Arrow keys are enough for the line you are typing. For anything larger, /edit opens snippets in an editor, lets you change them, and applies the result when you finish.
Choosing what to edit
/edit takes an optional argument that selects snippets:
jshell> /edit
jshell> /edit square
jshell> /edit 2
jshell> /edit 3-5
jshell> /edit -all- With no argument,
/editopens every active snippet. - A name opens the snippet that declares that variable, method, or class.
- A number or range uses the IDs shown by
/list. -allincludes dropped and failed snippets.-startopens the startup snippets.
Editing a declaration redefines it in place, as when you re-enter it by hand. Edit square and save, and JShell reports modified method square(int). Snippets that depended on it keep working against the new definition.
The default editor
Without configuration, /edit opens JShell Edit Pad. This is a small Swing window with Accept, Cancel, and Exit buttons. It needs a graphical display. On a remote server or in a container without a display, /edit fails, so set an external editor first.
Setting an external editor
/set editor chooses the command JShell launches. Without -retain the setting lasts only for the current session. With -retain it is stored for later sessions.
jshell> /set editor -retain -wait vim
| Editor set to: vim
| Editor setting retained: vimYou can also set the JSHELLEDITOR, VISUAL, or EDITOR environment variable, and JShell uses the first one it finds in that order. Run /set editor with no arguments to see the current setting, and /set editor -default to go back to Edit Pad.
The -wait option controls when JShell reads your changes:
- With
-wait, JShell pauses until the editor process exits, then applies the changes. Use it for terminal editors such as vim and nano, which take over the terminal. - Without
-wait, JShell stays usable while the editor is open and applies changes each time you save. This suits graphical editors.
Some graphical editors, such as VS Code, start a process that returns immediately. Without -wait, JShell handles this by applying changes on each save, which is usually what you want.
Errors during editing
If an edited snippet does not compile, JShell prints the error and leaves the previous definition in place. Run /edit on the same snippet to try again. Check /list -all if you are unsure which snippets failed.
Exercise
You declared int total = readCount();, where readCount() prints a message each time it runs. You then run /edit total and change nothing except a comment, then save. What do you expect to see, and why?
Show answer
The message from readCount() prints again. Editing a variable snippet re-evaluates its declaration, which runs the initializer. Changing only the comment still counts as an edit of that snippet, so total is redefined and readCount() is called a second time.
Exercise
You are working over SSH on a server with no display, and /edit fails to open a window. Give one command that makes /edit use vim from now on, and explain why it needs -wait.
Show answer
/set editor -retain -wait vim. Vim runs in your terminal, so JShell must wait for the editor process to exit before it reads the changed text. Without -wait, JShell would continue immediately and the terminal would be shared by two programs. -retain keeps the setting for later sessions.