"vim:filetype" 相当于 vscode

"vim:filetype" equivalent in vscode

Vim 有一个很好的方法来指示特定文件应使用哪种语法高亮显示(通过在文件开头或结尾指定关键字 vim:filetype 来忽略文件扩展名.)

是否有建议 vscode 在不注意文件扩展名的情况下对文件使用语法荧光笔的等效方法?

示例:在 vim

# vim:filetype=i3
set $mod Mod4
set $alt Mod1
...

I am not looking for files.associations or the Change Language Mode settings. I want to state the language mapping within the file itself and I understand that probably this is not a builtin feature just hoping an extension can do this

Why I am interested in this? because many of the so-called dotfiles in Linux don't have any special extension and when you open the file in another vscode instance (in another machine) you want the IDE to be intelligent enough to deduce the correct syntax highlighter.

如果你需要坚持

您可以在 VS Code 设置中定义文件关联。如果你打开 settings.json 你可以像这样定义一个地图:

{
  "files.associations": {
    "main.php": "html"
  }
}

我建议您在项目 settings.json 中配置它,这样它只适用于这些文件。

虽然我不知道如何直接从文件配置它。

如果您需要准时完成

您可以手动更改 语言模式 以对特定文件使用不同的语法突出显示,而不管其扩展名。我在最近的博客 post 中对此进行了详细说明:https://www.nicoespeon.com/en/2019/12/vscode-syntax-highlight-on-copied-text/

有多种方法可以做到这一点,但我建议您打开命令面板 (Ctrl+Shift+P) 并输入 "Change Language Mode"。

它将弹出列表,您可以在其中select选择合适的语言。

我知道这个问题已经存在很长一段时间了,但我想我应该分享一下,现在可以通过第三方扩展来解决这个问题: https://marketplace.visualstudio.com/items?itemName=chrislajoie.vscode-modelines

我自己没有测试过,但它有很多安装,绝对没有坏消息,所以它应该可以正常工作。

回购:https://github.com/ctlajoie/vscode-modelines