Automatically replace special characters by escape characters in URI link

What I’m trying to do


I want to paste links to local files into a note. Unfortunately my filenames contain a lot of special characters like #, (, ), … which seem to not work in URIs, so escape characters are required, as described here:

Because I have to insert a lot of links to local files, I have to do a lot of manual work to finde & replace all special characters by their corresponding escape-characters.

Now I wonder, if there is a way to be able to do that work automatically.

Things I have tried

There is this plugin “File path to URI”, which apparently is not under development anymore. This plugin could be used to automatically replace these characters, but as I don’t have the skills to modify that plugin, I don’t have any idea to proceed here…

Do you have any ideas?

Thanks in advance,
kind regards,


… it might be possible by using templater…

Here’s a completely not working script, that throws a lot of errors in the terminal :smiley: … as I said, I don’t have a lot of coding skills…



let text = tp.system.clipboard()


if (text.includes("#")) {
	text = text.replace(/#/g, "%23")



could anybody please help me correcting that script? :slight_smile:

Thanks in advance,
kind regards,


You could also do it with the Shellcommands plugin, and then use a regular expression replacement to replace whatever character you want.

I use this command to change _italics_ to *italics*:
sed -i -e 's/_\([a-zA-Z0-9]*\)_/*\1*/g' {{file_path:absolute}}.

Your case with # could look something like this: sed -i 's/#/%23/g' {{file_path:absolute}}

For now, I can only help with the structure of the Templater code. I cannot make time to look into all relevant character encodings or if they need double encoding, or ask about your operating system for treatment of slashes in the path, I’m afraid.

So your Templater script you can bind a hotkey to can look like this:

clipboard = (await tp.system.clipboard());
clipboard = clipboard.replace(/\s-\s/gm, " – ");
tR += clipboard;

In my trimmed example the clipboard = clipboard.replace(/\s-\s/gi, " – "); replaces dashes to n-dashes (when a dash is not a hyphen).
You’ll need to populate all your rules here. The stupidest ChatGPT bot can help with this. Just make sure you prompt it with your exact requirements (specify OS for correct path).
Feed it this snippet as well so the bots knows what to build its answers around.

When you fire the script, the changed content gets pasted in the editor with tR +=.

Also, there’s a great post about external links:

1 Like

Thanks a lot again gino_m!!! :slight_smile:
The script works perfectly fine!!!

I used this script:

clipboard = (await tp.system.clipboard());
clipboard = "<file:"+clipboard.replace("#", "%23")+">";
tR += clipboard;

to automatically fill in the URI-command, after I pressed “Strg + K” (= Insert Markdown link).

Enhancement of that script:

Now I’m wondering if I could combine both hotkeys, the “Strg + K”-Hotkey to insert a Markdown link on a selected string in Obsidian AND the script above.

I tried this on a “test-string” in Obsidian:

clipboard = (await tp.system.clipboard());
selection = (await tp.file.selection());
link = "[" + selection + "](<file:" + clipboard.replace("#", "%23")+">)";
tR += link;

but I got this result:


… That’s a very strange behaviour, I don’t have an explanation for, because it looks like the string “tp.file.selection()” has been saved in the variable “clipboard”…

Do you have an idea to combine both steps?

I’m a little vague on what you want to accomplish but one script can handle both clipboard and selection (not sure what you’d want to do with combining them).

The error on

clipboard = (await tp.system.clipboard());
selection = (await tp.file.selection());

is about having extra brackets before await and at the end. Slash these off.

Also, you can add more replacements beyond exchanging the # by adding more clipboard = clipboard.replace lines below one another.

1 Like

Thanks a lot gino_m!!!

This script:

clipboard = await tp.system.clipboard();
clipboard = clipboard.replace("%", "%25"); // has to be at the beginning, because the %-sign is part of the following escape-characters
clipboard = clipboard.replace("#", "%23");
clipboard = clipboard.replace("$", "%24");
clipboard = clipboard.replace("&", "%26");
clipboard = clipboard.replace("'", "%27");
clipboard = clipboard.replace("(", "%28");
clipboard = clipboard.replace(")", "%29");
clipboard = clipboard.replace("*", "%2A");
clipboard = clipboard.replace("+", "%2B");
clipboard = clipboard.replace(",", "%2C");
clipboard = clipboard.replace("/", "%2F");
//clipboard = clipboard.replace(":", "%3A"); -> is commented out, because the ":" is part of the filepath
clipboard = clipboard.replace(";", "%3B");
clipboard = clipboard.replace("=", "%3D");
clipboard = clipboard.replace("?", "%3F");
clipboard = clipboard.replace("@", "%40");
clipboard = clipboard.replace("[", "%5B");
clipboard = clipboard.replace("]", "%5D");
selection = await tp.file.selection();
link = "[" + selection + "](<file:" + clipboard+">)";
tR += link;

did the job.

Now I assigned the hotkey “Strg + K” to this script and deactivated that hotkey for “Insert Markdown link”.

Perfect! :slight_smile:

Did you try using the encodeURI()? It’s a standard javascript function to encode tricky characters.

I’ve not tried this, but I think the following should work:

const link = encodeURI( await tp.system.clipboard() )
const text = await tp.file.selection()
tR += "[" + text + "](file:<" + link + ">)"
1 Like

Thanks holroy, it works.
I didn’t know that function. In my opinion, the resulting link, produced by the encodeURI() function is not as good readable as the one, that will be created with my manual script above, so I’ll stick to that.

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