简体   繁体   English

在 visual studio 代码中为 system.out.println 创建一个快捷方式

[英]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:我正在为我的 Java 项目使用 visual studio code IDE,我想为以下项目创建一个快捷方式:

System.out.println();

That is if I write sysout or something like that it will automatically changed to System.out.println();也就是说,如果我写sysout或类似的东西,它会自动更改为System.out.println(); with cursor inside parenthesis.括号内为 cursor。

Is there any way it can be achieved in VScode?有什么办法可以在 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:您可以安装https://marketplace.visualstudio.com/items?itemName=redhat.java ,其中包含您的案例的预定义代码段(以及更多),以下是一些屏幕截图:

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.如果您想在 VS Code 中开发 Java 应用程序,您还可以安装https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack ,它为您提供调试、测试和项目管理功能。

As mentioned by @vaibhavsahu in the comments you can create user defined snippets in VSCode: https://code.visualstudio.com/docs/editor/userdefinedsnippets正如@vaibhavsahu 在评论中提到的,您可以在 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.要创建或编辑您自己的代码片段,select 文件 > 首选项下的用户代码片段(macOS 上的代码 > 首选项),然后 select 代码片段应该出现的语言(按语言标识符),或者如果他们应该出现新的全局片段文件选项出现在所有语言中。 VS Code manages the creation and refreshing of the underlying snippets file(s) for you. VS Code 为您管理底层代码片段文件的创建和刷新。

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).因此,在您的情况下,您将 go 转到文件(或 macOS 上的代码)> 首选项 > 用户定义的片段,然后输入 Java,这应该会打开一个 java.json 文件(其中包含一个示例用户定义的片段注释掉)。

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.然后,当您开始在 .java 文件中键入sysout时,IntelliSense 应该会显示代码片段的建议,当您输入时,应该将其替换为括号中的占位符。

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. java.json 文件中的System.out.println(placeholder)正是 IntelliSense 下拉列表中显示的内容,您可以将其设置为System print或任何您喜欢的内容,以便您识别它。 The actual code which gets substituted is in the "body"被替换的实际代码在“正文”中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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