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
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