Skip to content

Sublime Text: Custom Keymappings for Markdown

Discover custom keymappings for Sublime Text tailored for Markdown editing, including shortcuts for inserting Bash code blocks and examples effortlessly.


Get menu opened

ctrl+shift+p

Install Package Control

ctrl+shift+p
install package

Open Package Control

Preferences > Package Control 

Good Themes

  • Cyanide
  • Seti
  • Seti_UX

Select multiple lines

alt+shift+arrow_keys

Key Bindings: /home/wuseman/backup/sublime-text/Packages/User/Default (Linux).sublime-keymap

[
    {
        "keys": [",", "b", "b"],
        "command": "insert_snippet",
        "args": { "contents": "```bash\n$0\n```" }
    },

    {
        "keys": [",", "b", "c"],
        "command": "insert_snippet",
        "args": { "contents": "### $0\n\n```bash\n\n```" }
    },
    {
        "keys": [",", "m", "e"],
        "command": "insert_snippet",
        "args": {
            "contents": "!!! Example \"\"\n\n    ```bash\n    $0\n    ```"
        }
    },
    {
        "keys": [",", "m", "i"],
        "command": "insert_snippet",
        "args": {
            "contents": "!!! Info \"\"\n\n    ```bash\n    $0\n    ```"
        }
    },
    {
        "keys": [",", "m", "d"],
        "command": "insert_snippet",
        "args": {
            "contents": "!!! Danger \"\"\n\n    ```bash\n    $0\n    ```"
        }
    },
    {
        "keys": [",", "m", "w"],
        "command": "insert_snippet",
        "args": {
            "contents": "!!! Warning \"\"\n\n    ```bash\n    $0\n    ```"
        }
    },
{
    "keys": [",", "m", "r"],
    "command": "insert_snippet",
    "args": {
        "contents": "!!! Info \"Resource(s)\"\n\n    * <$0>"
    }
},
    {
        "keys": [",","m","t"],
        "command": "insert_markdown_title"
    },
  {
    "keys": [",", "ä"],
    "command": "surround_with_backticks",
    "args": { "language": "bash" }
  }
]

Settings: ~/backup/sublime-text/Packages/User/Preferences.sublime-settings

{
    "ignored_packages":
    [
        "Vintage",
    ],
    "index_files": true,
    "color_scheme": "Packages/Seti_UX/Seti.tmTheme",
    "theme": "Seti_orig.sublime-theme",
    "font_face": "terminus",
    "font_size": 14,
    "word_wrap": "false",
    "ui_scale": 1.1,
    "hardware_acceleration": "opengl",
    "show_encoding": false,
    "show_line_endings": false,
    "show_indentation": true,
    "show_syntax": true,
    "line_numbers": true,
    "highlight_line": true,
    "gutter": true,
    "margin": 4,
    "tab_size": 4,
    "mini_diff": "auto",
    "spell_check": false,
    "line_numbers": true,
    "auto_indent": true,
    "detect_indentation": true,
    "auto_match_enabled": true,
    "dictionary": "Packages/Language - English/en_US.dic",
    //
    // Application Behavior Settings
    //

    // Exiting the application with hot_exit enabled will cause it to close
    // immediately without prompting. Unsaved modifications and open files will
    // be preserved and restored when next starting.
    //
    // Hot exit has different modes to choose from:
    // - "always": Always perform a hot exit when the application exits. This
    //             includes when the last window is closed on relevant
    //             platforms.
    // - "only_on_quit": Only perform a hot exit when the application is asked
    //                   to exit, not when the last window is closed. This
    //                   setting is only used on Windows and Linux.
    // - "disabled": Disable hot exit.
    "hot_exit": "always",

}

Plugins

Enale surrond with backtips by pressing (swedish keyboard layout)

import sublime
import sublime_plugin


class SurroundWithBackticksCommand(sublime_plugin.TextCommand):
    def run(self, edit, language="bash"):
        view = self.view
        sel = view.sel()
        if not sel:
            return

        # Use first selection/caret
        pt = sel[0].begin()
        cur_row, _ = view.rowcol(pt)

        max_row, _ = view.rowcol(view.size())

        def line_text(row):
            r = view.line(view.text_point(row, 0))
            return view.substr(r)

        # Move up to first blank line (or top)
        up = cur_row
        while up > 0 and line_text(up) != "":
            up -= 1

        # Move down to first blank line (or bottom+1 sentinel)
        down = cur_row
        while down <= max_row and line_text(down) != "":
            down += 1

        # If current line is non-empty, adjust to the actual content block
        if line_text(cur_row) != "":
            up += 1
            down -= 1

        if down - up >= 0:
            # Compute insertion points
            top_line_region = view.line(view.text_point(up, 0))
            bottom_line_region = view.line(view.text_point(down, 0))

            top_insert_pt = top_line_region.begin()
            bottom_insert_pt = bottom_line_region.end()

            # Insert bottom fence first so top insertion doesn't shift it
            view.insert(edit, bottom_insert_pt, "\n```")
            view.insert(edit, top_insert_pt, f"```{language}\n")
  • Save it as surround_with_backticks.py

Bind it to

  • Open PreferencesKey Bindings and add:
[
  {
    "keys": [",", "ä"],
    "command": "surround_with_backticks",
    "args": { "language": "bash" }
  }
]

How to insert markdown title by keybinding

Go to: Tools → Developer → New Plugin…

Replace everything with:

import sublime
import sublime_plugin
import datetime


class InsertMarkdownTitleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        current_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        content = (
            "---\n"
            f"date: {current_date}\n"
            "title: \n"
            "description: \n"
            "hide: toc\n"
            "#level: classified\n"
            "---\n\n"
            "# \n\n"
            "***\n\n"
            "### \n\n"
            "```bash\n\n"
            "```\n"
        )

        self.view.insert(edit, 0, content)

        # Move cursor roughly like `3G8l`
        pt = self.view.text_point(2, 8)
        self.view.sel().clear()
        self.view.sel().add(sublime.Region(pt))

Save it as: insert_markdown_title.py

After insert_markdown_title.py is created go to Preferences > Key Bindings and add below

{
    "keys": [",","m","t"],
    "command": "insert_markdown_title"
}