Regex to insert space in vim

前端 未结 4 1336
轻奢々
轻奢々 2021-02-05 13:57

I am a regex supernoob (just reading my first articles about them), and at the same time working towards stronger use of vim. I would like to use a regex to search for all inst

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 14:32

    An interesting feature of Vim regex is the presence of \zs and \ze. Other engines might have them too, but they're not very common.

    The purpose of \zs is to mark the start of the match, and \ze the end of it. For example:

    ab\zsc
    

    matches c, only if before you have ab. Similarly:

    a\zebc
    

    matches a only if you have bc after it. You can mix both:

    a\zsb\zec
    

    matches b only if in between a and c. You can also create zero-width matches, which are ideal for what you're trying to do:

    :%s/:\zs\ze\S/ /
    

    Your search has no size, only a position. And them you substitute that position by " ". By the way, \S means any character but white space ones.

    :\zs\ze\S matches the position between a colon and something not a space.

提交回复
热议问题