12/04/2008

Use Reg Expression in VIM

    When I am writing blog, I will use photos from flickr very often. When posting photos to blogspot, I usually upload them to flickr first, then use the "add image" button in blogger edit box. At the mean time, I usually modify the image target link to the url of the original size photo. When the photo count is relatively large, for example, larger than 10, doing these things maually is annoying.

     So I decided to do it in two steps using VIM's replacing command:(Suppose we now have all image's urls in vim)

1. Compose the Blogspot style image anchor tags.
VIM command: %s/^\(\S\+\)$/str1\1str2\1str3

str1 -- <a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="
str2 -- "><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 240px; height: 135px;" src="
str3 -- " border="0" alt="" /></a>


2. Modify the image anchor target to be the original size photo:
The medium size photo's url is like: http://farm3.static.flickr.com/2357/2096529455_4bc506c8ff_m.jpg
The original size photo's url is like: http://farm3.static.flickr.com/2357/2096529455_4bc506c8ff_b.jpg

VIM command:%s/^\(\S\+href\S\+\)\(_m\)\(\S\+\)$/\1_s\2


So it becomes much easier to do blogging on blogspot with a lot of image urls.

For VIM reg expression and replacing syntax, see the reference documents(Sorry, all in Chinese).

Reference(In Chinese):
[1] http://ubuntu-fans.blogspot.com/2007/02/vi.html
[2] http://bbs.et8.net/bbs/showthread.php?t=746650
[3] http://vimcdoc.sourceforge.net/doc/pattern.html#pattern.txt

No comments: