Rakesh
Rakesh

Reputation: 283

Repeating replacement pattern number of times in vim

I want to do the following in vim.

Upvotes: 2

Views: 1836

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172520

That's another case for the powerful :help sub-replace-expression:

:%s/\(\w\),\(\d\+\)/\=join(repeat([submatch(1) . '@'], submatch(2)), ',')/g

This matches a word character (\w) followed by , and a number, appends @ to each matched word character (submatch(1) . '@), turns that into a List ([...]), multiplies the List element according to the matched number (repeat()), then join()s that back into a ,-separated string, which is used as the replacement.

This is based on your example; you need to tweak the pattern according to your particular needs.

Upvotes: 7

Kent
Kent

Reputation: 195029

vim has repeat() function. This command should do what you want:

%s/\v([a-z]),(\d+)/\=substitute(repeat(submatch(1).'@,',submatch(2)),',$','','g')/g

Note that this line will change foo,3abc into foo@,o@,o@abc. The result might be not exactly what you are looking for, but by reading the command, you should know the idea, you can change it to make it fit your needs.

Upvotes: 2

carlo denaro
carlo denaro

Reputation: 429

You can use https://github.com/msanders/snipmate.vim Snippet like SublimeText Editor

Example

snippet doctype XHTML 1.0 Transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

if you type

doctype XHTML 1.0 Transitional

and press [TAB] - u take autocompleted text

u can use parameters like

snippet proto
${1:class_name}.prototype.${2:method_name} =
function(${3:first_argument}) {
    ${4:// body...}
};

:-)

Upvotes: 1

Related Questions