Obsidian+PR Cookbook - π - YouTube - Official Obsidian Docs
I like snippets. Snippets works by replacing a word that you just typed with a piece of text. I like snippets so much that I defined my own snippet library for zshell.
Much as organizing as you go is a key part of using Obsidian, Iβve found that defining snippets as you go is a good way of actually getting around to creating snippets. Obsidian has a plugin for snippets called Text Snippets that works well enough but defining new snippets was just a bit too much effort for me to make it βautomaticβ. Fortunately a little tweaking with Plugin REPL got me what I wanted - although the solution involved a little more "magicβ that I would like.
I defined the following two commands, which I developed within Obsidian itself.
newCommand(async function expand_or_create_snippet () {
let before = lineAtPoint()
command('text-snippets-obsidian:text-snippets')
if (before != lineAtPoint()) {
return
}
let name = wordAtPoint()
await openSetting("Text Snippets")
let el = document.evaluate("//*[text()='Text Snippets - Settings']/ancestor::div/descendant::textarea", document).iterateNext()
let replacement = await promptString()
el.value = el.value +"\n" + `${name} : ${replacement}`
el.dispatchEvent(new Event("input"))
app.setting.close()
command('text-snippets-obsidian:text-snippets')
})
newCommand(async function open_snippet_settings() {
openSetting("Text Snippets")
})
The first command command
Tries to expand a snippet
If it fails to expand then it open the settings
Find the textbox to in the snippets plugin where snippets are defined
Prompts for a new snippet and inserts it into the textbox
(Slightly magic) ensures that Obsidian detects that the value has changed
Closes settings and expand the snippet
The second command open the settings for if I want to change snippets.
Here is an example of the commands in use.
This will probably be enough for me to use now. I may well define some commands to change snippets later - and I might end up just removing text snippet entirely since the functionality provided is quite simple. I might even end up using Templater templates instead. But itβs good enough for now - which is one of the philosophies of organize as you go.
Regardless, this is an example of the more hacky things that you can do with Plugin Repl.
This page is not affiliated with Obsidian. I am the author of Plugin REPL