获取 Visual Studio Code 将无扩展名文件的语法高亮显示为 bash 脚本? [英] Get Visual Studio Code to syntax highlight extensionless files as bash scripts?

查看:31
本文介绍了获取 Visual Studio Code 将无扩展名文件的语法高亮显示为 bash 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始编写一些 bash 脚本来增强 git,使用 Visual Studio Code 并且我希望编辑器能够这样做时使用语法突出显示.

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.

这些文件没有扩展名,否则 git 不会将它们作为附加命令提取.

The files do not have an extension, or git won't pick them up as additional commands.

以下是此类脚本的示例名称:git-review

Here's an example name of such a script: git-review

那么问题是,如何配置 Visual Studio Code 以对这些文件使用 Bash 语法高亮显示?

So the question is, how can I configure Visual Studio Code to use Bash syntax highlighting for these files?

如果我将 Visual Studio Code 中的文件重命名为带有 .sh 的文件,例如 git-review.sh,然后返回无扩展名 (git-review),Visual Studio Code 似乎记住了这一点并显示语法突出显示,但我添加到文件夹中的任何新文件都以纯文本形式突出显示(直到我进行重命名舞蹈).即使我关闭文件并重新启动 Visual Studio Code 并重新打开文件,Visual Studio Code 似乎也记住了这一点.我不知道多久.

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.

我可以使用纯文本文件,这些文件由于某种原因没有突出显示为 bash 脚本的扩展名,但显然,如果有一种方法可以使 Visual Studio Code 更加智能,例如查看 <的第一行code>#!/bin/sh 那就更好了.

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.

我尝试使用为 '.xyz' 配置文件关联"来查看这些关联是如何存储在设置文件中的,并发现这正确地将 Bash 突出显示应用于 .xyz 文件:

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"
},

然而,这不会:

"files.associations": {
    "*.": "shellscript"
},

也不这样做:

"files.associations": {
    "*": "shellscript"
},

有什么办法可以让这个设置适用于无扩展名的文件吗?

Is there any way to coax this setting into working for extensionless files?

如果这很重要的话,这就是 Visual Studio Code for Windows.

This is Visual Studio Code for Windows if that matters at all.

推荐答案

如果有办法让 Visual Studio Code 更智能,比如查看 #!/bin/sh 的第一行,那就更好了.

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 实际上已经确实基于shebang将文件与shellscript关联起来.但是,请注意它不会动态检测它 - 如果添加 shebang,则需要关闭并重新打开该文件,以便 VSCode 注意到它.

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.

这篇关于获取 Visual Studio Code 将无扩展名文件的语法高亮显示为 bash 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆