tags:

views:

96

answers:

4

I have a block of codes with timestamp in front of each line like this:

12/02/2010 12:20:12 function myFun()
12/02/2010 12:20:13 {....

The first column is a date time value. I would like to command them out by using VIM

/*12/02/2010 12:20:12*/ function myFun()
/*12/02/2010 12:20:13*/ {....

I tried to search for date first:

/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d

I got all the timestamps marked correctly. However When I tried to replace them by the command:

%s/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d/\/*\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d*\// 

I got the following result:

/*dd/dd/dddd dd:dd:dd*/ function myFun()
/*dd/dd/dddd dd:dd:dd*/ {....

I think I need to name the search part and put them back in the replace part. How I can do it?

+1  A: 
%s/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d/\/*&*\// 
Ignacio Vazquez-Abrams
David.Chu.ca
`:h sub-replace-special`
Ignacio Vazquez-Abrams
+5  A: 

I suppose I would just do something like:

:%s-^../../.... ..:..:..-/* & */-
DigitalRoss
+1, all those backslashes give me headaches.
Carl Norum
A: 
:%s/^\([0-9/]* [0-9:]* \)\(.*\)/\/*\1*\/ \2/
ghostdog74
+3  A: 

I would actually not us a regex to do this. It takes too long to enter the correct formatting. I would instead use a Visual Block. The sequence works out to be something like this.

<C-V>}I/* <ESC>
3f\s
<C-V>I */

I love regex, and don't want to knock the regex solutions, but find when doing things with pre-formatted blocks, that this is easier, and requires less of a diversion from the real task, which isn't figuring out how to write a regex.

frogstarr78