How to keep/always have # in front of Tags in properties

What I’m trying to do

I like to have tags preceded by the #, to keep them consistent with inline tags, so my typical tags in Properties look like this:

---
tags:
  - "#books"
  - "#reference"
---

1: adding tags during note editing

Obsidian adds tags in the format I want if I type # in the tags properties field

Is there a way to force Obsidian to always behave as when I type the #?
This would be much faster and less error prone (less chance to have some tags without the #)

2: adding tags to multiple files

Sometimes I need to apply tags to multiple files at the same time and I use the Multi Properties plugin but it adds tags like this:

---
tags:
  - books
  - reference
---

Things I have tried

I tried the Linter plugin but it does the opposite of what I need: it removes the # from tags in properties. I would need an AntiLinter.

I did a multi file search and replace with a text editor but I’m hoping there is a better way.

There is no setting in core Obsidian to achieve what you need.

But you can easily hack the main.js file of the said third-party plugin:
Disable the plugin, navigate to the plugins folder of your config files and to the ‘multi-properties’ folder, and open the main.js file in an external editor.
Find

function cleanTags(str) {
  let cleanStr = str;
  for (let index in KNOWN_BAD_CHARACTERS) {
    cleanStr = cleanStr.replaceAll(KNOWN_BAD_CHARACTERS[index], "");
  }
  return cleanStr;
}

Replace this function with:

function cleanTags(str) {
  let cleanStr = str;
  // Removing known bad characters
  for (let index in KNOWN_BAD_CHARACTERS) {
    cleanStr = cleanStr.replaceAll(KNOWN_BAD_CHARACTERS[index], "");
  }
  // Adding '#' before tags value
  cleanStr = "#" + cleanStr;
  return cleanStr;
}
  • The double quotes will be added by the plugin, so no need to add them here.

Save the main.js file. Re-enable the plugin and now it will add "#tagvalue" in your files.

These added lines will be overwritten again when the plugin gets updated. So quit Obsidian, make a copy of the plugin, rename the plugin id and the name of the plugin (to any descriptive name) in the manifest.json file and add that new id in the community-plugins.json file. Make sure you follow the syntax: comma after each line except the last one.
This way one can make sure the modified plugin’s main.js will not be overwritten.

2 Likes

Thanks a lot, It works!

Adding individual tags in an open file is still slow because I have manually type the # before selecting the tag, but now I can do bulk tag edits with a lot more ease.

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