Regex to insert space in vim

前端 未结 4 1370
轻奢々
轻奢々 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条回答
  •  Happy的楠姐
    2021-02-05 14:47

    you probably want to use :[^ ] to mach everything except spaces. As mentioned by Matt this will cause your replace to replace the extra character.
    There are several ways to avoid this, here are 2 that I find useful.
    1) Surround the last part of the search term with parenthesis \(\), this allows you to reference that part of the search in your replace term with a /1.
    Your final replace string should look like this:

    %s/:\([^ ]\)/: \1/g
    

    2) end the search term early with \ze This will means that the entire search term must be met for a match, but only the part before \ze will be higlighted / or replaced
    Your final replace string should look like this:

    %s/:\ze[^ ]/: /g
    

提交回复
热议问题