Showing posts with label items. Show all posts
Showing posts with label items. Show all posts

Tuesday, March 10, 2009

Change Last, First to First Last

Here's a tip borrowed from the Vim help file. Say that you have a list of names like:

Doe, John
Smith, Peter

and you want to change that to:

John Doe
Peter Smith

You can use the following substitute pattern to easily do so:

:%s/\([^,]*\), \(.*\)/\2 \1/

Basically, you're capturing zero or more NOT commas followed by a comma and space and then capturing zero or more of anything. The comma and space are outside the parens, so they're not stored. The parens store the captured groups in the special variables \1, \2, \3, \N from left to right, so the replacement pattern is substituting in the groups in the reverse order they were captured.