Using Github Pilot for Writing a Python Script for Extracting Markdown Links
The aim of this page📝 is to illustrate yet another use case for Github Pilot — recently praised by Guido van Rossum — and show how you can quickly turn your idea for scripted automation into reality in much faster time. This one took me 13 minutes.
To be clear, I’m including the time it took to solve parts that cannot be done by the copilot: how to make this a written Python script execute quickly via VSCode Tasks without going to the terminal and calling the script manually. Okay, I knew about the tasks, but did not know how to pass the name of the currently opened markdown file to the python script doing the magic.
My goal is:
- I have a doc (like this one, all’s in markdown)
- I have a template for every doc
- In that template, at the top, there is a section
LINKS
— something I learned in academia and found extremely useful and simply fair. - When writing, however, I am just using links “as-I-go” in markdown/vscode and pasting them to the bottom
- I usually do this as part of my editing (using regex and CTRL-F in vscode and then pasting, but it’s still semi-automation)
- Since I keep my
## LINKS
at the top of the doc in my markdown and at the bottom for published posts (I’m moving this on Medium), I am happy for them to just be automatically copied to the clipboard - NOTE: I value slow deep work, follow Cal Newport and consider the following one to be an integral part of my values
Nowadays it is not only my habit, it is also to my taste — a malicious taste, perhaps? — no longer to write anything which does not reduce to despair every sort of man who is “in a hurry.” … in the midst of an age of “work,” of indecent and perspiring haste, which wants to “get everything done” at once: this art does not so easily get anything done, it teaches to read well, that is to say, to read slowly, deeply, looking cautiously before and aft, with reservations, with doors left open, with delicate eyes and fingers
1. These are the comments/prompts for the AI and perfectly suitable code it generated
# extract markdown links from a file
# copy them to clipboard using pyperclip
- What follows is what Github Copilot’s just written for me — none of this code is mine and I am more than happy with it
2. I’ve linked the code to VS Code tasks to call the script with a key shortcut
- I’ve been using tasks in VS Code to run python scripts. FYI check How to execute Python code from within Visual Studio Code — Stack Overflow for basic intro* I was not sure how to pass a current filename to the script name ⟹ got a suggestion from Github Copilot about appending a build-in
${file}
after the script name in theargs
3. Sure, after initial use, I’ve added a couple of final touches
- Okay, after 2 minutes I decided I want to to have my links bullet-pointed and, also, to print them in the terminal
- At this point, I am not bothering myself with further prompts as “the final touches” can be done by me in no-time