Friday, January 29, 2010

Re: HTML editing: treat tags as a unit

On 11/12/09 17:24, Erik Falor wrote:
> On Fri, Dec 11, 2009 at 10:38:46AM -0500, Dennis German wrote:
>> What are your favorite scripts for editing HTML?
>>
>> Is there a script that treats HTML tags as units?
>> Like maybe mapping gw and gb ?
>>
>> For example with:
>>
>> <tr><td colspan="5"><h1>
>>
>> Starting in the first column,
>> currently the first w positions
>> after the "<" the next w before the">"
>> and the third w skips over the "><" and
>> cursor is positioned in the middle of the next tag
>> before the "td"
>>
>> I would like the gw to skip to the end of the<tr>
>>
>> In the following the pipe "|" shows the desired units
>>
>> <tr>|<td colspan="5">|<h1>
>>
>> Thanks in advance.
>>
>
> One way to do this is to use the f and F commands with< >. Read up
> on it at :help f.
>
> Also, checkout :help text-objects. Combining text-objects with
> normal-mode commands is one of Vim's strongest features. Basically,
> text-objects allow you to perform any action you might take on a word
> or line of text on an entire HTML tag, including its child nodes.
>
> Devin Weaver's xmledit plugin is highly rated and helpful, too:
> http://www.vim.org/scripts/script.php?script_id=301
>

In particular, the following objects are relevant to HTML/XML/etc.:

at from <tag> to the corresponding </tag> (including the tags)
it the contents between <tag> and </tag> (but not the tags
themselves)
a< from < to the corresponding > (inclusive at both ends)
i< from < to the corresponding > (exclusive at both ends)

at (or 1at ) means "the innermost tag containing the cursor", 2at is the
one around that, etc. These objects are meant to be used after an
operator, where a movement would be expected, e.g. yit to "yank the
inner tag", dat to "delete a tag", v2at to select the 2nd-level tag
around the cursor as a characterwise visual area, etc.

In all the above cases, nesting will be correctly handled (e.g. in the
case of a <div> containing another <div> inside it, or of a <ul> or <ol>
list where one of the <li> lines contains a further <ul> or <ol>, etc.).

I don't think it works for "implicitly closed" HTML tags, such as a <p>
closed implicitly by the next <p> in the absence of an intervening </p>,
similarly for <td> <td> without </td> or <li> <li> without </li> etc.;
but I haven't tried.


I also use the CloseTag plugin, which I still find useful, though its
usefulness has admittedly diminished now that you can use
omni-completion (after </ ) to close a tag. And then there is the
matchit plugin (distributed with Vim but not enabled by default) to jump
(when editing HTML) from < to > and back, from <tag> to </tag> and back,
etc.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
250. You've given up the search for the "perfect woman" and instead,
sit in front of the PC until you're just too tired to care.

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment