Sublime- assign custom-keyboard shortcut for code-block while editing markdown file

(Very short blog on adding a custom keybinding to multi-line code block)

A very quick post for a small time-saving trick in Sublime.

While editing a markdown file having an option to direclty invoke a code block like below with a keyboard shortcut is immensely time saving.

```js
```

And as of August-2020 in Sublime, I could not find a keyboard shortcut for adding code-blocks either natively from Sublime’s in-built keybinding or an option to add one from in-built commads.

So I am taking help of a third party package Markdown Code Blocks and will add a keyboard shortcut of ‘Ctrl + Shift + z’ for multi-line code block (you can ofcourse assign any other Keyboard combinations to it).

First install the package (Markdown Code Blocks) from Command palette using Package Control

Ctrl + Shift + P to open palette > Type ‘Install Package’ and choose the one you see below selected and then search for search for Markdown Code Blocks

Image for post
Image for post

A command will appear in your command palette.
Search for the Markdown Code Blocks: Add Block command, and type in your desired language name in lowercase letters. And you will see something like below for a .js code formatting

```js
```

Now time to add a keybinding to invoke the above.

First to know which is the relevant Sublime command (which I have to invoke with a keybinding), after installing Markdown Code Blocks open Command Palette > start typeing ‘code blocks’ like below and you will the see the relevant command is this ‘Markdown Code Blocks: Add Block’

Image for post
Image for post

So all I had to do is adding a keybinding to that.

Now nevigate to the Github source code of the Markdown Code Blocks package which is https://github.com/molnarmark/markdowncodeblocks

And search for the string ‘Code block’ in this repo. And I get the below result.

Image for post
Image for post

From the above I can see that the command that I am interested is the below one.

Image for post
Image for post

So now from Sublime > Command Palette > Key Bindings > Open my .sublime-keymap file and include the below command

{ "keys": ["ctrl+shift+z"], "caption": "Markdown Code Blocks: Add Block", "command": "markdown_add_code_block" },

And that’s it, now I can invoke the "Markdown Code Blocks: Add Block" command with "ctrl+shift+z"

Written by

MachineLearning | DataScience | DeepLearning. Previously Frontend Engineer. Ex International Banking Analyst. https://www.linkedin.com/in/rohan-paul-b27285129/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store