在替换中使用vim的printf

时间:2016-12-26 11:19:45

标签: regex vim

我正在尝试转换日期格式 m / d / yyyy 7/1/2016

每行发生一次至YYYY-mm-dd 00:00:00

我有搜索&参数部分工作,可以交换订单等,

但是我想用vim的printf来做这件事并提出以下内容......

new

我明白了......

true

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您需要使用submatch()功能:

:%s/\(\d\+\)\/\(\d\+\)\/\(\d\+\)/\=printf('%d-%02d-%02d 00:00:00', submatch(3),
submatch(1), submatch(2))/
:help submatch()
submatch({nr}[, {list}])                                submatch()
                Only for an expression in a :substitute command or
                substitute() function.
                Returns the {nr}'th submatch of the matched text.  When {nr}
                is 0 the whole matched text is returned.
                Note that a NL in the string can stand for a line break of a
                multi-line match or a NUL character in the text.
                Also see sub-replace-expression.(...)