Aliased link snippet w/ Templater

What I’m trying to do

I’m trying to speed up the creation of aliased links by means of a template shaped like [[${cursor}|${selection}]]. I.e., the text that I’m currently selecting should be to the right of the pipe, and the input cursor should be to its left.

Things I have tried

I’ve created a template which just contains this:

<% `[[${tp.file.cursor(0)}|${tp.file.selection()}]]` %>

Now, when I have some text selected and I run the command Templater: Open insert template modal (Alt + E) and select the template I’ve made, it does ALMOST what I need, but the cursor ends up to the left of the whole thing, before the square brackets.

What you’re doing should work, I’m confused as to why it’s not.

You can use this instead:

<%*
const editor = app.workspace.activeLeaf.view.editor
const selection = editor.getSelection() ? '|' + editor.getSelection() : ''
editor.replaceSelection(`[[${selection}]]`)
const cursor = editor.getCursor()
cursor.ch -= (selection.length + 2)
editor.setCursor(cursor)
%>

I’ll try this now. But did my solution work for you? I wonder if it’s just me.

No, yours didn’t and I don’t know why. I would have thought it would.

Yep, rich-text editors are a nightmare to create and maintain in the best of times…

Your template works for me just fine. Two possible things

  1. Another plugin might be interfering. Does this behavior still occur in the sandbox vault with only templater installed?
  2. Are you inserting into a particularly long file? There is an open bug report regarding files that have greater than 65,536 characters - link

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.