URL Scheme: direct navigation to heading

Using a multi-vault (A-Z) system now on mobile and the URL scheme to jump from vault to vault and indeed the ability to jump to headings would be nice.

Not pleasant to type or look at, but these seem to work (Advanced URI is off):

### Link to heading → %23%23%23Training = ###Training
obsidian://open?vault={VAULT}&file={MY%20NOTE}%23%23%23{Training}

Link to heading → %23Training = #nTraining

obsidian://open?vault={VAULT}&file={MY%20NOTE}%23{Training}
(Thanks @berot3; see next post…)

Link to block → %23%5ECardio = #^Cardio

obsidian://open?vault={VAULT}&file={MY%20NOTE}%23%5E{Cardio}

2 Likes

that does it! Just a note, one %23 (#) is enough for headings. I guess its the same rule es for normal heading-linking (you write only [[#heading]] no matter if it is H3 etc)

2 Likes

Ah! good catch! I was just thinking # = %23, so why not three of 'em. :laughing:

I actually noticed this a bit ago when trying to link to a ### References, but there was another ## References above it in the same note that I had forgotten about. Unique headings, no matter the level, for the win!

1 Like

Yet another update. I saw this post, and hoped that using an Obsidian URI for linking to a specific sub-heading (if there were multiple of the same name) would work. Sure does!

The link:
[[Untitled 1#Biggest header 2#smol header]]

The URI:
obsidian://open?vault={VAULT}&file=Untitled%201%23Biggest%20header%202%23smol%20header%20)

The markdown:

# Biggest header

### smol header

# Biggest header 2

### smol header
This is where I want to link to....

# Biggest header 3

### smol header

Obsidian_P1JfgHStDj

4 Likes

Well, I had no idea! Thanks! It’s actually in the help too, I haven’t read it yet. Internal links - Obsidian Help

Thanks to @ariehen for showing me the way! I wrote a PowerShell function to streamline this.

It returns the URI to open Obsidian by default, but also optionally takes a vault path, note path, and heading contents, and forms the URI to that location. Further, you can hook this up to a command in Obsidian Shell Commands that you can invoke upon selecting an entire heading (excluding the leading hashes), and configure it to redirect output to the clipboard. Be sure to replace <MY_VAULT_NAME> with your vault. The kicker is that Obsidian Shell Commands will populate the command with the double-curly-braced variables, almost fully automating this link copying behavior.

You could probably refine this command to be a bit more intelligent about input, but it works well for me, as is!

C:/path/to/functions.ps1:

<#.SYNOPSIS
Custom functions.
#>

function Get-ObsidianUri {
    <#.SYNOPSIS
    Get URI to open Obsidian, optionally to a specific vault, file, and heading.
    #>
    Param([string]$Vault, [string]$File, [string]$Heading)
    function escape {Param($s)[uri]::EscapeDataString($s)}
    $v = ($Vault) ? "open?vault=$(escape($(Split-Path $Vault -Leaf)))" : ""
    $f = ($v -and $File) ? "&file=$(escape($File))" : ""
    $h = ($f -and $Heading) ? "%23$(escape($Heading))" : ""
    Write-Output "obsidian://$v$f$h"
}

Shell command:

. "C:/path/to/functions.ps1" && Get-ObsidianUri {{vault_path}} {{file_path:relative}} {{selection}}
1 Like

I guess I missed the edit window on my comment above, sorry to bump again just to make a correction, but the following instruction is no longer necessary in the above.

The resulting URI from running my script is a standard obsidian:// URI, but this shell-dependent command won’t work on mobile of course. The Advanced URI plugin gives you a custom obsidian://advanced-uri-prefixed URI and links to headers, but it’s a bit non-obvious. Your cursor needs to be inside a heading, then running the Advanced URI: copy URI for current file command will link directly to the heading instead of just the file. See the discussion about that.

1 Like