0

I'd like to add some Java code snippets for Visual Studio Code, to speed up things that I commonly use, e.g. System.out.println() but the snippets are written in json, which I have no experience of. Can anybody help me with how to structure these Java snippets, and how you actually use them when programming?

This was my own attempt:

"Print to console":
{
    "prefix": "System",
    "body" : ["System.out.println("],
    "description": "Print to the console"
}

Though I don't know whether I've written it wrong or whether I'm not accessing the snippet correctly.

1 Answer 1

2

From https://code.visualstudio.com/docs/editor/userdefinedsnippets

You can define your own snippets for specific languages. To open up a snippet file for editing, open User Snippets under File > Preferences (Code > Preferences on Mac) and select the language for which the snippets should appear.

After open up java.json using the step described above, you should see an example commented.

To fix your snippet, it should be:

"Print to Console": {
    "prefix": "System",
    "body": [
        "System.out.println(\"$1\");"
    ],
    "description": "Print to the console"
}

As you type System on vscode, you will see the IntelliSense to give you the suggestion.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to escape the inner "s if you use ' there instead so "System.out.println('$1');"
Thanks for the example! For anyone else looking, placing $0 inside the brackets means that your cursor will start there when the snippet is added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.