Modern Python Projects Transcripts
Chapter: Your code editor
Lecture: Snippets

Login or purchase this course to watch this video and the rest of the course contents.
0:00 As you start typing in VSCode,
0:01 you will sometimes notice that there is a pop up suggesting you some different auto completion
0:05 So, if we try to create a new function,
0:08 you can see that there are some strange long auto completion options.
0:12 Those are snippets, and they come usually from different plug ins.
0:16 For example, python snippets come from the python pluggin.
0:19 They can save you some typing.
0:20 For example, this one for the function will pre generate the scaffolding of your function
0:25 So let's create a name of a function, you can hit TAB to go to
0:30 the next location in the snippet.
0:32 So now we need to provide some parameters.
0:34 Let's put the name the Docstrings and then finally,
0:39 the code. Let's print something and let's run this one.
0:50 The nice thing about snippets is that you can easily create your own,
0:54 so let's try to make a new snippet for python.
0:56 First, using the command palette,
0:58 select configure user snippets. That's such if we have Python snippets,
1:04 yes, we do. So, let's select this one.
1:06 And now we have a file where we can write our snippet inside this file.
1:10 We already have some example Snippet.
1:12 Unfortunately, it's not a Python snippet.
1:15 It's a JavaScript one, but it explains you.
1:18 What are the mandatory parts of each snippet
1:23 So based on that, let's try to create our own.
1:26 Let's say I want to create a snippet that will measure the execution time of a
1:30 given piece of code. So I want to start the timer on the beginning,
1:34 execute some code and then display how long it took.
1:37 So let's call it time measurement.
1:43 So first parameter is prefix. This is the text that will trigger the suggestion for
1:47 the snippet. Let's say we want to trigger the snippet auto completion when we
1:51 type time. Next, we have a body. Body is a list of lines that will
1:58 be inserted as our snippet. So, don't worry about the indentation.
2:02 VSCode will figure it out.
2:04 So here is the code of our simple snippet.
2:06 First we import time module, then we save What time is it,
2:10 then? We have a placeholder called tab stop, when you insert a snippet and you
2:15 add this $1, $2 and so on and so on.
2:19 This is the location where the cursor will move when you press stop,
2:23 as you saw when we used the def snippet each time I pressed stop,
2:28 I was moving first from the function name,
2:30 then to the parameters list, then to the Docstring and finally to this pass
2:34 statement. So those are four different tab stops. Here
2:38 We only have one, because this is where we want to type some code.
2:42 And finally, we have a line that will subtract the initial time from the current
2:46 time and print out the results.
2:53 Final parameter that we can specify is the description.
2:57 This is the description that will be displayed in this auto completion.
3:00 Pop up, for your snippet. So, once we have the snippet,
3:03 let's give it a try. Let's remove this and let's type time and you can
3:09 see this is our snippet. Let's add some numbers so it will take some time
3:13 Let's let's actually execute only this part in the terminal.
3:24 So I select the code and pressed shift enter and,
3:29 as you can see, first I have the sum of the first one million number
3:32 and then I have a statement saying that it took 0.4 seconds,
3:39 so that's how we can create a simple snippet.
3:41 If you want to learn more,
3:42 go to the VSCode documentation.
3:44 There's a whole section on snippets here,
3:46 but I suggest you take a look at create your own snippets, after explaining the basics
3:51 The important thing that you should take a look are variables,
3:55 so you can use special variables in your snippets and that will,
3:58 for example, insert the currently selected text,
4:02 the content of the current line or,
4:04 for example, the content of your clipboard.
4:06 And so on, dates and stuff like that,
4:11 you can even perform some regular expression transformations,
4:18 and you can even assign key bindings to snippets.
4:21 So to create more advanced snippets,
4:23 I suggest you take a look at this documentation.