Files
bobbie-vault/Templates/Wikilink select and create.md
2026-03-02 17:06:32 +00:00

1012 B

<%* const selection = tp.file.selection(); if (selection) { const editor = app.workspace.activeLeaf.view.editor; const replacement = [[${selection}|${selection}]];

// 1. Perform the replacement via tR for clean Templater execution
tR = replacement;

// 2. Use a tiny delay to allow Obsidian to finish rendering the new link
setTimeout(() => {
    const cursor = editor.getCursor();
    const line = cursor.line;
    const ch = cursor.ch;

    // 3. Calculate position relative to the cursor (which is now at the end of the link)
    // [[Target|Display]]
    //   ^----^ (this is what we want to select)
    
    const endCh = ch - (selection.length + 3); // Moves back past "]]" and the display text
    const startCh = endCh - selection.length;  // Moves back past the target text

    editor.setSelection({ line, ch: startCh }, { line, ch: endCh });
}, 20); // 20ms is usually enough to bypass the "Live Preview" rendering jump

} _%>