Sublime Text : Create Shortcut using Key Bindings

Be it a text-editor or any other tool, mastering keyboard shortcuts will really help you increase your productivity. In this article, I am gonna explain how to use Sublime Text key bindings to create keyboard shortcuts. Sublime text already features a host of keyboard shortcuts by default. But you can also create your own shortcut keys for operations that doesn’t have a default shortcut key.

Key Bindings in Sublime Text

A key binding in Sublime Text is a mapping of one or a combination of one or more keys in your keyboard to a command. Commands are named operations. For instance, re-indenting and converting selected text to title case are operations. reindent and title_case are their respective commands. So, once you know the command for which you want to create a keyboard shortcut, you can create a key binding. I have already compiled a complete list of Sublime Text commands, in my previous article.

To create a key binding, go to

On windows: Preferences > Key Bindings – User

On OS X: File > Preferences > Key Bindings – User

Single Key Shortcuts

You can bind a single key to a command. For example, you can use f12 as a shortcut key for re-indenting. Just add the following JSON object to the Key Bindings file.

 

[ 
{ "keys": ["f12"], "command": "reindent"} 
]

 

Combination Key Shortcuts

To create a combination key shortcut, just add a plus sign between the two key names. For example, to create a shortcut for the title_case command, you can do do as follows.

[ 
{ "keys": ["f12"], "command": "reindent"}, 
{ "keys": ["ctrl+t"], "command": "title_case"} 
]

 

Complex Combination Key Shortcut

After creating a couple of custom keyboard shortcuts, are you wondering how to create more complex keyboard shortcuts like the “Ctrl+Alt+R”,”Ctrl+Alt+B” you use to browse SFTP/FTP Server profiles? It’s possible.
You can add the second key combination to the keys array. Just like the one that follows.

[
	 { "keys": ["f12"], "command": "reindent"},
	 { "keys": ["ctrl+t"], "command": "title_case"},
	 { "keys": ["ctrl+k","ctrl+j"], "command": "swap_case"}
]

 

Shortcut Keys for commands that require arguments

If you have gone through the list of Sublime Text commands I have created,  you might have noticed that some commands require addition arguments. To create shortcut keys for these kinds of commands. We can add the args array.

[
	 { "keys": ["f12"], "command": "reindent"},
	 { "keys": ["ctrl+t"], "command": "title_case"},
	 { "keys": ["ctrl+p","ctrl+l","ctrl+r"], "command": "permute_lines", "args": {"operation":"reverse"}}
]
Sublime Text Custom Keyboard Shortcut

Sublime Showing the custom keyboard shortcut we just created

Best practices

  1. While creating keyboard shortcuts in Sublime Text, do not try to use existing shortcut combinations like Ctrl+C, Ctrl+V, etc.
  2. Prefer “Ctrl+Shift+<key>” or “Ctrl+Alt+<key>” over “Ctrl+Alt+Key1″,”Ctrl+Alt+Key2” type shortcuts as the former will usually be used by package developers.

 

I hope this helped you. Don’t hesitate to comment and let me know if I had missed anything in this tutorial.

3 Comments

  1. rohit das August 4, 2015
    • Narendran Parivallal August 4, 2015
    • Press Ctrl+Shift+P
    • Install Package -> Emmet
    • Once Emmet is installed,

    • Save your new document as an HTML file
    • Type html:5 and hit Tab
  • Sridhar August 17, 2015
  • Leave a Reply