# Keyboard Maestro Macro: Replacing text string in all zettel bodies

edited July 2019

After a few severe initial problems I now have a simple macro for replacing text strings in the zettelkasten that works well (as always, the folder needs to be adjusted). Very handy for changing, replacing or deleting tags:

• Thanks @Vinho for this very helpful script, and for the search & replace titles as well. I'm curious if there's a way to combine the two.

For example, when I search and replace titles, I would also like for it to automatically use the same search & replace variables in the first line only of my zettel (where I keep my titles.) I haven't had any luck searching for a modifier that only allows a perl s/// script to search an isolated line of text (in my case, the first).

Otherwise, I could simply duplicate this final shell script as a final step in your title-change macro, but as it is, it would search and replace every instance, which wouldn't always be ideal.

• Hi @rydash88 and welcome to the forum!

That is a good idea and should definitely be possible. I wanted to improve the macro for replacing strings in titles anyway because it is too slow and now that I know a bit more about Keyboard Maestro actions I realised that it could be much more elegant. I'll look into it at the end of this week.

• @rydash88: What exactly does the first line of one of your zettels look like? Could you post an example (and also tell me what the file name of the associated zettel is)?

Have you read this discussion about how to design the head of zettels? That might make you reconsider what you currently do.

• Thanks, @Vinho. I'll begin with the disclaimer that I'm still fairly new to all of this, so I'm not 100% settled that I've found the best format for my zettels. I did read the discussion you mentioned, and for now I'm trying to keep things relatively simple. My first line is simply the ID + Space + Title.

I'm also defaulting to .txt files to keep things simple as I get started.

So, for example, if I might perhaps want to change the titles with "Owen" to "John Owen" if I later were to come across another Owen, and did not want to confuse the subject of the note. But I would absolutely not want to replace all instances of Owen in every zettle, since that would be redundant. There may be other changes I would want to make to only titles, but not to the body of text, but that's the easiest example I foresee.

• Hi @rydash88!

I had a go yesterday and came up with this macro:

My title is always in line 3 of the zettel, so you have to replace the "3" in

sed -i .bak "3s/$KMVAR_oldstring/$KMVAR_newstring/g;" "$KMVAR_Path" rm "$KMVAR_Path".bak


with "1".

Let me know if it works. (And as always: Back up your ZK before you try anything ).

The disabled action is a slightly simpler alternative to the one before it, but only works if you have GNU-sed installed.