Built in hotkeys and tons of plugins. Quickadd in particular. I am not sure if Quickadd would do what I want since it seems to only replace after a certain line, usually a header.
What I’m trying to do
Map a hotkey/macro to insert a tag in the frontmatter. For example I have my header:
tags: [test1,test2,test3]
I would like to be able to press a hotkey to insert a tag after ‘test3’. Ideally with some kind of popup asking what I would want the inserted tag to be. So if I entered ‘test4’ I would want the frontmatter to now appears as:
This doesn’t completely answer your question as you’ll need to add a template around this, but I have a very concise function for updating frontmatter:
function updateFrontmatter(contents, field, value) {
const f = contents.match(/^---\n(.*?)\n---\n(.*)$/s)
const v = `${field}: ${value}`
const x = new RegExp(`^${field}:.*$`, 'm')
const [s, e] = f ? [`${f[1]}\n`, f[2]] : ['', contents]
return f && f[1].match(x) ? contents.replace(x, v) : `---\n${s}${v}\n---\n${e}`
}
So if you can get the array of existing tags, then you can use the above function in a Templater script to update the new frontmatter.
Something like this:
let tags = ['existing', 'tags']
// Pop up a dialog box to get the new tag
const newTag = await tp.system.prompt()
tags.push(newTag)
// Update the final set of tags
const file = app.vault.getAbstractFileByPath(page.file.path)
let contents = await app.vault.read(file)
contents = updateFrontmatter(contents, 'tags', tags.join(','))
app.vault.modify(file, contents)
I am trying to append, not replace. The idea is rather than move the cursor and then type. I use a hotkey with a prompt to enter text, that appends the tag on the end, and I never needed to move my mouse. Fairly small as an inconvenience but something I would like to do if possible.
Ah, I hadn’t fully explored templater. This seems like the right direction one way or the other. I can barely get the gist of that JS as I’m not really a dev or coder in any way. I’m not even 100% sure how to implement this but I’ll try to read the docs on that. If I use your two code snippets in a template should that be all I need to accomplish that?
@CawlinTeffid’s solution is actually quite genius for a non-coding solution. It’s pre-pending not appending, but the end result is still that you get the array of tags increased.