Sublime Forum

Keyboard shortcut for opening file in editor


There are UI buttons for opening a file in an editor and revealing it in finder, but I didn’t find any way to use them with keyboard shortcuts.



You can always create your own key binding, but I’m not sure if you can determine which file to open in an editor from a keybinding.

1 Like


I placed a Default.sublime-keymap file in the Data/Packages/User folder and its content is:
[ { "keys": ["ctrl+space"], "command": "toggle_side_bar" } ]
It doesn’t seem to be working…

1 Like


It works fine here. Do the menu commands in View > Side Bar work?



Yeah, the toggle works, it just looks that my shortcut is completely ignored. Not sure if its an indicationg but the hint when I hover on top of the ‘toggle sidebar’ button still indicates the default shortcut



Does it work any better if you use Preferences > Key Bindings and add it to the platform specific key bindings instead of Default.sublime-keymap?

Default.sublime-keymap is loaded first and Default (<Platform>).sublime-keymap is loaded second and overlaid on top, so if the platform specific key bindings also contains a binding to that key, that would mask it from doing what you want.



I don’t see the Preferences > Key Bindings on build 1107 of Sublime Merge. All I’m trying to do is remap a keyboard shortcut (ctrl+space) to show/hide the sidebar. Shouldn’t that be done through the Default.sublime-keymap file?



Oh my bad, I didn’t notice that this was in the Merge section of the forum.

Generally speaking, key bindings tend to differ by platform based on platform specific rules; for example many on MacOS use the command key while on Windows they would use Ctrl or Alt, and so on. So although keys can exist in the Default.sublime-keymap file, they’re generally put into a platform specific one instead. That allows people that sync their settings across multiple computers to not mess up bindings.

In any case, the problem that you’re having most likely stems from the fact that toggle_side_bar is a Sublime Text command for toggling the side bar (which is why I thought this was a text question). The command for doing this in Merge is toggle_location_bar. I’d try that in the file that you’re using and see if that works better.



Using toggle_location_bar worked but I wonder why, in the Default (Windows).sublime_keymap package in the Packages directory of the install directory, there’s the instruction { "keys": ["ctrl+k", "ctrl+b"], "command": "toggle_side_bar" }, on the 13th line… Is there a reason why that wouldn’t say toggle_location_bar instead? Or am I misinterpreting/missing something?



There is a Default.sublime-package in that folder and there’s also a Default - Merge.sublime-package as well.

The Default.sublime-package contains various resources that seem to be a blend of the resources for Text and Merge together (for example the context menu for branches is in there, but so is the menu file that describes encodings) while the Merge specific one has key bindings and preferences in it only.

I can’t recall offhand if it will load files first from Default and then from Default - Merge or if it only loads certain resources from certain places (my instinct is the first one). In any case, in order to get the full picture it’s helpful to look in the other package as well.



So someone knows what the “open in editor” command is?



Based on looking in the packages outlined above, the menu entries that execute this command look like this:

    "caption": "Open in Editor…",
    "command": "open_in_editor",
    "args": { "path": "$working_dir/$path", "line": "$line", "col": "$col" }

This entry opens the file that you’re currently looking at. The command can be used to open any file that you want so long as you already know the path; for example it’s used to allow you to edit the .gitattributes file or to edit settings directly.

In such a case, as seen here you can also pass a default parameter to set what text the file should contain by default if it doesn’t already exist:

    "caption": "Edit .gitattributes…",
    "command": "open_in_editor",
    "args": {
        "path": "$working_dir/.gitattributes", "line": "$line", "col": "$col",
        "default": "# Specify filepatterns you want to assign special attributes.\n\n"
1 Like