Regular expression explanation for vim

前端 未结 3 1499
旧时难觅i
旧时难觅i 2021-02-09 15:58

If I want all the lines with the text \'ruby\' but not \'myruby\' then this is what I would do.

:g/\\/

My question is what is the

3条回答
  •  暖寄归人
    2021-02-09 16:31

    Vim's rules for backslash-escaping in regexes are not consistent. You have to escape the opening brace of\{...}, but [...] requires no escaping at all, and a capture group is \(...\) (escaping both open and close paren). There are other inconsistencies as well.

    Thankfully Vim lets you change this behavior, even on a regex-by-regex basis, via the magic settings. If you put \v at the beginning of a regex, the escaping rules become more consistent; everything is "magic" except numbers, letters, and underscores, so you don't need backslashes unless you want to insert a literal character other than those.

    Your first example then becomes :g/\v/ and your second example becomes /\v^\n{3}. See :h /magic and :h /\v for more information.

提交回复
热议问题