Find every third value and insert cr or a new line in VIM

So, I have some large datasets that I need to make more readable, and currently I need to go in and go to every third value and insert a new row. I tried several things in VIM to make this work, but nobody seems to return the value I'm looking for. Here are some of my data:

(0.96260310749184663, 4.3830008206495812, 0.84922658632317849), (0.96260310749184663, 5.0000002088986637, 1.049701855818201), (0.96260310749184697, 5.6169993576359696, 0.8492264385213405), (0.96260310749184719, 5.9983257940402384, 0.32437568665165911), (0.96260310749184719, 5.9983258053918069, -0.32437572732698844), (0.96260310749184719, 5.6169994358349786, -0.84922691097323821), (0.96260310749184697, 5.0000000093711492, -1.0497019267383632) 

What I need to do is look like this:

 (0.96260310749184663, 4.3830008206495812, 0.84922658632317849), (0.96260310749184663, 5.0000002088986637, 1.049701855818201), (0.96260310749184697, 5.6169993576359696, 0.8492264385213405), (0.96260310749184719, 5.9983257940402384, 0.32437568665165911), (0.96260310749184719, 5.9983258053918069, -0.32437572732698844), (0.96260310749184719, 5.6169994358349786, -0.84922691097323821), (0.96260310749184697, 5.0000000093711492, -1.0497019267383632) 

I used this for selection to try to move this 3rd value down, but he duplicated the whole thing and then moved the whole line down:

 :'<,'>s/\([-[0-9.]\+,\s[-[0-9.]\+,\)/\1\r&/g 

I also tried removing 1 to make it work, but that didn't work either. So is there any way to capture this third value and insert a return carriage or a new line to make this work? Thanks.

Change ---

I apologize for the incorrect communication part of my problem: the data in the upper part is on one line, and not on several lines. It looks like this:

 (0.31852533878680489, 0.10352149350126813, -0.0046069731261429991), (0.31852526554320226, -0.103521543762028, -0.0046069731261429991), (0.19682845687859715, -0.27102285045297636, -0.004606973126142444), (-8.1184530326649769e-05, -0.33500267513407755, -0.0046069731261416669), (-0.19699089317458821, -0.27102292369657782, -0.0046069731261408897), (-0.31868770183919259, -0.10352150714022559, -0.0046069731261403346), (-0.31868770183919221, 0.10352156674487166, -0.0046069731261403346), 
+4
source share
6 answers
 :g/./norm! 2Wi^M 

Explanation:

  • :g/./{cmd} will run {cmd} on each line
  • norm! will execute the following line as normal mode commands
  • 2Wi^M Move 2 WORDS , then insert return
  • ^M is performed by pressing <cv><cr> or <cq><cr> .

It is very tempting to make %norm! 2Wi^M %norm! 2Wi^M , but this will not succeed, since it will ruin the executable lines.

+5
source

Two approaches

1. ex commands and formatting

My first thought was textwidth :

 :se tw=50 :g/./norm! gqq :%s/^[^(]/ &/g 

it

  • sets text width to 50 characters
  • reformat each (non-empty) line
  • inserts a space at the beginning of any lines that do not start with (

2. use a macro

Alternatively: make a macro

 gg qq2f,a<CR><Esc>j0q 100000@q 

Justification:

  • go to the beginning of the buffer
  • Macro recording q ( qq starts recording, q ends recording)
    • 2f, - forward to the second comma
    • a<CR><Esc> - add a new line
    • j0 - next line, move the cursor to the first character
  • rinse and repeat (100000 is the number of repetitions, processing stops when the macro does not work (for example, at the end of the file)
+5
source

Since all the text that needs to be broken is on the same line initially, you can use one short replacement command,

 :s/,.\{-},\|), /&\r/g 
+2
source

The way I do this would be something like this:

 '<,'>s/^[^,]\+,[^,]\+,\zs/\r 

Transfer:

 '<,'> " Over the visual range s/X/Y " Substitute X with Y ^ " Start of line [^,]\+ " Anything that isn't a comma, one or more, as many as possible , " A comma (end of first field) [^,]\+ " Anything that isn't a comma, one or more again , " A comma (send of second field) \zs " Mark this point as the start of the match so we don't have to bother including all of the above in the result \r " We're replacing nothing at the end of the above match with a new-line (\r) 

Another alternative:

 '<,'>s/^\(.\{-},\)\{2}/&\r 

Transfer:

 '<,'>s/X/Y " As before ^ " Start of line \(...\) " A group containing: .\{-}, " Everything up to and including the first comma \{2} " Match the preceding group twice (so up to and including the second comma) &\r " Replace with what was already there followed by a new-line 

And more ... considering that you have the same data widths, go to the space in front of the third column of the first row. Press Ctrl-V and omit so that you select the entire column of spaces ( 6j in your example). Press either s or Shift-i (depending on whether you want to save space), then ENTER and then ESC .

+1
source

Use macros to format the line and skip to the beginning of the next line. Then apply this macro as often as possible.

So, the beginning of the q_ macro recording, where _ is the register, then edit your line, press q again to save the macro. And finally, apply it with @_ , which you can prefix several times when you want to repeat it.

+1
source

Make a recursive macro (as an alternative to @sehe, the macro response for 100,000 times).

ggqa2f,a<CR><Esc> j@aq

Then @a to run the command on the second line (you do not need to move the cursor).

Visualization:

gg

enter image description here

qa

enter image description here

2f,

enter image description here

a<Cr><Esc>

enter image description here

j@aq

enter image description here

@a

enter image description here

+1
source

All Articles