默认为 VS Code 上的语言分配没有扩展名的文件 [英] Assign file with no extension to a language on VS Code as default

查看:28
本文介绍了默认为 VS Code 上的语言分配没有扩展名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VS Code 中,我通常打开没有扩展名的文件,只有 filename .我知道我可以使用 Change Language Mode --> 更改语言语法.我想要的语言,但我不想每次打开这样的文件时都手动执行此操作.每次打开没有扩展名的文件时,我可以默认使用这种语言吗?

In VS Code I usually open files that have no extension just filename . I know I can change the language syntax with Change Language Mode --> Language that I want but I don't want to do this manually every time I open such a file. Can I make a default to this language every time I open a file with no extension?

我知道我可以做到:

"files.associations": {
    "*.myphp": "php"
}

但是如果没有扩展名呢?此外,我希望能够在不影响其他文件类型(具有扩展名)的情况下执行此操作.

But what if there is no extension? Also I want to be able to do this without affecting the other file types (that have extension).

推荐答案

VS Code 的 globbing 目前似乎没有办法检测没有扩展名的文件.每次有人打开问题时,他们都会将其指向这里的问题.他们在此处详细介绍了globbing支持.

VS Code's globbing doesn't currently seem to have a way to detect files with no extension. Every time someone opens an issue, they point it back to this issue here. They detail their globbing support here.

也就是说,我确实有一个解决方案.把它放在你的工作区设置"中(不是你的常规设置,除非你真的希望它是全局的).

That said, I do have a hacky solution to this. Put this in your "WORKSPACE SETTINGS" (not your general settings unless you really want this to be global).

{
    "files.associations": {
        "[!.]": "php",
        "[!.][!.]": "php",
        "[!.][!.][!.]": "php",
        "[!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php",
        "[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]": "php"
    },
}

这是通过为每个文件名长度添加规则并确保文件中不能有句点(在我的示例中最多 20 个字符)来实现的.这是一个可怕的hacky解决方案,但是如果您握住鼻子并设置一次,您可以忘记它并且一切正常.我在我的 dotfiles 存储库中执行此操作以将无扩展名的文件与shellscript"相关联.

This works by adding a rule for every file name length and ensuring that the file cannot have a period in it (up to 20 characters in my example). This is a horrible hacky solution, but if you hold your nose and set it once, you can forget it and things just work. I do this in my dotfiles repo to associate extension-less files with "shellscript".

您也可以使用双星通配符为特定目录执行此操作:"**/just_this_dir_name/[!.]": "php".

You an also do this just for a specific directory by using the double star glob: "**/just_this_dir_name/[!.]": "php".

这篇关于默认为 VS Code 上的语言分配没有扩展名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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