Description
Designed for keyboard users who prefer to copy/paste path instead of using mouse to drag the reference in the note.
Insert a local link (outside your vault) pointing to a file or a folder in your PC. Only work in editing mode of course and only if the clipboard text is detected as a valid Windows or Unix path.
For instance, if your link is E:\OneDrive\My Documents\Any folder
, it inserts as:
[E:\OneDrive\My Documents\Any folder](<file:///E:\OneDrive\My Documents\Any folder>)
This pattern [Link Title](<link>)
instead of [Link Title](link)
allows Obsidian to open the link with no need of further url transformation such as replacing \
with %5C
or space
with %20
or any non English chars. As a result, the format is more readable than the one inserted when you ctrl+drag
a folder or file in your note from the files explorer. Especially if you use Windows path and the path contents spaces and non English chars.
If the link points to a folder, it opens with the default files explorer in your system.
If the link poins to a file, it opens with the default app set for this extension on your system.
After the link is inserted, the cursor is moved to its start to make it easier to edit the title if needed.
Prerequisite
You need the templater plugin.
You need to set the plugin with those options:
- Template folder location: put a path existing in your vault (usually
Templates
) - Automatic jump to cursor
- Script files folder location: put a path existing in your vault
Setup
Create the script
In your script files folder location set above (I set mine to MyScripts/Templater
), create a Insert_local_url.js
file with this content:
const prefix = "Insert_local_url";
function Insert_local_url() {
log("START");
const clipText = electron.clipboard.readText().trim();
if (!clipText) {
log("clipboard is empty or doesn’t content a text");
return;
}
if (!isLocalPath(clipText)) {
log("The clipboard doesn’t content a local path.");
return;
}
log(`clipText: ${clipText}`);
const url = `[${clipText}](<file:///${clipText}>)`;
log("clipText can be successfully inserted");
return url;
}
module.exports = Insert_local_url;
function log(msg) {
console.log(`[${prefix}] ${msg}`);
}
// return boolean if path is a local path
function isLocalPath(path) {
// Expression régulière pour les chemins de fichiers Windows
const windowsPathPattern =
/^[a-z]:\\(?:[^\\\/*:?<>|"\r\n]+\\)*[^\\\/*:?<>|"\r\n]*$/im;
// Expression régulière pour les chemins de fichiers Unix (Linux/Mac)
const unixPathPattern = /^(\/[^\/\0]+)+\/?$/im;
return windowsPathPattern.test(path) || unixPathPattern.test(path);
}
Create the template
In your Templates
folder used by templater, create a file named like snippet add link to local file
and insert this:
<% tp.file.cursor(1) %><% tp.user.Insert_local_url() %>
Summary
How it is organized in my vault.
Use
When you’re editing a note and want to insert a local path copied in your clipboard:
Option 1
- open the command palette or use the user defined shortkey to run the templater command
Open insert template modal
. - Start to type
local
and press enter to use your template namedsnippet add link to local file
.
Option 2 (the fast way)
If you use this action a lot, you could add a shortkey to directly insert the link in your clipboard. Check the templater settings Template hotkeys
. I set mine to ctrl+alt+U
.
Like if you appreciated this tip.