# Use aliases with Templater suggester

**URL:** <https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244>\
**Category:** Help\
**Tags:** templater\
**Created:** [February 3, 2024, 3:18pm UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244 "2024-02-03T15:18:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![govsat1](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@govsat1](https://forum.obsidian.md/u/govsat1)\
**Post date:** [February 3, 2024, 3:18pm UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/1 "2024-02-03T15:18:44Z")

</div>

Using Templater, I have a standard file suggester. This works fine, but I would like to include aliases in the list in the same manner as Quick Switcher, then be able to insert the link in my created file.

Example, choose a file named Books with an alias of “Reading”… if I chose Reading from the suggester, then insert it into the file that Templater creates as `[[Books|Reading]]`. I’ve attempted to use tp.frontmatter with no success.

Current standard file picker code:

```auto
const file = (await tp.system.suggester((item) => item.basename, app.vault.getMarkdownFiles(), true, "Enter File")).basename

```

Any suggestions appreciated.

---

<div class="post-metadata">

**Author:** ![gino\_m](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/3ec8ea/32.png) [@gino\_m](https://forum.obsidian.md/u/gino_m)\
**Post date:** [February 3, 2024, 11:54pm UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/2 "2024-02-03T23:54:59Z")

</div>

See:

> [@Extract to specified heading with Note Composer](https://forum.obsidian.md/t/extract-to-specified-heading-with-note-composer/67901/4):
>
> Using @AlanG’s script, but exchanging the note suggester with a faster method (my vault is too large): \<%\* // By AlanG; Taken from: https://share.note.sx/tus76cc6 // Get the selected text let text = app.workspace.activeEditor.getSelection(); // Get the destination file const files = app.vault.getMarkdownFiles().map(file =\> { const fileCache = app.metadataCache.getFileCache(file); if (fileCache?.frontmatter?.aliases) { file.display = `${file.basename}\n${fileCache.frontmatter.aliases.j…

- Original idea by @Zachatoo. See original/more [here](https://zachyoung.dev/posts/templater-snippets).

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 4, 2024, 4:30pm UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/3 "2024-02-04T16:30:07Z")

</div>

Using parts of the code listed in previous post, I built the following template:

```js
  <%_*
// Get the destination file
const allFiles = await app.vault.getMarkdownFiles()
// .slice(0, 10) // Uncomment for test purposes of a smaller file set

const alternatives = []

function pushAlternative(path, basename, alias) {
  alternatives.push({ path: path, basename: basename, alias: alias})
}

allFiles.map(file => {
  const fileCache = app.metadataCache.getFileCache(file);
  // Push non-aliased alternative
  file.alias = file.basename
  pushAlternative(file.path, file.basename, file.alias)
  // Use the following to check for existence, and type of alias
  const aliasType = typeof(fileCache.frontmatter?.aliases)
  
  if (aliasType == "string") {
    // Single alias
    file.alias = fileCache.frontmatter.aliases
    pushAlternative(file.path, file.basename, file.alias)
    
  } else if (aliasType == "object" && file.frontmatter?.aliases.length ) {
    // Multiple aliases
    for (const alias of fileCache.frontmatter.aliases ) {
      file.alias = alias
      pushAlternative(file.path, file.basename, file.alias)
    }
  }
})

const destination = await tp.system.suggester(
  item => item.basename + 
         (item.alias != item.basename ? `\n↳ ${ item.alias }` : ""),
  alternatives,
  true,
  "Enter file",
  10);

if (destination)
  tR += `[[${ destination.path } | ${ destination.alias }]]`
_%>

```

As a sample image, I searched for “newa” in my test vault, and got this result:

 ![image](https://forum.obsidian.md/uploads/default/original/3X/b/1/b106c88b23a00899c6803bd1571081a3367ce34b.png)

As we can see in the image both the fuzzy matching, and the aliases works. (See files “221231 - New years eve” and “New file 1 → new alias”).

And choosing the latter of those it produces the link `[[tmp/new file 1.md | new alias]]`. Hope this helps!

( Now I just need to figure out how to create a new file link based upon this without having to trigger another `tp.system.prompt()` or similar, as I then can use this template to locate or create new forum response notes. 😃 )

---

<div class="post-metadata">

**Author:** ![govsat1](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@govsat1](https://forum.obsidian.md/u/govsat1)\
**Post date:** [February 4, 2024, 7:03pm UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/4 "2024-02-04T19:03:06Z")

</div>

Works great, thanks!

---

<div class="post-metadata">

**Author:** ![gino\_m](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/3ec8ea/32.png) [@gino\_m](https://forum.obsidian.md/u/gino_m)\
**Post date:** [February 5, 2024, 12:45am UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/5 "2024-02-05T00:45:49Z")

</div>

Typed in an alias (to be an exact match): was not found.

* * *

> [@holroy](#):
>
> produces the link `[[tmp/new file 1.md | new alias]]`

- What’s with the pair of whitespaces there, may I ask?

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 5, 2024, 1:10am UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/6 "2024-02-05T01:10:36Z")

</div>

I’ve not considered any issues with spaces. Yet…

But what didn’t work in your case?

---

<div class="post-metadata">

**Author:** ![gino\_m](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/3ec8ea/32.png) [@gino\_m](https://forum.obsidian.md/u/gino_m)\
**Post date:** [February 5, 2024, 1:21am UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/7 "2024-02-05T01:21:19Z")

</div>

Well, even if I allow larger than 10 results, my aliases that worked in the original script are not offered for insertion. Simple one-string aliases, btw.

> [@holroy](#):
>
> I’ve not considered any issues with spaces.

No, I meant `tR += `[[${ destination.path }|${ destination.alias }]]`.

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [February 12, 2024, 1:21am UTC](https://forum.obsidian.md/t/use-aliases-with-templater-suggester/76244/8 "2024-02-12T01:21:40Z")

</div>

Closed on February 12, 2024, 1:21am UTC

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