Monday, March 14, 2011

UltraEdit macro for HTML/XML - auto-create closing tags

Here is another pair of UltraEdit macros that I use quite frequently - these two help me with creating container tags for HTML or XML; specifically, they automate the creation of the opening and closing tag: all I need to do is type out the tag name and run the macro with the cursor within (or at the left/right boundary of) the tag name.

For example, if I type out xmlTag and run the first macro, I will be left with the following: <xmlTag>|</xmlTag>. The I bar (|) indicates where the cursor is left when the macro finishes.
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Clipboard 9
IfSel
Cut
"<"
Paste
">"
Else
IfSel
Else
SelectWord
EndIf
StartSelect
Cut
"<"
Paste
">"
EndIf
Clipboard 0
Paste
Clipboard 9
"</"
Paste
">"
Clipboard 0
The second macro is the same except that it will paste whatever is in the clipboard at the time and leave the cursor at the end of the closing tag. For example, if I type and cut the following text: text in the clipboard, then type out xmlTag and run the second macro, I will be left with the following: <xmlTag>text in the clipboard</xmlTag>|. The I bar (|) indicates where the cursor is left when the macro finishes.
InsertMode
ColumnModeOff
HexOff
UltraEditReOn
Clipboard 9
IfSel
Cut
"<"
Paste
">"
Else
IfSel
Else
SelectWord
EndIf
StartSelect
Cut
"<"
Paste
">"
EndIf
Clipboard 0
Paste
Clipboard 9
"</"
Paste
">"
Clipboard 0
Note that both the macros above include instructions that are from another common UltraEdit macro I use, Alt+x to cut currently selected token (selected or if cursor is in the token).
IfSel
Else
SelectWord
EndIf
StartSelect
Cut