Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a string of a multiple copies of a substring in Vim

Tags:

vim

I'm working on a Vimscript that does some string manipulation. I'm wondering if there's a way to generate string by multiplying another string by some multiple. For example, 'a' * 5 would produce 5 a's: 'aaaaa'. Until now, I've been doing it with a while loop like this:

let l:char = 'a'
let l:x = 5
let l:i = 0
let l:somestr = ""
while l:i < l:x
    let l:somestr .= l:char
    let l:i += 1
endwhile

This seems awfully verbose for what I'm doing. Any way to make this more compact?

like image 693
erynofwales Avatar asked Mar 08 '13 20:03

erynofwales


1 Answers

The repeat() function can do that; not just for strings, also for list elements:

:let l:somestr = repeat(l:char, l:x)
like image 112
Ingo Karkat Avatar answered Oct 30 '22 15:10

Ingo Karkat