Birbal
Birbal

Reputation: 353

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

Upvotes: 15

Views: 5412

Answers (3)

almaceleste
almaceleste

Reputation: 467

If you choose mattmc3's solution you can use pattern-list entry to write it in one line:

{
    "file.associations": {
        // all files up to 30 characters long without extension will be associated with `php`
        "{[!.],[!.][!.],[!.][!.][!.],[!.][!.][!.][!.],[!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}": "php"
    }
}

In example above, all files up to 30 characters long without extension will be associated with php.

You can shorten this entry with multiple pattern lists combined in one line, such as {...}{...}{...}. The example below works like the previous one, but it is much shorter:

{
    "file.associations": {
        // all files up to 30 characters long without extension will be associated with `php`
        "{[!.],[!.][!.],[!.][!.][!.],[!.][!.][!.][!.],[!.][!.][!.][!.][!.]}{[],[!.],[!.][!.][!.][!.][!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}{[],[!.],[!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.][!.]}": "php"
    }
}

Also, if you want to set this up for files in a directory where you only have root access, or you don't want to create a .vscode folder in it, you can create a rule in your user settings.json with the path in this rule, e.g.:

{
    "file.associations": {
        "/etc/**/{[!.],[!.][!.],...}": "shellscript"
    }
}

Another solution for files in specific directory is to use multiple rules:

{
    "file.associations": {
        "/etc/**/*": "shellscript",      // this rule will work on all files
        "/etc/**/*.list": "debsources",  // and this one to exclude .list files
        "*.list": "debsources"           // due to this rule won't work for .list
                                         // files in /etc/** directories
    }
}

This is because a more specific rule takes precedence over a more general rule.

Upvotes: 0

Hustlion
Hustlion

Reputation: 2373

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:

sample highlight result

Upvotes: 3

mattmc3
mattmc3

Reputation: 18345

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

Upvotes: 9

Related Questions