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

我有一个文本文件(.txt)与CSS在其中,并希望获得语法高亮显示。你可以用ctrl+shift+p打开命令面板。但是你不能像Sublime那样设置语法。

有没有其他方法从我的文本文件得到CSS着色?


在右下角,微笑符号的左边有一个图标,上面写着“纯文本”。当你点击它,所有语言的菜单出现,你可以选择你想要的语言。

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

或者,从命令面板访问它,寻找“更改语言模式”,如下所示:

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

要解决这个问题,你可以Cmd + Shift + P→“install Extensions”,然后找到你想要添加的语言,比如“Scala”。

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

最重要的是,你可能希望VS Code将所有具有特定自定义扩展名的文件作为你的首选语言。假设你想突出显示所有*.es文件为JavaScript,然后只需打开“用户设置”(Cmd + Shift + P→“用户设置”),并配置您的自定义文件关联如下:

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

语法高亮显示自定义文件扩展名

任何自定义文件扩展名都可以与标准语法高亮显示相关联 在用户设置中自定义文件关联,如下所示。

注意,这将是一个永久设置。 若要单独设置当前会话,请在“选择语言模式”框中输入首选语言(不更改文件关联设置)

安装新的语法包

如果默认情况下所需的语法包不可用,您可以通过扩展市场(Ctrl+Shift+X)添加它们并搜索语言包。

您可以进一步复制上面的步骤,用新的语法包映射文件扩展名。

永久设置语言语法: 打开设置。json文件

用javascript格式格式化所有TXT文件

"files.associations": {
            "*.txt": "javascript"
          
     }

格式化所有未保存的文件(untitled-1等)到javascript:

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

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

之前的设置是:

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

这将不再总是有效,因为VSCode 1.42 (Q1 2020)将改变那些未命名编辑器的标题。 标题现在将是编辑器标题文档的第一行,以及作为描述的一部分的通用名称。 不会再以"untitled-"开头了

参见“无题编辑器改进”

关于“Untitled”编辑器的相关语言:

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:

并在VSCode 1.43中查看workbench.editor.untitled.labelFormat。


在2021年3月(可能用于VSCode 1.55), issue 118455“无题文件的自动语言分类”和PR 119325正在研究某种无题文件的自动语言检测。


如果你忘了,在2021年4月的VSCode 1.56中:

标题编辑提示

我们注意到许多新用户不知道为了获得完全的VS Code语言支持必须设置一种语言。 为了解决这个问题,我们为未命名编辑器引入了一个提示,以帮助用户设置正确的语言模式。 没有标题的提示可能对高级用户没有帮助,所以当您开始输入时,它会立即消失,或者您可以选择“不显示”以永远不再显示提示。


VScode 1.60(2021年8月):

自动语言检测

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


VSCode 1.70(2022年7月)将提出(issue 152920, PR 153872)

提供一个命令,用特定的语言集创建一个新的无标题文件 允许在命令中指定语言id来创建一个新的无标题文件 registerCommandAndKeybindingRule允许“New Untitled File args”添加已知的语言ID。

这允许像[创建Python文件](命令:toSide:workbench.action.files.newUntitledFile?<args>)这样的链接,它将直接为Python语言创建一个新的无标题文件。

现在你可以通过安装VS Code扩展从命令面板中设置语法高亮显示

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

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

您可以在VS Code中更改语言模式,如下所示。

选择要更改的文件,然后使用以下快捷方式。

在macOS:

按⌘+K,然后按M。

Windows和Linux操作系统:

按CTRL+K,然后按M

从提供的列表中选择语言或选择“自动检测”