Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign file with no extension to a language on VS Code as default

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).

like image 509
Birbal Avatar asked Mar 21 '18 15:03

Birbal


People also ask

How do I set the default language mode in VS Code?

In VS Code, we default the language support for a file based on its filename extension. However at times you may wish to change language modes, to do this click on the language indicator - which is located on the right hand of the status bar. This will bring up the Command Palette for Select Language Mode.

How do I change my extension settings in VS Code?

You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X).


1 Answers

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".

like image 162
mattmc3 Avatar answered Sep 23 '22 03:09

mattmc3