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.
Good Themes
- Cyanide
- Seti
- Seti_UX
Key Bindings: /home/wuseman/backup/sublime-text/Packages/User/Default (Linux).sublime-keymap
[
//
// --- BASIC BLOCKS ---
//
{
"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```" }
},
//
// --- MKDOCS / ADMONITIONS ---
//
{
"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>"
}
},
//
// --- MARKDOWN TITLE ---
//
{
"keys": [",", "m", "t"],
"command": "insert_markdown_title"
},
//
// --- SURROUND WITH BACKTICKS ---
//
{
"keys": [",", ","],
"command": "surround_with_backticks",
"args": { "language": "bash" }
},
{
"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/Theme - Cyanide/Cyanide - Purple.tmTheme",
"theme": "Cyanide - Wood.sublime-theme",
"font_face": "Hack",
"font_size": 12,
"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,
"hot_exit": "always",
"close_deleted_files": true,
"linters": {
"shellcheck": {
"executable": "shellcheck",
"exclude": ["SC2034", "SC2086"],
},
},
"lint_mode": "background",
}
Plugins
Tools → Developer → New Plugin…
Replace everything with:
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
Preferences→Key Bindingsand add:
How to insert markdown title by keybinding
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
Add `"!!! Example 'text here'" quotes around any single line
Tools → Developer → New Plugin…
Replace everything with:
import sublime
import sublime_plugin
class WrapLineExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
line_region = self.view.line(region)
line_content = self.view.substr(line_region)
# Replikera substitute(l:line, '"', '\\"', 'g')
escaped = line_content.replace('"', '\\"')
# Replikera setline('.', '!!! Example "' . l:line . '"')
self.view.replace(edit, line_region, f'!!! Example "{escaped}"')
- Save it as:
wrap_line_example.py
Then add the shortcut to your Key Bindings
Resource(s)