Macro for html, xml, etc. any file with <markup></m

Usage tips, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
helios
Posts: 710
Joined: Sun Mar 02, 2003 5:52 pm
Location: Helios Software Solutions
Contact:

Macro for html, xml, etc. any file with <markup></m

Post by helios »

Macro for html, xml, etc. any file with <markup></markup>

Purpose: Complete the opening <tag> with a closing </tag> without retyping the markup keyword.

Use Case:
The user types <body
The user types [Ctrl] > (Control + greater than key)
The macro completes <body></body> and position the cursor between the two tags.

Recording Procedure:
1) Type <
2) Type body
3) Start recording
4) Type the following:
>
[Ctrl][Shift]M
[Ctrol]+c
[Right]
[Right]
</
[Ctrl]+v
>
[Ctrl]M
[Left]


5) Stop recording.
6) Assign [Ctrl]. To the macro. (The dot "." is in the same key as the ">" -in my keyboard at least- )
7) Assign [Ctrl][Shift]. to the macro, also. In case somebody pressed [ctrl][shift] > just because the ">" is the "shift" key of the dot.

Now test the macro typing: <ThisIsATag and now [Ctrl]>

Contributed by Marcelo Finkielsztein.
BenjaminB
Posts: 10
Joined: Tue Sep 12, 2006 3:09 pm

Post by BenjaminB »

It's pretty useful, but I think I may have an improvement for that, because it doesn't work so well if you have tags with attributes. (<a href="URI"> or <div id="header">, for example)

I altered it to make it work in all cases:

Type <a href="URI"
Start recording
Type:

>
[Ctrl]+[Shift]+[M]
[Left]
[Ctrl]+[Shift]+[Right]
[Ctrl]+[C]
[Ctrl]+[Shift]+[M]
[Right]
</
[Ctrl]+[V]
>
[Ctrl]+[Shift]+[M]
[Ctrl]+[Shift]+[M]
[F8] (or whatever key is assigned to find and replace)
Search for: ([^ ]*)[ ]?(>)
Replace with: \1\2
(Click "Find")
(Click "Replace")
(Close the Window)
[Right]
[Ctrl]+[Shift]+[M]
[Left]
[Left]

Stop Recording

(I assume you are using Posix Syntax and Regular Expressions is checked at the Search&Replace Dialogue. That deletes the possible space inside the closing tag.)


Now save your Makro and assign any shortcut you like. This should work with both simple tags and tags that have attributes:
<a href="http://www.example.org"
+Macro becomes
<a href="http://www.example.org"></a>

<tag
+Macro becomes
<tag></tag>
User avatar
rsperberg
Posts: 35
Joined: Thu Jul 29, 2004 2:26 pm
Location: NJ

Slight change to account for nested elements

Post by rsperberg »

I was very delighted to discover this first version of the automatic end-tag creation macro by Marcelo, and then I found situations where it didn't work as intended.

These weren't, however, elements with attributes. Instead, the problem I stumbled across was creating a new, second element nested inside the first:

<li><p></p></li>

So I've modified BenjaminB's improved version to anticipate there being text -- and specifically a set of paired angle brackets -- following the characters being entered in the macro. Otherwise the [Ctrl]+[Shift]+[M] won't always be selecting what was intended.

This works for the nested elements and elements with attributes, at least with the handful of tests I ran:

(Start recording):

>
[Left]
[Ctrl]+[Shift]+[M]
[Left]
[Ctrl]+[Shift]+[Right]
[Ctrl]+[C]
[Ctrl]+[Shift]+[M]
[Right]
</
[Ctrl]+[V]
>
[Left]
[Ctrl]+[Shift]+[M]
[Ctrl]+[Shift]+[M]
[F8] (or whatever key is assigned to find and replace)
Search for: ([^ ]*)[ ]?(>)
Replace with: \1\2
(Click "Find")
(Click "Replace")
(Close the Window)

[Left]
[Ctrl]+[Shift]+[M]
[Left]

(Stop recording)

I found it very convenient to assign [Ctrl]+> to this macro (that's [Ctrl]+[Shift]+.) as Marcelo suggests, because it helps me remember that I'm closing both the start-tag with a > and the whole element with an end-tag.

Thanks to Marcelo and BejjaminB for making this!

Roger Sperberg
Last edited by rsperberg on Wed Jan 03, 2007 11:56 am, edited 1 time in total.
BenjaminB
Posts: 10
Joined: Tue Sep 12, 2006 3:09 pm

Re: Slight change to account for nested elements

Post by BenjaminB »

rsperberg wrote:So I've modified BenjaminB's improved version to anticipate there being text -- and specifically a set of paired angle brackets -- following the characters being entered in the macro. Otherwise the [Ctrl]+[Shift]+[M] won't always be selecting what was intended.
I didn't think of that, thank you :)
troels_kn
Posts: 32
Joined: Fri Oct 28, 2005 12:51 pm

Post by troels_kn »

If you don't use POSIX type regexp, you can instead use This for search and replace, respectively:

Code: Select all

[ ]?>
>
Post Reply