Sublime Forum

Tying a keymap to a project?

#1

Is there any way I can tie a keymap to a project? I’ve got a macro that is only used for one project, and I’ve set up a keymap to invoke it. I’d rather have that keymap only active when using the project it’s appropriate for, but from what I can see in the docs, there’s no way to include a keymap in the .sublime-project file.

0 Likes

#2

If you create a setting specific to your project, that setting will be applied to all of the views contained in the same window as the project. From there you can use that setting in a context in the key mapping so that it will apply only inside that project.

Note that project information is specific to each window, so to be considered part of the project the view has to be contained in the window where the project is loaded. That means that dragging the tab out into it’s own window or otherwise just having the file open isn’t enough to associate it with the project.

By way of example, this creates a project specific setting named my_custom_setting by adding it to the settings key of the sublime-project file and including the setting inside.

{
    "folders":
    [
        {
            "path": "."
        }
    ],
    "settings":
    {
        "my_custom_setting": true
    }
}

As soon as you do that, the setting is automatically applied to all views inside of the project window. With that in place you can create a key binding that only applies while the setting is an appropriate value. Here the tab key inserts the text <Project Tab Key> inside all views where the setting is set.

{ 
    "keys": ["tab"], 
    "command": "insert", 
    "args": {"characters": "<Project Tab Key>"},
    "context": [
        { "key": "setting.my_custom_setting", "operator": "equal", "operand": true },
    ]
},

It seems as if this only works with a boolean key though, so if you want to do this in more than one project you need to use different settings names instead of a setting with a string value of some description unless you also provide your own context handler for the key.

2 Likes