Elegant Way to Prepend Copied Text (to the Top) of a Markdown File?

I'm still fairly new.

I see how to append text to the bottom (and even then I'm not sure what option I should choose for encoding when it comes to a markdown document), but I can't think of an elegant way of prepending to the top of the document file.

Thanks for looking into this with me!

1 Like

Are you talking about a document that's open -- and, if so, in what application? Or do you mean a file on disk?

file on disk - sorry, I should have specified.

Hey @NoorTheGrey,

Well, no matter how you look at it you have to read the file – concatenate it with the new data – and write the file.

But... There's a way to make most of that pretty transparent to the user.


Prepend Text to a File v1.00 copy.kmmacros (6.0 KB)

1 Like

This is amazing!! It's working exactly as intended - thanks for taking the time.

Can you help me understand what is happening in the "Search and Replace File" step? It looks really clever, but my intuition says what I should get is replace all with the local_TextData contents - it's clearly not that since it works as intended.

Also, is there a chance this will mess the document (and thus should I include a back-up in the macro)?

Finally, is there any size limitation when it comes to variable/ this workflow? e.g. only go up to a specific number of characters/ MB

I’m nowhere near the RegEx guru @ccstone is, but here’s what the metacharacters that you see in tha action mean:

\A matches the very beginning of all text when searching across multiple lines.
\n (which in this action is repeated twice) is for a new line.

So essentially he’s looking for just the very beginning of the entire text, and replacing just that “beginning” (which doesn’t contain data since it’s an anchor only) with the contents of the variable, and two new lines; in effect prepending the variable’s contents to whatever is already in the file. That action then of course writes all that info back to the source file.

Highly unlikely... but I believe in “better safe than sorry” practices, so while I don’t always backup specific files, I have multiple local and cloud-based backups that run periodically to ensure I don’t lose too much data in the event of a power failure, or “user-error”. :sweat_smile:

Not that I am aware of... I regularly modify different text-based documents that are quite large and have never had an issue. No doubt the more experienced users here can confirm/deny that.



Many thanks for this @cdthomer.

I actually couldn't find the regex after a DuckDuckGo search, and that helped a lot!

Also, thanks for the reassurance regarding the reliability of the operation!

I'm grateful to you and @ccstone for helping me close this (after I was wrestling with it for quite a bit!)


Hi @NoorTheGrey,

No need to apologize for being a newbie. I've been using KBM for two years and it was less than two weeks ago that I found out about the Search and Replace action using Regular Expressions -- and I've been using REs for over 40 years. My toolbox just got a whole new drawer!