A minimal python script you can use anywhere to perform substitutions of your specifications on save.
git clone
/git submodule
this repository into the project folder- Ensure you have python3 installed
- While at the root of your project folder, run the following command.
python macroOnSave/main.py
- If this is your first time, a
macro_config.json
file would be created. - Edit the
macro_config.json
file to your requirement.watch_dir
is the directory which macros will be enabled.macro_dir
is the directory where you define your macros.marker
is the tag before the file name to be substituted.replace_start
is the tag to mark the start of a macro.replace_end
is the tag to mark the end of a macro.refresh_time
is the period which the program will check file saves
- Re-run
python macrosOnSave/main.py
to start the program.
Suppose you set your watch_dir
as src/
and macro_dir
as macro/
.
And you are working on a file src/A/main.cpp
which has the following content.
// src/A/main.cpp
#include <iostream>
int main() {
std::cout << "Hello World\n";
}
And you have defined a macro in macro/B/bye.cpp
. Note that only the lines between ...MACRO-START...
and ...MACRO-END...
will be used.
// macro/B/bye.cpp - Start
#include <iostream>
// MACRO-START
void bye() {
std::cout << "Goodbye\n";
}
// MACRO-END
Then simply by typing >>> <substring of macro file name>
like >>> by
or >>> bye.c
,
// src/A/main.cpp
#include <iostream>
>>> by
int main() {
std::cout << "Hello World\n";
}
Turns into the following on save
// src/A/main.cpp
#include <iostream>
void bye() {
std::cout << "Goodbye\n";
}
int main() {
std::cout << "Hello World\n";
}
Its just that simple.
- Macros are line level replacements. So expect the line containing
>>> <filename>
to be replaced with the file contents of the macro. - You can define macros in subdirectories, of
macro_dir
as the program will scour the directory for a suitable file.