View list of tags contained within current file

What I’m trying to do

We have the feature where we can see a list of ALL the tags in the vault apparently in the side pane. I just want a list of the tags that are in the current note only (and ideally/practically the number of times they’re mentioned within the note. If on the side it’s additionally mentioned how many times the tag is mentioned in the whole vault, that seems very practical to me, though not essential).

Things I have tried

Surely this must be an obviously available thing and I’ve just been missing it? I searched the Help docs, searched settings, searched community plugins, so far I have somehow not been able to find out how to get a simple list of tags that are only in the current note/file I’m in. Instead I have the list of ALL tags across all the files, and I just want a list of tags that are mentioned within the body of a document/file/note, just like how outline view and similar features work.

Help is appreciated :pray:

5 Likes

You can for exemple insert inside the note this dataview query:

```dataview
LIST WITHOUT ID t
FROM ""
WHERE file.name = this.file.name
FLATTEN tags as t
```

Thank you, however this dataview approach seems like a solution designed for rare/occasional use, or for a specific note/template. For me to make use of this would require that I add content to All of my notes when I simply want to be able to glance at my sidepane where I view all my other data points (table of contents, inbound and outbound links), whichever file I’m in – the ones I use frequently daily as well as the ones I hop into only briefly to find things.

It seems very strange to not have this feature built in? If tags are a thing, I’m surprised that tags within a note is not a thing.

2 Likes

As far as I know there isn’t a way. You could do it with search but you’d have to keep manually changing the filename in the search.

I recommend posting a feature request. I believe it would be an easy plugin to make, so if you’re lucky maybe a plugin developer will be inspired to do it.

(It doesn’t totally surprise me that there isn’t a feature or plugin for this yet, because many people put all of a note’s tags in a single place in the note, and because tags officially apply to the whole note.)

A convoluted but working solution:


Create a note with the following DataviewJS codeblock:

~~~dataviewjs
const file = app.workspace.getActiveFile();
const tags = app.metadataCache.getFileCache(file)?.tags?.map(a => a.tag);
if (tags) {
	const tagSet = new Set(tags);
	const tagArray = [];
	for (let tag of tagSet) {
		tagArray.push([tag, tags?.filter((b) => (b === tag))?.length]);
	}
	dv.table(["Tag", "\\# of occurences"], tagArray);
} else {
	dv.span("No tags");
}
~~~

Drag that note to the sidebar you want it to be in.

Then, go to Settings > Hotkeys and bind a hotkey to the command Dataview: Force Refresh all Views and Blocks.

That’s it! Note that in order for the codeblock to update (e.g. when adding/removing tags or switching notes) you need to press the hotkey.

3 Likes

I am very much missing this feature as well, and I’m surprised it’s not available by default.

Some of my notes are very long, and tags are very much interspersed within them, so it’s impossible, at a glance, to see how many tags there are in a note, which of them might be the most frequent, etc.

I’ll give the solution provided by @pellucid a try, although it does look intimidating, and as an Obsidian newbie, I have yet to find out what “Dataview” and “codeblocks” are (I’m no programmer…), I guess they will be some sort of community plugins… Well, I’ll certainly give this a try.

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