Automating Snippets in Visual Studio Code with Python | by Lucas Soares | Towards Data Science

Every good development workflow involves some kind of management of code snippets, where you are constantly storing and retrieving snippets of code to solve a wide range of tasks in your programming routine.
In this article, I will show you how to automate the process of creating snippets for VScode using Python to save them directly from your clipboard with just one terminal command.
In VScode the format of a snippet file is as such:
{"Title": {"prefix": "the prefix of the snippet","body": ["the body of the snippet", "as a list of strings".         "where each list element corresponds to a line of code","description": "a description of the snippet"}}
This structure is supposed to be leveraged by the VScode text editor so that when you call the prefix for the desired snippet, you get a perfectly formatted piece of code that matches your specific need for the moment.
The issue here is that the process of creating such a snippet can be a bit cumbersome, so we end up googling almost everything which can be a bit annoying sometimes.
So why not automate this process with Python, so that when you write some code that you want to save it for later, all you have to do is save it to the clipboard and run a command on the terminal?
The steps to do this will be:
Specify a variable with the path for your snippets folder.Write the code to get the contents of your clipboard.Write the code to save the content in a pre-specified snippets file (to be determined when calling the script) with the right format.Save the final Python script in a folder.Write an alias to call that script from anywhere in the terminal.
Specify a variable with the path for your snippets folder.
Write the code to get the contents of your clipboard.
Write the code to save the content in a pre-specified snippets file (to be determined when calling the script) with the right format.
Save the final Python script in a folder.
Write an alias to call that script from anywhere in the terminal.
Now, let’s go through each step one by one.
Specify a variable with the path to your snippets folder
Specify a variable with the path to your snippets folder
global_snippets_folder = “/home/username/.config/Code/User/snippets/”
VScode usually has the snippets folder with a path like this, change it to your specific case.
2. Write the code to get the contents of your clipboard
import clipboardclipboard.paste()clipboard_content = clipboard.paste()
Here, we are using the clipboard package to get the contents of your clipboard.
3. Write the code to save the content in a pre-specified snippets file (to be determined when calling the script) with the right format
This is code saves the content of the clipboard to the specified folder with a name given by the user when calling the script and with the required format needed to call the snippet later inside VScode.
4. Save the final Python script in a folder
Here you can save it in wherever folder you want inside your computer.
5. Write an alias to call that script from anywhere in the terminal
To do this all you have to do (assuming you are working in a Linux machine or in Windows but with WSL: Linux), is open your .bashrc file and write:
alias save_snippet_from_clipboard="python /path/to/python_script.py"
Then, save the modified file and run source .bashrc to update your terminal.
Now you can start saving snippets directly from your clipboard!
It is important to note that, this is one approach, there are so many ways to save snippets and retrieve them, in the end, you should experiment with as many tools as you can to see what works for you.
The approach I described here has some drawbacks related to the guarantee that the format of the saved snippet will be intact due to issues with saving code from the clipboard, but in the end, I feel like the few seconds I save every time I need to save or retrieve a certain snippet make it worth it.
If you prefer you can watch my Youtube video on this topic here:
If you liked this post, join Medium, follow, subscribe to my newsletter. Also, subscribe to my youtube channel connect with me on Tiktok, Twitter, LinkedIn, Instagram! Thanks and see you next time! :)