Delphi Productivity Tips: Live Templates
A long time ago when Delphi 5 was king and conferences were in person, I was able to attend BorCon and saw a session presented by Mark Miller. I don't remember the topic but I do remember noticing that he was typing code really fast. He wasn't just typing fast, though, he was coding fast! As he was typing, characters were being turned into words and sometimes even full expressions. At some point, he explained how he was doing it: he was using a Delphi IDE plugin called CodeRush. I don't remember anything else from that presentation but I soon had my own copy and started learning to use keyboard expansion macros.
CodeRush had a lot of other features as well but I had to turn most of them off because the software had a lot of bugs. However, I had experienced the productivity boost and was hooked! When Mark Miller and his product went to DevExpress and focused on .NET tools, I was disappointed as Delphi moved forward without my favorite plugin but I found an acceptable tool, albeit less fancy, with GExperts' Code Proofreader. I used that for a few years until I discovered Live Templates had been added to Delphi 2006. I was elated as these brought back all the functionality I had missed!
Live Templates, combined with Code Completion, is one of my favorite productivity-boosting features of the code editor in Delphi. There are many templates that come with Delphi but many people don't know about them because, by default, they have to be manually activated with Ctrl+J--unless you edit them to be automatically activated.
Let's check some out.
Using a Live Template
Edit a Delphi source file and on a new line in the public declaration for a class, hit Ctrl+J and type "propf". You'll see a window of templates pop up under the cursor and as you type, it selects the template by that name. When you hit enter, the template activates and writes code for you. But it does more than that. Notice the cursor is right after the word
property with the word "name" highlighted? As soon as you enter a name for the new property, hit Tab, enter a field type, then hit Tab once more , the rest of the line is finished for you; according to standard Delphi naming schemes, the field name for a property is the name of the property starting with "F" so it finishes it for you and even creates the private variable in the class.
Try this with the "prop" template and watch it create the getter and setter methods for you, placing the cursor in the body of the automatically class-completed method body.
Another way to manually activate a template is type the template name first (like "prop" or "propf" that we just tried) and then hit Ctrl+J. Delphi figures out the template you've already selected by name and immediately activates it.
Try one more: type "begin" and hit Ctrl+J and watch a
begin-end block be created for you with the cursor indented inside, ready to add code.
The real boost to productivity, in my opinion, is to have these templates automatically activate. To do that, we need to edit one.
Editing a Live Template
If the Templates pane in Delphi is not showing, you can view it by selecting View > Tool Windows > Templates (in Delphi 10.4). Expand open the "Delphi" section to see a list with two columns, the first being the template name, the second being a description of what it does. The templates that come with Delphi are stored in the
ObjRepos\en\Code_Templates folder under Delphi's main folder. That means if you installed Delphi under the default of
C:\Program Files (x86)\Embarcadero\... then you can't edit them directly. Templates that you create or that are installed by third parties are put in your user "Documents" folder (
C:\Users\<user>\Documents\Embarcadero\Studio\code_templates). So you'll either need to move the default ones or create different ones in your Documents folder. The easiest way to create a new template based off an existing one is to simply select the one you like, edit it, then save the XML file to the
code_templates folder mentioned here.
Let's look at one. Click on the image below to see the default
This is a simple template and several parts should be obvious by looking at the XML tags. You can easily change the author and description but notice the
invoke parameter in the template tag? It is set to "manual". If you change it to "auto" then you don't have to hit Ctrl+J to activate it, simply a Tab or Space will do it. This natural typing keystroke (a space), combined with "auto" invoked templates, is where the big productivity boost can be found!
The complicated part of the template is in the
code section. You will need to read the documentation and look at several templates to understand how the
CDATA block works. The cool part is the asterisk (*) indicates where the cursor will end up when the template is finished. Look at a more complicated template like
propf we used and you'll see there are "jump points" defined with
hint tags in the XML. These define places in the template that pause and wait for entry from you, such as typing the name or type of the property, before it finishes.
I have set several of my favorite ones to "auto" and also created several new ones to match ones I learned while using CodeRush. For example, instead of typing out the word "begin", I just type "b" and a space. Here are a few others:
Result := False;
Result := True;
There are many, many more--and every time I look through my list, I am reminded of ones I had forgotten!