HTML editing: treat tags as a word

2 views
Skip to first unread message

Dennis German

unread,
Nov 5, 2009, 10:16:01 AM11/5/09
to vim...@googlegroups.com
Is there a script that treats HTML tags as words?
Also desirable would be to not consider the
characters =./- as word delimiters.

For example with the line:

<tr><td><code><a href=http://www.somesite.com/directoy
style=text-decoration:none>description</a>
</code></td><td>quanity</td></tr>

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 before the "td"

I would like the w to skip to the end of the <tr>

In the following the pipe "|" shows the desired words

<tr>|<td>|<code>|<a| href=http://www.somesite.com/directory|
style=text-decoration:none>description</a>|
</code>|</td>|<td>|quanity|</td>|</tr>

Thanks in advance.

Tim Chase

unread,
Nov 5, 2009, 10:50:20 AM11/5/09
to vim...@googlegroups.com

I don't know of a readily available way to do this. You can get
a somewhat close approximation to your "not consider the
characters" bit by adding

set isk+=>
set isk+=.
set isk+=/
set isk+==

to your list of "word" characters as defined by the 'iskeyword'
setting. However this isn't quite what you want for the first
question of "treat[ing] HTML tags as words". My next suggestion
would be to use the f/F/t/T/,/; family of commands so you can do
things like

f<

or

t>

to rapidly move within the line to the desired character. More
on those at

:help f
(and following)

My last suggestion would involve knowing why you want to move by
these jumps, and if the HTML/XML-specific text-objects would help
you out. With your cursor in the word "description", you can do
things like

vit
dat

and with your cursor inside a tag such as on the "o" on
"somesite", you can do things like

ca<
yi<

which you can read about at

:help i<
:help a<
:help it
:help at
:help tag-blocks

So while none of them are exactly what you asked for, some mix
thereof may help bring you closer to what you want. I use the
text objects and the f/F/t/T/,/; suite on a near daily
basis...hopefully you'll find them as useful for your HTML/XML
editing too.

-tim


Dgerman@rws

unread,
Nov 5, 2009, 10:58:07 AM11/5/09
to vim...@googlegroups.com
Thanks for your suggestions
I will hive them a try

Dennis


On Nov 5, 2009, at 10:50 AM, Tim Chase <v...@tim.thechases.com> wrote:

>

Ben Fritz

unread,
Nov 6, 2009, 11:34:13 AM11/6/09
to vim_use


On Nov 5, 9:16 am, Dennis German <DGer...@Real-World-Systems.com>
wrote:
> 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 before the "td"
>
> I would like the  w to skip to the end of the <tr>
>
> In the following the pipe "|" shows the desired words
>
> <tr>|<td>|<code>|<a| href=http://www.somesite.com/directory|
> style=text-decoration:none>description</a>|
> </code>|</td>|<td>|quanity|</td>|</tr>
>

You have some good suggestions, but also consider proper use of W and
E in addition to w and e, to skip over all non-whitespace characters
to the beginning or end of a word, instead of all non-word characters.
This will give you what you want for your href=... text, for example.

Also of interest to you may be the at, it, a<, and i< text objects.
Take a look at :help text-objects. For example, to change
"description" when your cursor is anywhere within the entire <a> tag
(even over the href value, for example) just type cit and enter your
replacement text.

Incidentally, your example html may need some attention. Have you
validated your markup? I don't remember if html requires it, but at
the very least it's good practice (and required by xhtml) to quote all
your attribute values, like this:

<tr><td><code><a "href=http://www.somesite.com/directoy"
style="text-decoration:none">description</a>
</code></td><td>quanity</td></tr>

Either single or double quotes will work.

Ben Fritz

unread,
Nov 6, 2009, 11:50:26 AM11/6/09
to vim_use


On Nov 6, 10:34 am, Ben Fritz <fritzophre...@gmail.com> wrote:
> Incidentally, your example html may need some attention. Have you
> validated your markup? I don't remember if html requires it, but at
> the very least it's good practice (and required by xhtml) to quote all
> your attribute values, like this:
>
> <tr><td><code><a "href=http://www.somesite.com/directoy"
> style="text-decoration:none">description</a>
> </code></td><td>quanity</td></tr>
>

Whoops, like this:

<tr><td><code><a href="http://www.somesite.com/directoy"

Tony Mechelynck

unread,
Dec 1, 2009, 10:05:27 PM12/1/09
to vim...@googlegroups.com, Ben Fritz
In non-X HTML, quotes are not necessary unless you want to include
spaces etc.

Best regards,
Tony.
--
You cannot propel yourself forward by patting yourself on the back.

Dave Land

unread,
Dec 2, 2009, 3:18:44 AM12/2/09
to vim...@googlegroups.com
On Dec 1, 2009, at 7:05 PM, Tony Mechelynck wrote:

> On 06/11/09 17:50, Ben Fritz wrote:
>>
>> On Nov 6, 10:34 am, Ben Fritz<fritzophre...@gmail.com> wrote:
>>> Incidentally, your example html may need some attention. Have you
>>> validated your markup? I don't remember if html requires it, but at
>>> the very least it's good practice (and required by xhtml) to quote
>>> all
>>> your attribute values, like this:
>>>
>>> <tr><td><code><a "href=http://www.somesite.com/directoy"
>>> style="text-decoration:none">description</a>
>>> </code></td><td>quanity</td></tr>
>>
>> Whoops, like this:
>>
>> <tr><td><code><a href="http://www.somesite.com/directoy"
>> style="text-decoration:none">description</a>
>> </code></td><td>quanity</td></tr>
>
> In non-X HTML, quotes are not necessary unless you want to include
> spaces etc.

But they're ALWAYS advisable, or you can run into some *very* hard-
to-diagnose bugs.

This page <http://www.cs.tut.fi/~jkorpela/qattr.html> tells why. For
example, the following chunk of HTML validates, even though it is
manifestly wrong:

<a href=../test/abug.html>
<img alt="Yucca" src=../yucca.gif aling=right></a>
<a href=foo.html>bar</a>

More amazing, the fact that it validates is _not_ a bug in the
validator, but is because of the little-known "SHORTTAG" feature of
HTML, which (if it was supported *at all* in browsers) would allow
you to write this:

<em/foo/ instead of <em>foo</em>

It's kind of the ultimate version of self-closing tags.

As a result of it, the little chunk of HTML above, in the eyes of
the validator (which, because it is NOT a browser, so it follows
ALL of the arcane rules of HTML), is equivalent to this:

<a href=..>test</a>abug.html>
<img alt="Yucca" src=..>yucca.gif aling=right>
<a href=foo.html>bar</a>

Which is clearly not what was intended. Quoting the attributes would
have allowed the validator to notice that the "align" attribute is
misspelled.

Bottom line: quote your attributes. The time you save leaving out
the quotes won't be worth it.

Dave

Dennis German

unread,
Dec 2, 2009, 9:47:24 AM12/2/09
to vim...@googlegroups.com
Thanks for the additional insight.

Dennis German

unread,
Dec 2, 2009, 10:09:47 AM12/2/09
to vim...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages