IDE Window: Code Templates

From Lazarus wiki
Revision as of 15:26, 10 January 2012 by Mattias2 (talk | contribs) (Define your own macros)

Deutsch (de) English (en) español (es) suomi (fi) français (fr)

What are Code Templates?

Code Templates are texts with an abbreviation. For example: You can type 'cl' in the source editor and then type Ctrl+J to invoke the code templates starting with 'cl'. This feature is a great time saver.

This dialog allows you to edit the existing or add your own templates.

The texts can contain macros. And you can add your own macros by creating a design time package and using the macrointf.pas of the IDEIntf package.

Example 1 - ifb



The pipe should represent the cursor. Press Ctrl+j. It will be expanded to

  if | then begin


Again the pipe represents the cursor. You can see the cursor has moved as well.

This template is called 'ifb' and is defined as follows:

if | then begin


This time the pipe '|' is a real character. It defines the cursor position after inserting the template.

Example 2 - Using macros

This example shows how to create a code template 'w' which inserts a line with the current procedure name.

  • Click on the 'Add' button. A dialog pops up. Set Token to 'w' and Comment to 'writeln(ProcName)', then click Ok.
  • Your new item has been added to the list box.
  • In the source below fill in
 writeln(' ',|);
  • The pipe is really a pipe and defines the new cursor position. Now place the cursor after the first ' and click on the 'Insert macro' button. A dialog pops up showing all available code macros. Choose 'ProcedureName' and click 'Insert Macro'. You should now have:
 writeln('$ProcedureName() ',|);
  • Click 'Ok' to save and close the code template dialog.
  • Move the cursor into a method body, type 'w':

For instance:

 procedure TForm1.FormDestroy(Sender: TObject);

The pipe should represent here the blinking cursor, not a character.

  • Press Ctrl+j. You will get:
 procedure TForm1.FormDestroy(Sender: TObject);
   writeln('TForm1.FormDestroy ',|);

Auto complete on

Code templates can be invoked by the shortcut Ctrl-j or by an event. You can set for each template on what event it is executed. You can enable none, some or all.

  • line break - whenever the user presses the 'Return' or 'Enter' key
  • space - whenever the user presses the Space bar.
  • word end - whenever the user presses a key that ends a word. For example when typing 'b;' the code template 'b' can be executed.
  • do not add character - this option can be combined with any of the above. When enabled the triggering key will not be inserted. For example when the option 'space' is enabled and the user triggers a template by pressing the space bar, the space character will not be inserted in the source editor.


Macros have the form $(MacroName) or $MacroName(Param1,...). Macros can be nested.

Define your own macros

You can register your own macros by writing a design time package and installing it. See ide/codetemplatesdlg.pas procedure CreateStandardCodeMacros for examples.

DCI file format

The default dci file of the IDE is pcp/lazarus.dci, where pcp is your primary config path (e.g. under Linux $HOME/.lazarus).

A dci file is a text file with a list of templates.

Each template starts with line [name | description]. The template's value are the following lines.