Live templates are a wonderful part of PyCharm. They are very helpful when there is a repetitive pattern that you need to type over and over again. PyCharm 3.4 introduced a couple of new snippets into the mix, so let's see how we can make our own by inspecting the snippets that are already available. Let's take a look at the main
snippet that comes bundled with PyCharm 3.4:
We can see that [1] is the snippet in question and it has its own abbreviation main
, and this is what we invoke with [2]. The description for snippet [3] is what will be shown when you try to invoke Insert Live Template (Command/Ctrl + J). You can change the abbreviation to suit your fancy so that you can invoke it using whatever you like. The most important parts of the screenshot to discuss are [4] and [5].
$END$ [4]
is a special template variable, and it is where the cursor will be after the snippet has been inserted (after you've put in any variables that the template requires). $END$
is a reserved template variable, and there are a couple of others that we will get into. This particular snippet is under a particular template group, Python [5]. This means that this template can only be invoked inside a .py
file.
Variables in Live Templates are very similar to that of file templates, but with Live Templates, you have a lot more control. So, let's take a look at a List comprehension
snippet in PyCharm, compl
:
So, the variables are there and you can use them as they are, but we need to take a look at the customizations made to the variables to appreciate the power of this snippet:
The order in which the variables are arranged in this list is the order in which your cursor will be placed, so in this case, your cursor will initially be placed in the position where the ITERABLE
variable sits.
So, ITERABLE
is pyIterableVariable()
[1], and this means that the only type that is allowed in the code completion dialog box is an iterable, meaning a generator or a list, set, and so on:
Note the completion that you get. The variables are only iterable. This is actually a very powerful way in which you can minimize any mistakes that you make when writing Python.
But you can do more than just saying something will be an iterable. In the Edit Template Variables screenshot, you will see that there is another expression, collectionElementName(ITERABLE)
[2], and this takes the first variable as an argument to the expression, essentially making a function call. This function turns a plural noun into a singular noun, so take a look at the following screenshot:
So, we have a list called names, and that gets turned into a singular version, name, by the logic in the snippet. There are many more in-built functions, which can be seen in the drop-down box in the expression column of each variable.
You can have default values and they can be variables that you have defined before, or you can have hard coded variables using strings, so we can modify the main
snippet that we discussed earlier:
ifname
so that we invoke it using ifname
instead of main
. You can do this by changing the abbreviation.__main__
.__main__
:__main__
as the default argument, but you can change it if you like.Although this is a simple example, it does demonstrate a lot of the things that we talked about.