在unix / vim中,如果该行包含某些文本,则替换某些文本

时间:2017-08-10 20:22:04

标签: unix vim

例如,给定文字:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    var observer: NSKeyValueObservation?

    /* ... */

    override func viewDidLoad() {
        super.viewDidLoad()

        let handler = { (textView: UITextView, change: NSKeyValueObservedChange<CGSize>) in
            if let contentSize = change.newValue {
                print("contentSize:", contentSize)
            }
        }
        observer = textView.observe(\UITextView.contentSize, options: [NSKeyValueObservingOptions.new], changeHandler: handler)
    }

}

如果该行包含source: today is monday target: tomorrow is monday ,我想用'monday'替换'tuesday'

2 个答案:

答案 0 :(得分:2)

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

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

如需更多帮助,请参阅:

:h :s
:h :g

答案 1 :(得分:0)

您可以在模式中使用\zs在所需字符串之前设置匹配的开头:

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

另一种(稍微不那么可读)的可能性是使用\@<=后视断言:

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