Tag Archives: vim

Regular Expression Magic and ViM

Every now and then I have an editing task for which Eclipse is just not up to the job. Usually it involves making a lot of changes all at once. (I realize that Eclipse has regex find/replace, but I feel much better working in ViM in these cases.)

Here is an example: I have a block of Java enum code that I want to split out into an enum and two maps.

Here is the regular expression I use to parse the enum declaration:


Here is an example of an enum declaration that matches this regular expression:

FACEBOOK(Constants.FACEBOOK, "fbconnect://success", "fbconnect://success?error_reason"),

I then run three s// commands on the input:

%s//callbackMap.put(Provider.\1, \3);/g
%s//cancelMap.put(Provider.\1, \4);/g

After each command I copy the buffer and paste it back into Eclipse, then hit ‘u’ to undo my changes so that I can run the next command on the original buffer contents.

Here’s what I get (these aren’t meant to be used in isolation like this, of course):

callbackMap.put(Provider.FACEBOOK, "fbconnect://success");
cancelMap.put(Provider.FACEBOOK, "fbconnect://success?error_reason");

For complete examples, check behind the cut.

Continue reading Regular Expression Magic and ViM