Home > Net >  Break line after n matches of a certain string in vim
Break line after n matches of a certain string in vim

Time:08-12

Let the next string aaaa, bbb, ccc, iii, hdjhda90., jjsjkasjaks, 00-01. I would like a help to break this line after n matches of ",". For example for n=2 I expect

aaaa, bbb, 
ccc, iii, 
hdjhda90., jjsjkasjaks, 
00-01

I tried using \{n,m} without sucess

CodePudding user response:

This is how I would do it if I felt like doing it with a substitution:

:s/,.\{-},\zs /\r

The pattern matches a ,, followed by any character, as few as possible of it, followed by a , and a . The trick is to ignore whatever comes before the with :help \zs so that only the is replaced.

The is replaced with a newline.

But, frankly, I would probably use :help /, :help c, and :help gn instead:

/, <CR>
ncgn,<CR><Esc>
nn.
nn.

CodePudding user response:

One way would be to record a macro:

qq2f,a<CR>
  • 2f, moves the cursor to the second comma in the line
  • a<CR> (where <CR> is you pressing 'return') breaks the line

The you can do 999@q to repeat the macro to break up the line.

  • Related