在 vim 正则表达式中替换期间如何用空格填充插入的表达式到给定长度

how to fill with spaces an inserted expression to a given length during substitution in vim regex

我想在 c++ 中实现类似于 setw()setfill() 的行为。例如我想替换表达式:

15.8

与:

_______15,8

在文本中和:

10000

与:

_10000 

要实现此类行为,evaluate the substitute string as an expression 的能力很有帮助。 如果e。 G。我们把

let s:fill = "_"
func! Setfill(fill)
    let s:fill = a:fill
endfunc

let s:width = 8
func! Setw(width)
    let s:width = a:width
endfunc

func! Fild()
    let m = submatch(0)
    return repeat(s:fill, s:width-len(m)) . m
endfunc

在vimrc文件中,我们可以使用

:call Setw(6)
:%s/10000/\=Fild()/