Select Many Lines to Create Separate Links

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.
    Screen Shot 2023-04-04 at 6.34.10 AM
    Screen Shot 2023-04-04 at 6.34.22 AM

You can do this by holding down the option key to create multiple cursors at the start and then end of each line:

multiple cursors

2 Likes
  • Thank you very much!
  • I’m going to be using this for thousands of lines for my work, do you know how it could be further automated perhaps?
1 Like

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:

\n(..*$)

And, in the lower field, add:

[[$1]]\n


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!

3 Likes

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.

3 Likes

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.

Thanks!

1 Like

Thank you for the reply!

I’ll be trying that and I’ll make sure to have a backup as you suggested.

1 Like

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 :exploding_head:

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"
%>

1 Like

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.

2 Likes

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.

1 Like

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