Increase Your Productivity with VSCode Snippets | by Nik Piepenbreier | Oct, 2022
And three useful snippets for data science in Python
Visual Studio Code (VSCode) is one of the most customizable and productive code editors available today. Using snippets is a great way to increase your productivity — but did you know you can create your own snippets? This tutorial will explore how you can create your own snippets to boost your data science productivity!
Code snippets are templates for reusable coding patterns. They let you automate keyboard commands and even let you cycle through different parameters.
This is better explained visually, so let’s take a look:
Snippets can be simple text expansions. But as the example above shows, you can even cycle through different parameters of the expansion!
Convinced? Let’s see how we can create a snippet of our own!
Not only does VS Code let you create your own snippets, it even lets you define what language they’re applicable to. To get started with making your own snippets, you can follow the steps below:
- Open the command palette and search for
Snippets: Configure User Snippets
. - Select
python.json
to create snippets for Python.
This opens up the following editor, which gives you a template for creating snippets:
We can see that each snippet is given a name and then inside of a dictionary, with the following kets:
prefix
: the keyboard command to usebody
: the content of the snippetdescription
: a string to describe the snippet itself
You can insert placeholders for where to include cursor points to tab through by using the $1
values. These placeholders can be repeated and start at 1. $0
refers to the end position of the placeholders.
Let’s start with creating a few more snippets!
Below are three snippets to get your creative juices flowing and create some of your own:
Modify a Pandas Column
We can modify a Pandas column by re-assigning it to itself. Personally, I find this a bit tedious to write out, especially with longer column names. The snippet below makes the process much easier:
The snippet allows you to write the DataFrame name and column name only a single time, then hops to the end of the line.
Filter a Pandas DataFrame
We can filter a Pandas DataFrame by using different modifiers, such as ==
or >=
. VSCode lets you define these as a drop-down by wrapping them in ||
characters.
Let’s see what this looks like:
if __name__ == “__main__”
Given the prevalence of this, writing this safeguarding command will make you wonder why you’ve never used snippets before!
This looks like the following:
VSCode snippets allow you to simplify repetitive code. However, they also go much beyond text expanders, giving you the option to use placeholders you can easily tab through. I encourage you to make your own if you find yourself writing the same code over and over again!
And three useful snippets for data science in Python
Visual Studio Code (VSCode) is one of the most customizable and productive code editors available today. Using snippets is a great way to increase your productivity — but did you know you can create your own snippets? This tutorial will explore how you can create your own snippets to boost your data science productivity!
Code snippets are templates for reusable coding patterns. They let you automate keyboard commands and even let you cycle through different parameters.
This is better explained visually, so let’s take a look:
Snippets can be simple text expansions. But as the example above shows, you can even cycle through different parameters of the expansion!
Convinced? Let’s see how we can create a snippet of our own!
Not only does VS Code let you create your own snippets, it even lets you define what language they’re applicable to. To get started with making your own snippets, you can follow the steps below:
- Open the command palette and search for
Snippets: Configure User Snippets
. - Select
python.json
to create snippets for Python.
This opens up the following editor, which gives you a template for creating snippets:
We can see that each snippet is given a name and then inside of a dictionary, with the following kets:
prefix
: the keyboard command to usebody
: the content of the snippetdescription
: a string to describe the snippet itself
You can insert placeholders for where to include cursor points to tab through by using the $1
values. These placeholders can be repeated and start at 1. $0
refers to the end position of the placeholders.
Let’s start with creating a few more snippets!
Below are three snippets to get your creative juices flowing and create some of your own:
Modify a Pandas Column
We can modify a Pandas column by re-assigning it to itself. Personally, I find this a bit tedious to write out, especially with longer column names. The snippet below makes the process much easier:
The snippet allows you to write the DataFrame name and column name only a single time, then hops to the end of the line.
Filter a Pandas DataFrame
We can filter a Pandas DataFrame by using different modifiers, such as ==
or >=
. VSCode lets you define these as a drop-down by wrapping them in ||
characters.
Let’s see what this looks like:
if __name__ == “__main__”
Given the prevalence of this, writing this safeguarding command will make you wonder why you’ve never used snippets before!
This looks like the following:
VSCode snippets allow you to simplify repetitive code. However, they also go much beyond text expanders, giving you the option to use placeholders you can easily tab through. I encourage you to make your own if you find yourself writing the same code over and over again!