简体   繁体   中英

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?

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"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM