Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a shortcut for system.out.println in visual studio code

I am using visual studio code IDE for my Java project and I want to create a shortcut for:

System.out.println();

That is if I write sysout or something like that it will automatically changed to System.out.println(); with cursor inside parenthesis.

Is there any way it can be achieved in VScode?

like image 558
Mayank Kumar Thakur Avatar asked Jun 10 '26 20:06

Mayank Kumar Thakur


2 Answers

There is no need to define the snippet yourself. You can install https://marketplace.visualstudio.com/items?itemName=redhat.java, which contains the predefined snippet for your case (and more than that), here are some screenshot:

sysout:

enter image description here

syserr:

enter image description here

and even systrace:

enter image description here

If you want to developing your Java application in VS Code, you can also install https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack, which provides debugging, testing, project managing capabilities to you.

like image 97
Sheng Chen Avatar answered Jun 12 '26 11:06

Sheng Chen


As mentioned by @vaibhavsahu in the comments you can create user defined snippets in VSCode: https://code.visualstudio.com/docs/editor/userdefinedsnippets

Here's a relevant excerpt from that document which shows where to locate the user defined snippets file:

You can easily define your own snippets without any extension. To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages. VS Code manages the creation and refreshing of the underlying snippets file(s) for you.

So in your case you'd go to File (or Code on macOS) > Preferences > User Defined Snippets and then type in Java, this should open a java.json file (which contains an example user defined snippet commented out).

I think this snippet would do it for what you're trying to accomplish:

"System.out.println(placeholder)": {
    "prefix": "sysout",
    "body" : ["System.out.println(${1:string})"],
    "description" : "System out println with placeholder in parens"
}

Then when you start typing sysout in a .java file IntelliSense should show the recommendation for the code snippet, when you enter enter it should be substituted in with your placeholder in the parens.

Edit:

The System.out.println(placeholder) from the java.json file is just what displays in the IntelliSense dropdown, you could make it System print or whatever you'd like so that you'll recognize it. The actual code which gets substituted is in the "body"

like image 28
R4N Avatar answered Jun 12 '26 09:06

R4N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!