vim - call function inside 'replace' expression

大憨熊 提交于 2019-12-23 07:28:30

问题


I understand you can call a function in a vim search/replace operation. For example:

%s/regex/\=localtime()/g

will replace anything matching 'regex' with the current epoch time. The problem is, I can't add anything else to the 'replace' expression. For example:

%s/regex/epoch: \=localtime()/g

does not treat 'localtime' as a function anymore. Rather it just prints 'epoch: =localtime()' as a string in the replacement text. The intention is for it to print 'epoch: 1353085984' instead.

Is there any way to call a function from within an arbitrary section of a replacement expression?


回答1:


Once you use a replace-expression, it must be the entire replacement. You can use String concatenation in your expression (and refer to captured elements via submatch(1) instead of \1), like this:

:%s/regex/\='epoch: ' . localtime()/g


来源:https://stackoverflow.com/questions/13421430/vim-call-function-inside-replace-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!