问题
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?
I know I can do this:
"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).
回答1:
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"
},
}
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".
You an also do this just for a specific directory by using the double star glob: "**/just_this_dir_name/[!.]": "php"
.
回答2:
Thanks to the closing of issue Expose 'change language' as command and the development of plugin Modelines, we can now use vim style modeline inside files without suffix and vscode can detect the file type.
Example:
filename: post-commit
file content:
#!/bin/sh
# vim: set ft=sh
echo "hello world linked"
After installing the Modelines extension, the file is highlighted properly:
来源:https://stackoverflow.com/questions/49410524/assign-file-with-no-extension-to-a-language-on-vs-code-as-default