VSCode syntax highlighting for custom shebang
Asked Answered
R

3

10

At work we run python under a custom environment, and thus we use a non-standard shebang. I tested that VSCode recognizes python files without a .py extension if they have a shebang that's either #!/usr/bin/env python or /usr/bin/python or variants of these.

At work I use a shebang similar to this: #!/some/directory/envroot "$ENVROOT/bin/python" but vs code doesn't recognize this, so I have to manually set the language to python each time.

Is there a configuration somewhere that I can map a custom shebang to a language so I don't have to set it manually each time I open the file?

Reins answered 28/2, 2017 at 0:12 Comment(0)
C
11

I work on VSCode.

The shebang mapping is defined by firstLine in the extension grammar contributions:

    "languages": [{
        "id": "python",
        "extensions": [ ".py", ".rpy", ".pyw", ".cpy", ".gyp", ".gypi" ],
        "aliases": [ "Python", "py" ],
        "firstLine": "^#!/.*\\bpython[0-9.-]*\\b",
        "configuration": "./language-configuration.json"
    }]

There is no setting to control this, but you could use file.associations to map these files to python directly.

Your specific example also seems like a bug to me. We currently only use the first line pattern if the entire line matches, which seems odd. I've opened an issue to investigate this: https://github.com/Microsoft/vscode/issues/21533

Convalesce answered 28/2, 2017 at 3:28 Comment(1)
Thank you for the file associations tip! All these files are in the bin directory, so I should be able to use that to mark all of them as python.Reins
S
3

To complement Matt Bierner's helpful answer:

The JSON settings Matt references are in <languageId>/package.json files in the following locations:

On GitHub:

When installed:

  • In the resources/app/extensions subfolder of the VSCode installation folder; e.g.:

    • Windows (32-bit version):

      C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions
      
    • macOS:

      /Applications/Visual Studio Code.app/Contents/Resources/app/extensions
      
  • E.g, for Python:

    • Windows (32-bit version):

      C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\python\package.json
      
    • macOS:

      /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/python/package.json
      
Smoothspoken answered 22/2, 2018 at 22:28 Comment(0)
T
0

And to complement mklement0's complement:
On Linux, see /usr/share/code/resources/app/extension/
E.g for Lua: /usr/share/code/resources/app/extensions/lua/package.json

Tyrontyrone answered 16/12, 2019 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.