Before we start
Please take a backup
You’re about to search-and-replace to an entire vault, so I can’t stress enough to make a backup of your folder before you start, in case something really should go wrong. I’ve tested the regex on my own vault, so I’m pretty sure there aren’t any large errors, but please do take a backup.
During the testing of these regex’s I found that VS Code hung up on me in some occasions, so I had to restart it a few times. This was when I was typing the regex one by one character, and it kept trying to execute the search whilst I was typing.
Toggle the regex option, .*
, to avoid hang ups
Therefore, I can’t stress enough that before you do a change to the search (and/or replace) strings, turn off the regex option, .*
on the far right, so that it doesn’t do the search with regex before you’ve completed entering the search. I also recommend to copy-paste the strings, instead of typing them in. These beasts, are just that, beasts!
Finally, you need to repeat some of the operations, and the easiest way I found to repeat them is to toggle the regex option, .*
, off and back on again. It’ll then repeat the search (and/or replace). Keep doing them, until you see “No results found.” before proceeding to the next step.
Open up VS Code
I’ve not mentioned it before, but open up VS Code, and open up your vault folder, and get ready to do this.
So, take the backup, and let’s continue…
Multiple frontmatter fences?
Search: (---\n(?:.*\n)*?---\n(?:.*\n)*?)---\n
Replace: $1-#-#-\n
This will check if any of your files has multiple occurences of the frontmatter fence, ---
, and if so, it’ll replace the extra ones with the -#-#-
, which I’m hoping doesn’t exist in your files. If you suspect that, come up with another string, not likely to be present.
Look through the search result, and verify that it seems correct, and either go through and replace each and every one of the matches, or hit the Replace all icon next to the replace field.
After the first run, toggle the regex-option, .*
, off and back on again, so that VS Code repeats the search. Repeat the replacement, and repeat this until it says “No results found”.
Frontmatter not a start of file?
Furthermore we need to verify that we’re actually only checking frontmatter, which actually are at the front of the file. So let’s do a search for this. Remember to turn off regex-option, .*
, if you are to type this in manually. It’ll most likely hang VS Code if you don’t…
Search: (?:.*\n)+?---\n(?:.*\n)*?---
When it’s entered, re-enable regex-option, .*
, and see if you’ve got any matches. If you do have any matches, then go in manually, and change the triplet, like we did automatically before. In other words, change ---
into -#-#-#
.
Repeat the search, by toggling the regex-option, .*
, off and back on again, and verify there are “No results found” before proceeding.
The actual replacement of the double colon
As before, turn off the regex-option, .*
, before doing changes to the search and replace strings.
Search: (---\n(?:.*\n)*?)([^:]+?)::(.*\n)((?:.*\n)*?---\n)
Replace: $1$2: $3$4
This search-and-replace needs to be repeated as many times as the longest frontmatter in your vault has fields. So get used to toggle the regex option, .*
, and hitting the Replace all icon, after a quick glance through the search results.
Repeat until you get “No results found”, yet another time. When done, you’re nearly complete with the entire operation.
Home stretch
To finalise this operation, we need to fix those sanity checks we did in the start.
Search: -#-#-
Replace: ---
And when you’ve search and replaced these, and repeated until you don’t find any more, your vault should now not have any double colons in the frontmatter anymore.