Core Java

Set Visual Studio Code as default editor in jshell

Java Shell (jshell) is an interactive tool for learning and prototyping in Java. It was introduced with Java 9 and since then I use it ocassionally either for some quick prototyping, during presentations or simply to verify new features in the Java language. Since jshell is a command line tool (with basic intellisense) editing files in jshell is not the best expierience. Fortunatelly, jshell allows changing the default editor and set it to the one of your choice, including Visual Studio Code, Atom or Sublime.

My primary and favorite IDE is IntelliJ it is pretty havy for some really basic source code editing and its built-in support for jshell is far from perfect. So why not to set Visual Studio Code as a default jshell editor?

Set editor for the current jshell session

Open jshell in the terminal

1
$ jshell

Run jshell command

1
2
jshell> /set editor /usr/local/bin/code -w
|  Editor set to: /usr/local/bin/code -w

The -w option sets waiting for the file to be closed before returning.

Now you can edit the current session by running the edit command:

1
jshell> /edit

This will open Visual Studio Code (it opens new tab if you have VSC opened). You can edit the file and once you are done with editing save and close the file.

Set editor and retain the setting between jshell sessions

If you want to retain the setting between jshell sessions you need to add retain flag while setting the editor:

1
2
3
jshell> /set editor -retain /usr/local/bin/code -w
|  Editor set to: /usr/local/bin/code -w
|  Editor setting retained: /usr/local/bin/code -w

Learn about the tools I use on macOS in this post: macOS: essential tools for (Java) developer]

The solution presented here will also work for Atom or any other editor with the option to wait for the opened file to be closed before returning.

See also

Do you want to switch Java SDKs easily so that you can play with new Java features in jshell? Use SDKMAN! Check this blog post and learn how to get started with SDKMAN!: Manage multiple Java SDKs with SDKMAN! with ease

Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Set Visual Studio Code as default editor in jshell

Opinions expressed by Java Code Geeks contributors are their own.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ElenaGillbert
4 years ago

Hi..
I’m Elena gillbert.The -w option sets waiting for the file to be closed before returning. This will open Visual Studio Code (it opens new tab if you have VSC opened). You can edit the file and once you are done with editing save and close the file.

Back to top button