Using Text Commands
- 3 minutes to read
You can use Text Commands to control different aspects of the expanded template. You can use Text Commands for the following tasks:
Clipboard Contents
The Paste text command allows you to insert the clipboard content at the specified caret position. Click the “Insert clipboard paste command” toolbar button in the template editor to add the Paste text command.
For example, if the clipboard contains the “192.168.1.15” string, CodeRush expands the “nsq” template as follows:
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Data Source=192.168.1.15; User id=; Password=;";
sqlConnection.Open();
try {
}
finally {
sqlConnection.Close();
}
Note
If you want to change the template expansion when the clipboard contains no proper data, create an alternative expansion for this template and add a context restriction to the base template. The template that contains the «Paste» text command should restrict expansion when the clipboard does not contain proper data.
Caret Position
Use the «Caret» text command to put the caret at a specified position after the template expansion. You can insert the text command which marks the caret position with the second Template Editor toolbar button.
This template expands with the caret placed at the specified position, as shown below.
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Data Source=localhost; User id=; Password=;";
sqlConnection.Open();
try {│
}
finally {
sqlConnection.Close();
}
Selection
You can use two text commands — «Caret» and «BlockAnchor» — to select a part of your template after expansion. The text between these text commands is selected after expansion, and the caret is placed at the «Caret» text command’s position. You can insert the «Caret» and «BlockAnchor» text commands using the second and third Template Editor toolbar buttons.
After expansion, this template contains the selected text block.
Markers
Note
To enable this feature, use the Enable Markers checkbox on the Editor | All languages | Markers options page.
Use markers to label important positions in your template and jump to them after expansion with a single keystroke. You can drop markers at the required positions and collect them after the expansion. To insert the «Marker» text command, click the fourth Template Editor toolbar button.
After expansion, this template contains two markers, which can be sequentially collected using the Esc key.
Note
If you are not sure that the marker is required, you can specify its lifetime. This deletes the marker if it is not collected after a certain period. You can pass the lifetime in seconds as a parameter to the «Marker» text command. For instance, the marker declared as «Marker(10)» is removed in ten seconds after the template expansion.
Expand Another Template
You can expand a template inside another template expansion. To insert the corresponding Text Command, right-click the template editor area and choose the Insert Alias… item from context menu.
Select the required template in the invoked editor and click OK.
This inserts the text command that expands the selected template inside your template.