vim - Get the length of the string in substitution -
i'd calculate length of replace string used in substitution. is, "bar" in :s/foo/bar
. suppose have access command string, can run , undo it, , may separate parts marked /
split(). how string length of replace string if contains special characters \1, \2 etc or ~?
for instance if have
:s/\v(foo)|(bars)/\2\rreplace/
the replace length strlen("bars\rreplace") = 12.
edit: clear, hope use move cursor past text affected substitute operation. i'd appreciate alternative solutions well.
you have use :help sub-replace-expression
. in it, use submatch(2)
instead of \2
. if expression custom function, can side effect store original length in variable, , access later:
function! replace() let g:replacelength = strlen(submatch(0)) " equivalent of \2\rreplace return submatch(2) . "\r" . 'replace' endfunction :s/\v(foo)|(bars)/\=replace()/
Comments
Post a Comment