Reputation: 391276
I have started writing some bash scripts to augment git, using Visual Studio Code and I want the editor to use syntax highlighting when doing so.
The files do not have an extension, or git won't pick them up as additional commands.
Here's an example name of such a script: git-review
So the question is, how can I configure Visual Studio Code to use Bash syntax highlighting for these files?
If I rename the file from inside Visual Studio Code to something with .sh
, like git-review.sh
and then back to no extension (git-review
), Visual Studio Code seems to remember this and show syntax highlighting but any new files I add to the folder is highlighted as plain text (until I do the renaming dance). Visual Studio Code seems to remember this even if I close the file and restart Visual Studio Code and reopen the file. For how long I do not know.
I can live with plain text files that for some reason doesn't have an extension being highlighted as bash scripts, but obviously, if there is a way to make Visual Studio Code even smarter, like looking at the first line for #!/bin/sh
then that would be even better.
I tried using the "Configure file association for '.xyz'" to see how these associations were stored in the settings file and found that this correctly applies Bash highlighting to .xyz
files:
"files.associations": {
"*.xyz": "shellscript"
},
However, this does not:
"files.associations": {
"*.": "shellscript"
},
nor does this:
"files.associations": {
"*": "shellscript"
},
Is there any way to coax this setting into working for extensionless files?
This is Visual Studio Code for Windows if that matters at all.
Upvotes: 18
Views: 14745
Reputation: 1
Recently I made extension that allow to set syntax manually for each file. https://github.com/azhinu/grammar-move-vscode
Upvotes: 0
Reputation: 2321
You can use globbing to hack it into your settings.json
as found in this post.
"files.associations": {
/* BEGIN: hack to make files with no extension use bash syntax highlighting */
"[!.]": "shellscript",
"[!.][!.]": "shellscript",
"[!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript",
"[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "shellscript"
/* END: hack to make files with no extension use bash syntax highlighting */
},
Upvotes: 1
Reputation: 1366
As a workaround you can manually set highlighting for your file by clicking on the bottom right corner of VS code editor "Select language mode" and then select the file type (also Ctrl+K M).
The down side is that you have to do this every time but it is a lot easier than the renaming procedure you described.
Upvotes: 11
Reputation: 34128
if there is a way to make Visual Studio Code even smarter, like looking at the first line for
#!/bin/sh
then that would be even better.
VSCode actually already does assoicate files with shellscript
based on the shebang. However, note that it doesn't detect it dynamically - if you add a shebang, you will need to close and reopen the file for VSCode to notice it.
Upvotes: 27