Associate a file type with a specific syntax highlighting in the text editor

Hello,
I’ve been running jupyter-lab in a cluster and using it in my local computer (ssh tunneling) and that’s been working great. My problem is that I’m editing a lot of snakemake files (workflow manager written in python), usually with a .smk file extension and every time I open a file, I have to go to the menu:
view --> text editor syntax highlighting --> python. I saw in the Advanced settings editor that you can create shortcuts, so I tried to create a shortcut to highlight these files using the python syntax, but there aren’t examples relevant to my case in the already created shortcuts and I failed to do it. Also, i don’t know if it’s possible to associate a x file extension with a specific language, so you can open it and have a “proper” syntax highlighting, which would be even better than a shortcut.

I’m running jupyter-lab 0.35.4

Cheers

1 Like

Hi @matrs, you should be able to do this with the following keyboard shortcut in the advanced setting editor:

{
       "shortcuts": [
        {
            "command": "codemirror:change-mode",
            "keys": ["Accel P"],
            "args": { "name": "Python" },
            "selector": ".jp-FileEditor"
        }
    ]

}
1 Like

Hey, thanks. I tried but isn’t working, I tried different "keys" like Ctrl Y because Ctrl p or P are shortcuts for printing in the browser. Checking the existent shortcuts, all look like this

"application:activate-previous-tab": {
      "command": "application:activate-previous-tab",
      "keys": [
        "Ctrl Shift ["
      ],
      "selector": "body",
      "title": "Activate Previous Tab",
      "category": "Main Area"
    },

so aren’t those keys, title, category, mandatory?

Ah, the format changed between 0.35 and the current 1.0 prerelease (which is where I tested mine). Nevertheless, I think the same command ID, args, and selector should work.

That suggestion from @ian-r-rose still works for me (as of jupyter lab 2.2.2):

{
    "shortcuts": [
        {
            "command": "codemirror:change-mode",
            "keys": [
                "Shift Ctrl Alt P"
            ],
            "args": { "name": "Python" },
            "selector": ".jp-FileEditor"
        },
    ]
}