vim: replace sub-match with the same number of new strings -
my plan pretty standard search replace, replacing instances of old_string new_string. problem want arbitrary number of old_strings following specific prefix.  example:
old_string = "a" new_string = "b" prefix = "xxxx"  xxxxaaaaaaaa => xxxxbbbbbbbb xxxxaaapostfix => xxxxbbbpostfix xxaaaa => xxaaaa   etc.  i'm not sure how this.  imagine there's way s/xxxxa*/xxxxb{number of a's}/g or something, have no idea is.
you can this! use \= register evaluate vimscript. :h s/\=:
substitute expression           *sub-replace-expression*                         *sub-replace-\=* *s/\=* when substitute string starts "\=" remainder interpreted expression.  special meaning characters mentioned @ |sub-replace-special| not apply except "<cr>".  <nl> character used line break, can 1 double-quote string: "\n".  prepend backslash real <nl> character (which nul in file).   then can use repeat , submatch functions build right string. example:
:%s/\(xxxx\)\(a\+\)/\=submatch(1).repeat('b', len(submatch(2)))   i chose use \+ instead of * because pattern not found after substitute command finished (this effects hlsearch , n)
of course, if use \zs , \ze (start/end of match) atoms, can use less capturing groups, makes waaay shorter , clearer.
:%s/xxxx\zsa\+/\=repeat('b', len(submatch(0)))      
Comments
Post a Comment