Joe Casadonte
Joe Casadonte

Reputation: 16889

Custom language associations in VS Code based on the contents of the first line

Using VS Code (v1.39.2) when I open a file whose first line is #!/bin/bash, the editor knows to set the language to Shell Script. Is there a way that I can configure my own language associations such that some arbitrary first line is associated with a built-in language (e.g. /bin/hello-world selects Perl)?

Upvotes: 1

Views: 331

Answers (2)

PotatoHeadz35
PotatoHeadz35

Reputation: 43

Shebang Language Associator is great, however, you can configure language associations in the application. In the config file add:

    "files.associations": {
        "*.myphp": "php"
    }

or

    "languages": [{
        "id": "java",
        "extensions": [ ".java", ".jav" ],
        "aliases": [ "Java", "java" ]
    }]

Language supports are added using the language identifier:

    "grammars": [{
        "language": "groovy",
        "scopeName": "source.groovy",
        "path": "./syntaxes/Groovy.tmLanguage.json"
    }],
    "snippets": [{
        "language": "groovy",
        "path": "./snippets/groovy.json"
    }]

Hope this helps!

Upvotes: 1

Vassilios
Vassilios

Reputation: 493

The #!/bin/bash part is called a shebang. I don't believe that VSCode supports custom shebangs by default but you can use the Shebang Language Associator to do what you want. You just enter its settings and set whichever pattern and language you want.

Example:

"shebang.associations": [
    {
        "pattern": "^#!/bin/bash$",
        "language": "shellscript"
    }
]

Upvotes: 3

Related Questions