Add "Copy Wikilink" command

This would be nice to have as a core feature. In the meantime, sharing some Templater scripts that serve me well:

Copy Link to Note

Copy_Link_to_Note.md (1.8 KB)

<%* 
/**
 * Template Name: Copy Link to Note
 * Description: Copies wikilink to current note. If disambiguation needed, presents modal with options: copy with parent folder, or copy with parent folder and alias.
 * Version: 2.0
 * Author: Created via Claude
 * Source: https://forum.obsidian.md/t/add-copy-wikilink-command/56121/11
 * Last Updated: 2024-12-09
 */

// Store current selection
const selection = tp.file.selection();

// Get current file info
const currentTitle = tp.file.title;
const fullPath = tp.file.path(true);

// Find other .md files with same basename, excluding current file
const filesWithSameBasename = app.vault.getAllLoadedFiles().filter(f => 
    f.basename === currentTitle && 
    f.path !== fullPath &&
    f.extension === 'md'
);

// Get immediate parent folder name if any
const parentFolder = fullPath.split('/').slice(-2, -1)[0];

// Handle disambiguation if needed
if (filesWithSameBasename.length > 0) {
    const options = [
        {
            text: `With parent folder: [[${parentFolder}/${currentTitle}]]`,
            value: `[[${parentFolder}/${currentTitle}]]`
        },
        {
            text: `With alias: [[${parentFolder}/${currentTitle}|${currentTitle}]]`,
            value: `[[${parentFolder}/${currentTitle}|${currentTitle}]]`
        }
    ];
    
    const choice = await tp.system.suggester(
        (item) => item.text,
        options,
        false,
        "Choose wikilink format"
    );
    
    if (choice) {
        await navigator.clipboard.writeText(choice.value);
        new Notice("Copied to your clipboard");
    }
} else {
    // No disambiguation needed, copy simple wikilink
    await navigator.clipboard.writeText(`[[${currentTitle}]]`);
    new Notice("Copied to your clipboard");
}

// Restore selection
tR = selection;
%>

Copy Note Embed

Copy_Note_Embed.md (1.1 KB)

<%* 
/**
 * Template Name: Copy Note Embed
 * Description: Copies embed wikilink to current note. If disambiguation needed, adds parent folder.
 * Version: 2.0
 * Author: Created via Claude
 * Source: https://forum.obsidian.md/t/add-copy-wikilink-command/56121/11
 * Last Updated: 2024-12-09
 */

// Store current selection
const selection = tp.file.selection();

// Get current file info
const currentTitle = tp.file.title;
const fullPath = tp.file.path(true);

// Find other .md files with same basename, excluding current file
const filesWithSameBasename = app.vault.getAllLoadedFiles().filter(f => 
    f.basename === currentTitle && 
    f.path !== fullPath &&
    f.extension === 'md'
);

// Get immediate parent folder name if any
const parentFolder = fullPath.split('/').slice(-2, -1)[0];

// Copy appropriate embed link format
if (filesWithSameBasename.length > 0) {
    await navigator.clipboard.writeText(`![[${parentFolder}/${currentTitle}]]`);
} else {
    await navigator.clipboard.writeText(`![[${currentTitle}]]`);
}

// Restore selection
tR = selection;

// Show notification
new Notice("Copied to your clipboard");
%>

Copy Link as Footnote

Copy_Link_as_Footnote.md (1.1 KB)

<%* 
/**
 * Template Name: Copy Link as Footnote
 * Description: Copies link to current note as ^[[[Title]]]. If disambiguation needed, adds parent folder.
 * Version: 1.0
 * Author: Created via Claude
 * Source: https://forum.obsidian.md/t/add-copy-wikilink-command/56121/11
 * Last Updated: 2024-12-07
 */
 
// Store current selection
const selection = tp.file.selection();

// Get current file info
const currentTitle = tp.file.title;
const fullPath = tp.file.path(true);

// Check for duplicates among all files
const hasDuplicate = app.vault.getAllLoadedFiles().some(f => 
    f.basename === currentTitle && f.path !== fullPath
);

// Get immediate parent folder name if any
const parentFolder = fullPath.split('/').slice(-2, -1)[0];

// Determine the appropriate wikilink format with inline footnote syntax
const wikilink = hasDuplicate 
    ? `^[[[${parentFolder}/${currentTitle}]]]`
    : `^[[[${currentTitle}]]]`;

// Copy to clipboard
await navigator.clipboard.writeText(wikilink);

// Restore selection
tR = selection;

// Show notification
new Notice(`Copied to your clipboard`);
%>

Copy Full Link

Copy_Full_Link.md (546 Bytes)

<%*
/**
 * Template Name: Copy Full Link
 * Description: Copies full path to current note from vault root as wikilink.
 * Version: 1.0
 * Author: Created via Claude
 * Source: https://forum.obsidian.md/t/add-copy-wikilink-command/56121/11
 * Last Updated: 2024-12-08
 */
 
const selection = tp.file.selection();
const path = tp.file.path(true);
const pathWithoutExt = path.replace(/\.[^/.]+$/, "");
const wikilink = `[[${pathWithoutExt}]]`;
await navigator.clipboard.writeText(wikilink);
tR = selection;
new Notice("Copied to your clipboard");
%>

Each can be added to the file menu with Commander:

Screenshot


Also, Text Transporter adds a Send link of current note to the Clipboard command to the ribbon, Command Palette and when right-clicking in note.

1 Like