What I’m trying to do
- I want to select many lines and make each its own separate link.
- Using the shortcut I can create a link from one line. But if I select multiple lines they all become one link.
You can do this by holding down the option key to create multiple cursors at the start and then end of each line:
Another option is to first make a backup, then use the Replace functionality within VSCode. Regex will need to be enabled. To do this, click on the
.* icon in the dialog (see image at the bottom). Then in the search field at the top, add:
And, in the lower field, add:
To be clear, I am definitely not an expert. But, I will explain the regex, since it isn’t overly complicated. And, like anything else on a computer (and in life), beware that there could be unintended results, so make a backup before experimenting. Anyways, while it isn’t necessary to fully understand the regex, it may be useful to help avoid doing something destructive, or to potentially use as a starting point for anyone new to regex. See this link: Regular Expressions in Obsidian (copied from mdn web docs) for more information about regex in Obsidian search.
Within the search field at the top, the regex matches a new line with
\n. Then the parentheses are used to contain the text that matches the regex within the parentheses. This is a capturing group. Within the parentheses, the first
. is there to make sure that there is at least one character on the line. Then, the
.* will continue to select anything for the rest of the note. In order to prevent this, the
$ is added. The
$ causes the regex to only match the preceding pattern until the end of the line.
Within the replace field on the bottom, the opening brackets are added. Then, the
$1 returns the text that was matched within the first (and only) capturing group of the search. So, the entire text from each line are returned. The closing brackets are added next. And, finally, so that all of the links are not returned as a continuous string, the
\n is added. This starts a new line for each of the replacements.
It should look like the attached image here:
I hope this helps! Good luck! Be careful!
For thousands of lines? Have you written those yourself, or are they generated? If the latter, you should look into generating them as links in the first place.
Another option for those kind of amount of lines, I would also consider defining a template to do all the work for you. Let it split the selection into multiple lines, and insert a link for each line.
Great idea! I would love to see how this could be accomplished using Templater.
Also, I agree that simplifying the process by having the brackets added in the first place seems to be the most logical long term solution. But, depending on the source, that might not be an option. A possible alternative would be to craft a search that matches most or all of these notes. Then, using the Copy Search Results command within the Search pane, wikilinks for all the matches can be copied to the clipboard.
No pressure on the Template. I just wanted to voice my interest in case you were considering creating the script.
Thank you for the reply!
I’ll be trying that and I’ll make sure to have a backup as you suggested.
Generating them as links in the first place is the best way as you suggested because they are generated indeed. Thank you as it didn’t occur to me.
Very welcome. Late coming back to this; sorry about that. Fully endorse the ideas the others have suggested. Hope you find a solution that really works for your needs. A lot of links
Can also be useful to add a hotkey for the
Add internal link command.
The rude and crude version of this, with almost no error checking and thusly relying on the user knowing what he wants done, aka surround each and every line as a link I present to you this template:
<%* const sel = tp.file.selection() if ( sel == "" ) window.alert("Please select a whole lot of lines to be 'linkified'") tR += "[[" + sel.split("\n").join("]]\n[[") + "]]\n" %>
Yes, it’s always advisable to have a copy of your earlier state before making many changes, especially with regex.
But for the sake of @Jordan33J, let me chime in with this:
If you install GitHub Desktop and use Git for backup or even sync, if you make any major changes to your vault, you can use GH Desktop to go through any individual items to see the difference between Before and After.
Should your (regex) changes (in the future) not work out well, you can discard the changes in GH and thus be reverted back to a state you had before the changes.
It is how I do it (or: I can accept the changes and hit revert last commit later, which is faster). I never physically copy my myriad files (markdown and image files and all) to a different folder on my hard drive.
Thanks so much! I’m learning a little bit here and there, and definitely realizing how powerful templates can be without the code getting too complicated.
It works perfectly. Much appreciated!
Thank you for the insight.
That is an innovative way to leverage github’s functionality. The feature that you suggested is a keeper regardless of my work.
Thank you again and sorry for the late reply. Cheers to you!
The Smarter Markdown Hotkeys plugin by pseudometa allows you to set multiple cursors or select multiple list items and transforms them each into wikilinks when you use its
Smarter wikilink command.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.