Packetizer

Using Vim to Convert HTML Tags from Uppercase to Lowercase

September 3, 2009

Bookmark and Share

Sometimes, a good tip is worth sharing. As I was making some edits to some HTML pages using Vim, I wanted to find a quick way to convert uppercase tag and attribute names to lowercase. I found this tip somewhere on the Internet and it worked so well, I thought it might be worth repeating.

To convert HTML tags from uppercase to lowercase, use this command in Vim:

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

To convert HTML attributes from uppercase to lowercase, use this command in Vim:

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

4 comment(s)

by anonymous February 12, 2010 08:31 UTC

Thank you, I was searching for something like this. I would appreciate if you could add an explanation what's happening. I know some regex and some vim but I've never seen zs, ze or @<=. The latter is look-ahead?

by Paul E. Jones February 12, 2010 18:27 UTC

When I saw the syntax, I had to look through the vim documentation to see what it was doing, but decided that it was something I would so rarely use that it wasn't worth trying to understand it in depth.

What the documentation says is:

\@<= Matches with zero width if the preceding atom matches just before what follows.

\ze Matches at any position, and sets the end of the match there: The previous char is the last char of the whole match.

\zs Matches at any position, and sets the start of the match there: The next char is the first char of the whole match.

Finding this documentation was tricky, but it is worth reading if you want better understanding. To find it, I typed "help @<=" and, once in the help file, searched for \ze and \zs. All of those are documented in the same help file.

by Ferg.... June 16, 2010 09:38 UTC

Sweeeeeeeeeet tip....
And the html monkeys in here look at me funny for using vim, try doing that in dreamweaver my boy!!

by KaBaDaBrA June 29, 2010 14:55 UTC

in dreamweaver just head over to file -> convert -> XHTML 1.0 TRANSITIONAL and it does it for you, afterwards you can convert back to any one you want...super easy and simple!


Click here to view a complete blog index.
Click here to view the main blog page.

Paul E. Jones

About Me
My Blog ATOM Feed
Email Me
IM Me