Search code examples
sasvisual-studio-code

VS Code default language of new file


I want all new files in VS Code to open as .sas files.

Attempt 1

  1. Open a .sas file in VS Code.
  2. ctrl + , to open settings UI.
  3. Search for files, then click Files (19) in the left menu.
  4. Insert sas or ${activeEditorLanguage} into "Files: Default Language" box.
  5. Close the settings window.
  6. Press ctrl + n to open a new file.
  7. Bang head on desk since the new file is still a .txt file instead of .sas

Attempt 2

  1. Open a .sas file in VS Code.

  2. ctrl + , to open settings UI.

  3. Search workbench.settings

  4. Change Settings: Editor to json

  5. Close the editor

  6. ctrl + , to open settings file in JSON.

  7. Become surprized because I see so few settings to modify (I would have expected more settings in this file):

    {
        "editor.insertSpaces": false,
        "editor.wordWrap": "on",
        "workbench.settings.editor": "json"
    }
    
  8. Add lines to the file to try to dictate that new files should open with sas syntax highlighting and with the .sas extension, save and close:

    {
        "editor.insertSpaces": false,
        "editor.wordWrap": "on",
        "workbench.settings.editor": "json",
        "files.associations": {
            "*.sas": "sas"
        },
        "files.defaultLanguage": "SAS"
    }
    
  9. Press ctrl + n to open a new file.

  10. Bang head on desk since the new file is still a .txt file instead of .sas

When using ctrl + , to open the settings editor in json, where are the rest of my settings including the default language settings?

Other info

I looked at this link, but the solution did not help.

I do not think it matters, but I am using the SAS-Syntax extension for my syntax highlighting.


Solution

  • Looking through the SAS language extension repository, the extension provides the following language in the package.json

    "languages": [
        {
            "id": "SAS",
            // the rest of the language entry
        }
    ]
    

    This means the language ID inside Code is indeed "SAS". Since the IDs are case-sensitive, using "sas" will not work. You can also see the ID in parentheses when changing the language mode of a file:

    VS Code language mode picker

    That means your settings entry "files.defaultLanguage": "SAS" is correct. I have tested it on a clean VS Code installation, and it worked as expected.

    Also because of this, your file association will not work. When Code doesn't find the specified ID, it will fall back to plain text. You need to change your "files.associations" settings to

    "files.associations": {
        "*.sas": "SAS"
    }
    

    File's language mode

    File's language mode determines how Code handles syntax highlighting and code snippets.

    Note that the language mode is not indicated by the icon displayed on the file's tab. The SAS extension doesn't support a custon icon and will use the icon of plain text files. The current language mode is displayed on the status bar in the bottom right:

    Status bar showing the language mode of the current file

    You can manually select a language for a file with the "Change Language Mode" command, which is by default bound to the Ctrl + K, M shortcut.

    More about Code's settings files

    The Code's settings.json file is so small for you, because Code stores only the settings that are changed over the default ones. There is no default settings file, as the settings are hard-coded in the source code, but you can have Code generate the file for you.

    1. Press Ctrl + Shift + P or F1 to open the Command Palette

    2. Type in "default settings"

    3. Select "Preferences: Open Default Settings (JSON)" from the list

    Speaking of the Code's Command Palette, it is an incredibly powerful tool that I recommend looking into. You can use it to perform nearly any action in the editor just by searching for its name. For example, you can quickly open both the UI and JSON settings with the appropriate command.

    EDIT:

    To address the question in the comments, Code uses 3 levels of settings:

    • Default settings - are hard-coded in the editor and can't be changed.
    • User settings - on Windows are located in the %AppData%\Code\User\settings.json file (for other systems see the Code's documentation) and apply to all files and projects opened inside Code.
    • Workspace settings - are located in the project's root in the .vscode\settings.json file and apply to all files when a workspace with the workspace settings file is opened.

    The settings are applied sequentially from default to workspace. If any key is set in multiple files, only the last one will take effect.

    How would I specify the language for a specific projects instead of the entire workbench?

    Create a .vscode\settings.json file in the project's root and place your settings there. If you prefer to use the UI settings instead, you can select the "Workspace" tab to save your settings to the workspace settings file instead.

    enter image description here

    How would I open a similar file in a way to make changes?

    Unfortunately, Code doesn't allow editing or saving the defaultSettings.json file, so the only way to copy it is by copy-pasting the contents. Fortunately, that isn't needed - you can override any of the default settings in the user or workspace settings files.