Zettelkasten Forum

[KM Macro] Rename Note and Update Wikilinks to It

This Keyboard Maestro macro will rename a specified note and update all [[wikilinks]] to it.

The macro assumes that wikilinks use the full filename of the target note, and will not be useful to anyone using explicit UIDs. A principal benefit of a UID scheme is of course that it should obviate the need for a macro like this.

Steps in the macro:

  1. The user is prompted for the note to be renamed, in an autocomplete list search.
  2. The user is prompted for the new title.
  3. The note to be renamed is backed up and renamed. Its modification time is updated.
  4. All wikilinks to [[Old Title]] are replaced with [[New Title]]. The modification time of other notes are not updated.



  • Thanks for the macro. I don't rename notes very often, and when I do, I would just do a batch rename using BBEdit. This KM macro is definitely both faster and more convenient, though, so I'm definitely going to use it from now on instead.

    I noticed one issue though:
    When I run the ("with backup" version of the) macro, neither the modification date of the renamed file changes nor that of a test file in which a wiki link is changed. This causes The Archive to only notice the name change of the first file, but not the content change of the second file. In other words, if I were to make changes to the second file before restarting The Archive, the wiki link changes in the second file would get lost.

    To address this issue, I have simply added a "Quit and Relaunch The Archive" action at the end, which works well enough.

    If you like The Archive's "PrettyFunctional (Basic)" theme, consider upgrading to the "PrettyFunctional (Regular)" theme.

  • Thank you, @Basil.

    I've revised the macro to update the modification times of all edited notes (including that of the renamed note itself).

    In retrospect, the behaviour of preserving original modification times seems like more of a bug than a feature in the context of The Archive, for the reason you point out. I've left the lines which achieve this in the shell script but commented out, so that users who'd like to can easily uncomment them.

    (Your message prompted me to look into a way of updating modification times while preserving creation times. This turns out to be more difficult than one might assume, because 'creation time' is not a standard Unix file metadata key. It's simple to do using SetFile, but this requires that the user have Command Line Tools for Xcode installed.)

  • Thank you for the updated version and the explanations. I was a bit on the fence first, but after thinking about it some more, I think changing the modification dates would be preferable to preserving them, even if The Archive did not exhibit the issue described above.

    When I ran the updated script, I was surprised to see that it changed the modification dates of almost all of my notes. I think the issue is with the grep -rl "[[$KMVAR_Instance_Old_Title]]" . | while read -r f ; do command. More specifically, it looks like the square brackets with the note title are not searched for as a single string, but instead, they seem to be interpreted as a bracket expression (i.e., the check seems to be for the individual letters inside the bracket).

    I then tried a few things like escaping the square brackets, but that didn't work, and since I don't have much experience with the grep command and didn't want to dig deeper into it right now, I put the issue aside. Hopefully someone a bit more versed in the dark arts of grepping can chime in with an easy solution.

    Fortunately, the contents of all those erroneously matched notes remain intact, and those files that do contain the respective wiki link get properly changed, so the only issue seems to be that a lot of files have their modification dates mistakenly altered.

    If you like The Archive's "PrettyFunctional (Basic)" theme, consider upgrading to the "PrettyFunctional (Regular)" theme.

  • I'm very sorry about the macro stuffing up the modification times on your notes, @Basil. I'll have to test these more rigorously before uploading.

    Escaping the square brackets like so does seem to fix this issue for me:

    grep -rl "\[\[$KMVAR_Instance_Old_Title\]\]" . | while read -r f ; do

    Another way the script will need to be improved is to account for special characters like square brackets inside of the title.

  • No worries, @seanakabry, I was able to restore the original modification dates with very little effort: I just pulled an earlier version of my notes folder from Time Machine and then ran a compare tool on both folders to selectively copy the small number of files that had actually changed since the last backup.

    I escaped the square brackets exactly like you did in your previous post, but the reason it didn't work for me was that I ran grep -rl "\[\[test title\]\]" . (and other test commands) directly in Terminal, because that was quicker than doing it via KM and because I had completely forgotten about the potentially different outcomes you can get when passing more complicated arguments (such as regex patterns) that way.

    I still don't fully understand why I couldn't get the grep command to work in Terminal after trying various combinations of escaping (some of) the square brackets, adding single quotes around the note title, etc. I occasionally use regular expressions in various programs and in Python without any issues, but grep and I don’t seem to be friends. Maybe one day, I'll find the time and energy to properly dive into grep (and/or egrep), but that day is not today. :smile:

    Anyway, thanks for clearing this up and for all your efforts in making The Archive even better with various KM macros.

    If you like The Archive's "PrettyFunctional (Basic)" theme, consider upgrading to the "PrettyFunctional (Regular)" theme.

Sign In or Register to comment.