File Templates

Anders Lund

The File Templates plugin allows you to create files based on other files.

When you first install Kate, you need to enable the plugin:

  1. Click SettingsConfigure Kate

  2. In the tree on the left of the screen, click Plugins.

  3. In the Plugin Manager panel on the right, select File Templates, and then click OK.

You can now use any file as a template (this creates a copy of the file with an empty URL.) Special template files can contain macros to fill in information such as your name, email address, the current date, and position the cursor at a convenient position in the new file.

You can easily create a new template from an open document.

Templates that you save to the template folder are listed under the menu item FileNew from Template.

The template folder is part of the KDE file system, and consists of at least

If your KDEDIRS environment variable contains additional directories, those are searched for a similar subdirectory as well. If equally named templates are found, the one in the local (KDEHOME) folder is chosen.

Menu Structure

FileNew From TemplateAny File...

Presents you with Open File dialog that allows you to use any file as a template. If the chosen file has the extension katetemplate it will be parsed for template information and macros.

FileNew From Template

The remainder of submenus contains links to templates. Click a menuitem to create a file as described by the menu item text.

SettingsManage Templates...

This will launch a dialog with a list of all templates found within the template directories, along with options to add, edit or remove templates.

Using a Kate Template

When creating a file from a template that contain template macros, some macros appears as editable variables in the text. Such variables appears as underlined words in the text.

The first variable will be selected, so you just have to type to edit it. If the document text contains more instances of the same variable, they are changed as you edit. To move to the next editable variable, press the Tab key. When the last variable is edited, the list is dropped, and your Tab key works as normal.

Creating your own templates

To create a new template, use the SettingsManage Templates Item to launch the template management dialog. In that, click New... to launch the File Template Wizard. You will be asked for an optional file to turn into a template and prompted for template information settings, and a template file will be created for you.

Alternatively, you can create a template manually by adding template information to the top of any file, add text and macros, and save it with the katetemplate extension.

The template menu gets automatically updated if you chose to store your template in the template directory.

Editing templates

To edit a template, use the SettingsManage Templates.... Select the template you want to work on and click Edit..., and the template file will be opened. Close the dialog, edit the template file as desired, save it and close it. Changes to templates takes immediate effect, you can activate the template to test your changes after saving it.

The Kate Template Format

If you use files with the extension katetemplate, they will be parsed for template information, macros and a cursor position.

Template information

While reading in the file, the parser keeps lines beginning with the phrase katetemplate: and searches them for template information in the form VARIABLENAME=VALUE. The first line not starting with katetemplate: will be taken as the start of the template contents. VALUE may contain any character but equal sign (=). Legal variable names are:

Template

This is the template name, displayed in the FileNew from Template menu.

Group

The group places the template in a submenu of the FileNew from Template menu.

Name

This is the name that will be set for the document, and displayed in the file list and title bar. If the name contains %N that will be replaced with a number, increasing if more documents has the same name.

Highlight

The plug-in will try to set the Highlight for the new document to the value of this variable. The value should be the name, as found in the ToolsHighlighting.

Description

A short informative description of the template. This is currently used to set a Whatsthis string for the menu item, but may be used for more purposes in the future.

Author

A string identifying the author, for example in the form Name <email address>. This is currently used to set a Whatsthis string for the menu item, but may be used for more purposes in the future.

Template Macros

While parsing the template contents, macros in the form %{NAME} or ${NAME} are expanded. If you use the $ prefix, the expanded macro will be treated as a editable variable when a document is created from the template, whereas if you use % it is not, unless expanding failed.

The following macros are expanded:

time

Expands to the current time in your locale format.

date

Expands to the current date in short format.

datetime

Expands to the current date and time, formatted as a string according to your locale.

year

The current year as a four digit number.

month

The full name of the current month, according to your locale.

day

Expands to the current day of the month.

hostname

Expands to the 'hostname' of your computer.

index

Expands to 'i'.

fullname

Expands to your full name, as defined by the owner addressee in your standard KDE addressbook.

firstname

Expands to your first name, as defined in the owner addressee in your standard KDE addressbook.

lastname

Expands to your last name, as defined in the owner addressee in your standard KDE addressbook.

email

Expands to your email address, as defined by the owner address in your standard KDE addressbook.

Any macro not in the above list is treated as a editable variable no matter the prefix. If the same variable occurs multiple times in the template, they can be edited at once after creating a document from the template.

Setting the cursor position

The special macro ${cursor} will be replaced with a vertical bar and added to the end of the list of editable variables, independent on its location in the text.

Thanks and Acknowledgments

Kate Plugin File Templates copyright 2004 Anders Lund .

Documentation copyright 2004 Anders Lund