Type | while text is selected to create a link with the selected text as an alias

I’m not sure if I know good words for searching for this feature in the forum, but I tried to do a quick search.

Use case or problem

I have some text already written, and I want to select a word and then make it a link. Instead of a simple link, I want the link to have an alias, which would be the word that I have selected.

Consider the following:

  1. My text: “I have 14 cats.”
  2. I will highlight (=select) the word cats. I want to make that word to became an alias in a link.
  3. Currently, I can make the word to become a link just by typing [[. In this case, the result will be: “I have 14 [[cats]].”
    In this case, the word cats will become the name of the linked page, but instead of that, I want cats to become an alias: “I have 14 [[|cats]].” would be my preferred outcome.

Proposed solution

Typing [[ works well and I’m not suggesting any changes to that. But I have two ideas that both could be used to achieve the same goal.

A) Select some text and simply type |. It will result in [[|cats]] and it will place the cursor just before the | character so that I can then continue to write the name of the page that I want to link to. This way I could smoothly supplement the link to become [[Cat|cats]].

OR:

B) Almost the same as A, but instead of typing |, I would first type [[ to get the normal link [[cats]] (with cats still highlighted) and then I would type |, which would insert the | character, place the highlighted text at the right side of the | character, and finally place the cursor to the left side of the | character.

Current workaround

  1. Select the word cats.
  2. Type [[. Result so far: “I have 14 [[cats]].” (cats is still highlighted)
  3. Hit left key to remove the highlighting.
  4. Type |. Result so far: “I have 14 [[|cats]].” (nothing is highlighted)
  5. Hit left key again to get the cursor to the left side of the | character.
  6. Now you can finally write the page name that you want to link to, and autocompleting works too. If you would not write | before writing the page name, you could not use autocomplete, because hitting enter or tab during autocomplete would make the alias text disappear (= replaced by the autocompleted page’s name). When you have the | character in place, you won’t loose the alias text (= this works really well in Obsidian already).

I’m sorry if my post is messy. Please ask me clarification if something is unclear, I will answer gladly. Thank you for your support! :slight_smile:

10 Likes

Current workaround

Option 1: I use markdown-style links instead of wiki-links instead of [[selected alias]], by pressing “Insert Markdown link” (Ctrl+K) I get [selected alias]() with cursor between round brackets where filename belongs.
Remaining issue is that auto-complete for markdown-style links does not work yet.


Option 2: For wiki-style links, this could be solved by AutoHotkey script:

^k:: ; Ctrl+K hotkey pressed manually. 
    ; Change to your preferred hotkey. Modifiers: !^+#
  Send, {[ 2}{Left} 
    ; Enclose selected text in [[ ]] (Closing brackets autopaired by Obsidian.)
  Send, {|}{Left} 
    ; Write pipe character,  simulate left arrow to place the caret/cursor before it.
  return

The clunky way to achieve the desired result now is: select the words you want to turn into the link text, cut, type [[, enter the link title, type | and paste.

When writing in an inflectional language like Russian words and phrases keep taking different forms depending on their grammatical role in the sentence and linking can feel quite infuriating.

3 Likes

Thanks! :slightly_smiling_face:

Same with my language (Finnish), every word can have tens of different forms depending on the sentence and other words.

2 Likes

Use case or problem

Sometimes after writing, a user may want to go back and add an alias and the process can a little inefficient. They could type out the wikilnk syntax with [[ and then remove the ]] automatically generated and then add the alias.
While a solution is possible with Templater as described below, I think this is a feature that at least a good amount of users would use, and its good to reduce dependency on 3rd party plugin.

Proposed solution

Create a command called something like “Create a wikilink” that replaces text with [[|text]] and puts the cursor between [[ and | so the user can type in . The command could be assigned a hotkey.

Current workaround (optional)

You could create a Templater template and assign a hotkey to it:
[[<%tp.file.cursor() %>|<% tp.file.selection() %>]]

2 Likes

This worked/ is acceptable to me. Thanks!

I don’t know what I do wrong, but it doesn’t work.
After inserting the template, I get:
[[<% tp.file.cursor() %>|my_selection_text]]
So tp.file.cursor() is not being replaced.
Using the latest versions of Obsidian and Templater.

Well, it’s kind of odd that such a function was not implemented from the start. It is even among the base functionality of every visual editor since like the beginning of time:

Whenever you need to insert a link, you first
1) select some text and then
2) paste the link.

That’s it!

In Obsidian we just select our own text to create links to our own notes. So we don’t need to paste links probably, rather - we’d want to have notes looked up in our vault. And Obsidian has a tool for that already.

Imagine I have a dictionary of terms in my vault, and I mark incoming texts with the dictionary words: for each entry of a dictionary word I make a link to the corresponding note. And, of course, one doesn’t simply edit the original text apart from inserting links.

This needs to be enabled in Templater’s settings. That might be it.

1 Like

Thanks, @ariehen !

It is now being replaced, however I get my cursor set right before the opening double square brackets, not after them :-/
I’m unlucky :slight_smile:

It works!

@ariehen Actually, a while after creating that template, I noticed that it doesn’t work when the word its at the start or end of the line. Apparently someone else noticed it as well and submitted a bug issue in the Templater repo: tp.file.cursor() doesn't work for my template when I use in on text selected in the middle of the line. It only works on text select at the start or end of it. · Issue #1089 · SilentVoid13/Templater · GitHub.

You guys should look at it and see if you have the issue too. If you do, I’d recommend replying to the thread saying you notice the same thing, and or at least give that post a thumbs up.

1 Like

I have some text already written, and I want to select a word and then make it a link. Instead of a simple link, I want the link to have an alias, which would be the word that I have selected.

Have a look at Link with alias plugin, which is available since today. I made it exactly for the purpose and it helped me to create about 1700 links during 3 days of conversion of plain text to kind of personal wikipedia. I hope it helps you too :grinning:

3 Likes

This is brilliant! Thank you for solving this problem so elegantly!

2 Likes

For example if I have some block of text that I’m reading and I realize that I have a brown fox note

The quick brown fox jumps over the lazy dog

I’d like to be able to highlight the text somehow and then select a note / search from a drop down menu

The quick [[Brown foxes|brown fox]] jumps over the lazy [[Canis lupus|dog]]

Currently I have to create the link separately then copy and paste or re-type the text.

7 Likes

请参考以下页面上的视频演示:
https://getquicker.net/Sharedaction?code=51add9e1-60ad-4037-1961-08d7fc020536

1 Like

I would also really like to see this feature added. Useful for when reviewing over old notes and realizing there are some good link opportunities that exist now which didn’t back then.

+1 as well. I have the issue that I paste in quotes and within the quote I want to link to existing notes. Right now I have to manually enter square braces around the word / phrase and it’s really tedious.

I think I’ll put together a KM shortcut for this in the meantime

Continuing the discussion from Link workflow:

+1 to this. I was just complaining about the lack of this feature. The behavior when you choose the “internal link” command when text is selected is really unintuitive and unhelpful.

And here’s another feature request for the same thing: Auto-complete piped links

1 Like

I think the default assumption of a pipeless link is reasonable, but piped links should be easier. Maybe always pop up the autocomplete form (if you want the link to just be the text it’s easy to select) but add a switch to select which kind of link?