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?
There is no need to define the snippet yourself. You can installhttps://marketplace.visualstudio.com/items?itemName=redhat.java , which contains the predefined snippet for your case (and more than that), here are some screenshot:
sysout:
syserr:
and even systrace:
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.
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"
this extension or plugin can meet your need https://marketplace.visualstudio.com/items?itemName=ricardo-emerson.java-sysout
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.