eastafri
eastafri

Reputation: 2226

Split a string into subtrings based on fixed length with vim

In most scripting languages it is easy to split a string into fixed length substrings without a delimiter. e.g. in ruby I can do

'acgatgctgc'.scan(/.{3}/).join(' ') #=>"acg atg ctg"

What is the equivalent of doing this using vim script? or achieving the same with a single command in vim?

edit NB: notice Ruby strips the last c

Upvotes: 1

Views: 350

Answers (1)

Kent
Kent

Reputation: 195039

join(split('acgatgctgc','.\{3}\zs'),' ')

the above line will give you

"acg atg ctg c"

I know there is a c, it could be removed by filter() function, if you want to remove it:

join(filter(split('acgatgctgc','.\{3}\zs'),'len(v:val)==3'),' ')

will give you:

"acg atg ctg"

I don't know if it answers your question.

Upvotes: 3

Related Questions