Vim Tips Wiki
(Assign tip id + convert to TipNew template + minor clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{TipNew
 
{{TipNew
 
|id=1513
 
|id=1513
|previous=1512
+
|previous=1511
 
|next=1514
 
|next=1514
|created=August 10, 2007
+
|created=2007
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Alex Jakushev
 
|author=Alex Jakushev
 
|version=6.0
 
|version=6.0
  +
|subpage=/200712
 
|category1=Advanced Regex
  +
|category2=HTML
 
}}
 
}}
 
One part of converting from HTML to XHTML is changing all the tags to lowercase. If you open your HTML file in Vim, this task may be done with this piece of Vim magic:
 
One part of converting from HTML to XHTML is changing all the tags to lowercase. If you open your HTML file in Vim, this task may be done with this piece of Vim magic:
Line 25: Line 28:
 
:%s/<\([^>]*\)>/<\L\1>/g
 
:%s/<\([^>]*\)>/<\L\1>/g
 
</pre>
 
</pre>
:This is exactly what I thought of to start with. It is a nice, simple, easy regular expression to accomplish ''almost'' the same task. But, it is not quite equivalent. The two regular expressions given in the tip would replace <tt><nowiki><TAG ATTR='VAL'></nowiki></tt> with <tt><nowiki><tag attr='VAL'></nowiki></tt>, whereas this one would replace it with <tt><nowiki><tag attr='val'></nowiki></tt>. Sometimes this is desired, sometimes not, for example with <tt><nowiki><p class='bigLongClassName'></nowiki></tt>. Note that none of these regular expressions will work across linebreaks.
+
:This is exactly what I thought of to start with. It is a nice, simple, easy regular expression to accomplish ''almost'' the same task. But, it is not quite equivalent. The two regular expressions given in the tip would replace <code><nowiki><TAG ATTR='VAL'></nowiki></code> with <code><nowiki><tag attr='VAL'></nowiki></code>, whereas this one would replace it with <code><nowiki><tag attr='val'></nowiki></code>. Sometimes this is desired, sometimes not, for example with <code><nowiki><p class='bigLongClassName'></nowiki></code>. Note that none of these regular expressions will work across linebreaks.
   
 
----
 
----
  +
The first search pattern does not recognize tags that have digits in it,
[[Category:Advanced Regex]]
 
  +
like <code><nowiki><H1></nowiki></code>.
[[Category:HTML]]
 
  +
It can be corrected replacing the <code>\a</code> (=any alphabetic character) by a <code>\w</code> (=any word character) which includes digits.

Latest revision as of 06:28, 13 July 2012

Tip 1513 Printable Monobook Previous Next

created 2007 · complexity intermediate · author Alex Jakushev · version 6.0


One part of converting from HTML to XHTML is changing all the tags to lowercase. If you open your HTML file in Vim, this task may be done with this piece of Vim magic:

:%s/<\/\?\zs\(\a\+\)\ze[ >]/\L\1/g

Note that this will change tag names only. To change tag attributes to lowercase as well (multiple attributes supported), use this command:

:%s/\(<[^>]*\)\@<=\<\(\a*\)\ze=['"]/\L\2/g

Comments[]

This should work as well:

:%s/<\([^>]*\)>/<\L\1>/g
This is exactly what I thought of to start with. It is a nice, simple, easy regular expression to accomplish almost the same task. But, it is not quite equivalent. The two regular expressions given in the tip would replace <TAG ATTR='VAL'> with <tag attr='VAL'>, whereas this one would replace it with <tag attr='val'>. Sometimes this is desired, sometimes not, for example with <p class='bigLongClassName'>. Note that none of these regular expressions will work across linebreaks.

The first search pattern does not recognize tags that have digits in it, like <H1>. It can be corrected replacing the \a (=any alphabetic character) by a \w (=any word character) which includes digits.