在 Visual Studio 代码中设置语法突出显示的语言

Set language for syntax highlighting in Visual Studio Code

在混淆开始之前,这个问题是关于代码的,新的轻量级 Visual Studio 编辑器。 您可以从这里获取:https://code.visualstudio.com/

我有一个包含 CSS 的文本文件 (.txt),我想获得语法高亮显示。您可以使用 ctrl+shift+p[=23打开命令面板 =].但是你不能像在 Sublime 中那样设置语法。

是否有任何其他方法可以从我的文本文件中获取 CSS 着色?

在最右下角,笑脸左侧有一个图标 "Plain Text"。单击它时,会出现包含所有语言的菜单,您可以在其中选择所需的语言。

Ctrl + KM 然后输入(或单击)您想要的语言。

或者,要从命令面板访问它,请查找 "Change Language Mode",如下所示:

人们可能难以使语法高亮显示工作的另一个原因是他们没有安装适当的语法包。虽然一些默认语法包是预安装的(如 Swift、C、JS、CSS),但其他语法包可能不可用。

要解决此问题,您可以 Cmd + Shift + P → "install Extensions" 并查找您要添加的语言,例如 "Scala"。

找到合适的语法包,安装并重新加载。这将为具有预定义扩展名的文件选择正确的语法,即本例中的 .scala

最重要的是,您可能希望 VS Code 将所有具有特定自定义扩展名的文件视为您的首选语言。假设您想将所有 *.es 个文件高亮显示为 JavaScript,然后只需打开 "User Settings" (Cmd + Shift + P → "User Settings" ) 并像这样配置您的自定义文件关联:

  "files.associations": {
    "*.es": "javascript"
  },

自定义文件扩展名的语法突出显示

任何自定义文件扩展名都可以与标准语法高亮关联 custom files association 在用户设置中如下。

Note that this will be a permanent setting. In order to set for the current session alone, type in the preferred language in Select Language Mode box (without changing file association settings)

正在安装新的语法包

如果需要的语法包默认没有,可以通过Extension Marketplace添加(Ctrl+Shift+X ) 并搜索语言包。

您可以进一步重现上述步骤,将文件扩展名映射到新语法包。

要永久设置语言语法:
打开 settings.json 个文件

  • javascript 格式化所有 txt 文件
"files.associations": {
            "*.txt": "javascript"
          
     }
  • 将所有未保存的文件(untitled-1 等)格式化为 javascript:
"files.associations": {
            "untitled-*": "javascript"
          
     }

请注意,对于“无标题”编辑器(“Untitled-1”、“Untitled-2”),您现在可以在设置中设置语言。

之前的设置是:

"files.associations": {
        "untitled-*": "javascript"
 }

这将不再有效,因为 VSCode 1.42 (Q1 2020) will change the title of those untitled editors
标题 现在将成为编辑标题 文档的第一行,连同作为描述一部分的通用名称。
它不会再以“untitled-

开头

参见“Untitled editor improvements

关于那些“无标题”编辑器的相关语言:

By default, untitled files do not have a specific language mode configured.

VS Code has a setting, files.defaultLanguage, to configure a default language for untitled files.

With this release, the setting can take a new value {activeEditorLanguage} that will dynamically use the language mode of the currently active editor instead of a fixed default.

In addition, when you copy and paste text into an untitled editor, VS Code will now automatically change the language mode of the untitled editor if the text was copied from a VS Code editor:

并查看 workbench.editor.untitled.labelFormat in VSCode 1.43


2021 年 3 月(VSCode 1.55 可能),issue 118455 "Automatic language classification for Untitled files" and PR 119325 正在研究某种针对无标题文件的自动语言检测。


如果您忘记了,VSCode 1.56,2021 年 4 月:

Untitled editors hint

We have noticed that many new users are not aware that a language has to be set in order to get full VS Code language support.

To help with this problem, we have introduced a hint for untitled editors to help users set the correct language mode.
The untitled hint might not be helpful to advanced users, so it goes away immediately as you start typing or you can select don't show to never display the hint again.


VScode1.60(2021 年 8 月):

Automatic language detection

Last release, we introduced an experimental feature for untitled files that would automatically set the language mode for the file based on the contents.

This feature uses machine learning to guess the language and is done entirely on your machine. It's powered by the open-source ML library, Tensorflow.js, and the ML model from Guesslang by GitHub user @yoeo.

This release we are enabling this feature by default and also expanding the automatic language detection to files that don't have a file extension. In Notebooks, we provide an easy way to ask for language detection using the language picker. This enables a few fun scenarios:

  • Grab an example from online and paste it in an untitled editor Language detection of untitled files

  • "Pipe into code" language detection (showing off detection of extension-less files) Language detection of extension-less files

  • Auto detect option in Notebooks language picker Language detection of Notebooks


VSCode 1.65(2022 年 2 月)附带:

Improved automatic language detection

When the new setting workbench.editor.historyBasedLanguageDetection is enabled, untitled editors will use an improved automatic language detection algorithm that factors in your editor history and the contents of the current workspace to provide detection results with much less input text required than before.

Below is an example of using this across JavaScript, TypeScript, Markdown, PHP, and C++ (many more languages are supported):

Theme: GitHub Light + HC Customizations

现在您可以通过安装 VS Code 扩展从命令面板设置语法高亮:Set Syntax

https://marketplace.visualstudio.com/items?itemName=ahgood.set-syntax

这可能不是问题的正确答案,但这就是发生在我身上的事情。有时我们寻求的答案可能不是我们需要的答案。

您可以按如下方式更改 VS Code 中的语言模式。

Select您要更改的文件然后使用以下快捷方式。

在 macOS 上:

  • +K然后按M.

在 Windows 和 Linux 上:

  • CTRL+K然后按M

Select 提供的列表中的语言或选择 Auto-detect