Many of my notes consist of technical guides that include keyboard shortcuts. for example:
Show Explorer/Toggle focus: Ctrl + Shift + E
Wrapping the keys with HTML tag <kbd>
will render them initially to stand out and be more visually pleasing:
Show Explorer/Toggle focus: Ctrl + Shift + E
Now, typing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>E</kbd>
for each key is too much work, so I wrote a simple Templater script
<%*
const selectedKey = tp.file.selection();
if (selectedKey) {
tR = `<kbd>${selectedKey}</kbd>`;
} else {
tR = `<kbd></kbd>`;
}
%>
Running the template via Templater: Open Insert Template Modal will wrap the selected text in the note with <kbd>
tag, or simply bind it to a hotkey (In my case Ctrl + K). If text is NOT selected, it will simply insert <kbd></kbd>
into the cursor position.
Since <kbd>
is an HTML tag, it can be targeted with with a custom CSS snippet, as I wanted it to look more like a keyboard key. I created kbd.css
file with the following and placed it in folder .obsidian/snippets/
kbd {
/* Define custom properties for easy management */
--key-text-color: #6d6f76;
--key-shadow: inset 0 -2px 0 0 #cdcde6,
inset 0 0 1px 1px #fff,
0 1px 2px 1px rgba(30, 35, 90, 0.4);
--key-gradient: linear-gradient(-225deg, #d5dbe4, #f8f8f8);
/* Styling for <kbd> element */
border: none;
background: var(--key-gradient);
border-radius: 3px;
box-shadow: var(--key-shadow);
color: var(--key-text-color);
text-align: center;
padding: 0.2em 0.6em;
display: inline-block;
font-weight: bold;
font-size: 0.85em;
min-width: 1em;
}
Styling credits go to pipic1’s button
This will result in keys to look like this:
Note that the css snippt will style keyboard shortcuts in the command palette:
My editing flow of keys now is:
- I select the text of the key (using Shift+ keyboard arrows)
- Then use Ctrl + K to run the Templater’s template which will wrap the key text with
<kbd>
tags.