在UNIX/VIM,替代某些文本,如果行中包含某些文字

问题描述:

例如,给定文本:在UNIX/VIM,替代某些文本,如果行中包含某些文字

source: today is monday 
target: tomorrow is monday 

我想替换'monday''tuesday'如果行包含'target'

+0

这种问答** **可能是在S.E.更合适相关网站http://superuser.com或http://vi.stackexchange.com。使用Q底部的'flag'链接并请主持人移动它。请不要在2个不同的网站上发布相同的Q.在此处发布更多Q​​值之前,请阅读http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/help/dont-ask和http://stackoverflow.com/help/mcve。祝你好运 – shellter

您可以使用\zs在模式所需的字符串之前设置比赛开始:

:%s/target.*\zsmonday/tuesday/ 

另一个(略少可读性)可能是使用\@<=向后看断言:

:%s/\(target.*\)\@<=monday/tuesday/ 

使用全局,:g和正常替换,:s

:g/target/s/monday/tuesday/g 

如需更多帮助,请参见:

:h :s 
:h :g