Reputation: 283
I want to do the following in vim.
a,2
with a@,a@
b,3
with b@,b@,b@
c,6
with c@,c@,c@,c@,c@,c@
and so onUpvotes: 2
Views: 1836
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
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
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