How to extract regex matches using Vim

后端 未结 5 1732
悲哀的现实
悲哀的现实 2020-12-08 15:52

An example:

case Foo:
    ...
    break;
case Bar:
    ...
    break;
case More: case Complex:
    ...
    break:
...

I’d like to retrieve al

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 16:32

    Though it's not possible to write a one-liner to accomplish your example, it's hard to type commands such as :%s/case \([^:]*\):/\=.../ interactively.

    I prefer using vim-grex with the following steps:

    1. Use / to check whether a regular expression matches to expected lines. For example: /^\s*\
    2. Execute :Grey. It yanks lines matched to the current search pattern.
    3. Open a new buffer by :new etc.
    4. Put the yanked lines by p etc.
    5. Trim uninteresting parts by :%s//\1/.

提交回复
热议问题