Reputation: 429
I have around 50 markdown files with frontmatter. Something like this:
---
title: Funny Title
date: 2020-01-09
---
I'd like to edit all of the files such that I have the following:
---
title: Conference Volunteering
dateCreated: 2020-01-09
dateUpdated: 2020-01-09
That's it. I was able to right a macro in Vim to do this for me, but did not find a way to run this on multiple files. Now I'm thinking there must be a way to do this in VS Code.
For example, I can search and replace date
with dateCreated
in all *.md
files. That would get me halfway there, I just can't figure out how to also duplicate this line and replace dateCreated
with dateUpdated
Any help appreciated. If you need more details/explanations, please let me know.
Upvotes: 2
Views: 2707
Reputation: 11820
In vim you could try this way:
Set the hidden
option to allow vim jump to the next file with no saving: :set hidden
Open all your markdown with:
vim *.md
then run a
:silent argdo 3s/^\v(date)(:.*)/\1Created\2\r\1Updated\2/ge
silent ........... does not show messages on each substitution
argdo ............ acts over all files opened
3s ............... substitute on the third line
^ ................ start of the line
\v ............... very magic (allow more sane regex)
(date) ........... first regex group --> \1
(:.*) ............ second regex group --> \2
\r ............... adds a new line
e (flag) ......... ignore files where the pattern does not appear
Before saving all files (the arglist) take a look if the result is ok, then:
:argdo update!
In oposite case, there is a mistake you can revert all you done with:
:silent argdo edit!
Upvotes: 1
Reputation: 126
If you have a macro for this you can do it in Vim, no need for VS Code. Look at the second answer here (by Rich): https://vi.stackexchange.com/questions/119/how-can-i-run-a-function-or-macro-across-a-folder-of-files
You can populate your args list by starting vim like this:
vim my/directory/*md
After that, as shown in the answer I linked to, you can run (inside vim):
:argdo normal @<macro label>
Make sure that the macro includes navigation to the correct line in the file: I'm not sure if the initial position of the cursor is the beginning of the file or where ever it was when you last closed the file, but anyway I'd start the macro with going to the top of the page and then navigating to the lines you want to change.
PS: Note that in the answer I linked to there's a discussion on how to save the changes, I'll leave it for you to look into that.
Upvotes: 1
Reputation: 28783
do a regex search with
Search: date: (.*)
Replace: dateCreated: $1\ndateUpdated: $1
Upvotes: 3