Monday, March 17, 2008

VIM Regex Cheatsheet

A quick and dirty vim cheatsheet for common regex stuff


Delete all lines that do not start with "www" (:v is a negative match):


Delete all blank lines, including lines with only whitespace (:g is a positive match):


Remove the string " - 300k - Cached" from google search results, and matches any size in k:

:g/ - \d+k - Cached//g

Replace string "XXX" on every line with the current line number:


For CSV file with 2 values separated by a comma per line. Swap the values on each line.



Sort all lines alphabetically, line by line:


Sort all lines numerically, line by line:

:sort n

Sort lines 1,2 and 3 numerically, leaving the rest alone:

:1,3sort n

Sort all lines, remove duplicates:

:sort u

Sort all lines reverse numerically:

:sort! n


Validate string is an email address - This expects that there is only one email in the string, with no leading/trailing whitespace. For more details on email regex, take a look at



Here is a case where I need to transform google maps KML polygon data into a format that will work with Bing maps via the javascript api (version 7). I chose to push each set of polygon points into a javascript array of map location types as shown below.
--- Initial idea of how data comes from KML. Tags are not valid KML tags, just used for example data
<KML><Content>-77.315148,39.284253 -77.308349,39.277322 -77.29868,39.276026<Content></KML>

--- end result to be copied/pasted into a .js file
--- note that the Location function/method takes lat/long in reverse order of what KML gives us. v.push(new Microsoft.Maps.Location(39.284253,-77.315148));
v.push(new Microsoft.Maps.Location(39.277322,-77.308349));
v.push(new Microsoft.Maps.Location(39.276026,-77.29868));
Here are the vim commands I ran against the raw KML data to create the end result shown above
--- remove all xml tags
--- The dataset I get is all on one line, each record separated by a space. Replace the space with a newline to get records on their own row
%s/ /\r/
--- swap thing before the comma with thing after the comma (unfortunately it leaves an extra comma at the end of the result)
--- remove that extra comma at the end
--- prepend each record with the javascript code we want for pushing the row into a JS array. This is specific to this projects js code
%s/^/vertices.push(new Microsoft.Maps.Loaction(/
--- append each record with end parens and semicolon for good JS

No comments: