"vim:filetype" equivalent in vscode

1.5k views Asked by At

Vim has a nice way of dictating what syntax highlighter shall be used with a specific file (disregarding the file extension by specifying the keyword vim:filetype in the beginning or at the end of the file.)

Is there an equivalent way of suggesting vscode to use a syntax highlighter with a file without paying attention to its file extension?

Example: in 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.

2

There are 2 answers

2
infinityzxx On BEST ANSWER

I know this question has been around for quite some time now but I thought I should share that this is now possible through a third party extension: https://marketplace.visualstudio.com/items?itemName=chrislajoie.vscode-modelines

I have not tested this myself but it has a lot of installs with absolutely no bad press so it should work properly.

Repo: https://github.com/ctlajoie/vscode-modelines

3
nicoespeon On

If you need to persist that

You can define file associations in VS Code settings. If you open the settings.json you can define a map like this:

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

I'd suggest you configure this in the project settings.json, so it only applies to these files.

I don't know a way to configure that from the file directly though.

If you need to do that punctually

You can change the language mode manually to use a different syntax highlighting for a specific file, regardless its extension. I detailed this in a recent blog post: https://www.nicoespeon.com/en/2019/12/vscode-syntax-highlight-on-copied-text/

There are different ways to do this, but I suggest you open the Command Palette (Ctrl+Shift+P) and type "Change Language Mode".

enter image description here

It will pop up the list where you can select the appropriate language.