Finding exact match in vim

后端 未结 4 1885
春和景丽
春和景丽 2021-02-02 16:26

Using / or ? enables to find a match for a word in vim. But how can I find an exact match? For example, my text contains the following words: a a

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 16:58

    To find a single, standalone aa (not a, aaa, ...), you need to assert no match of that character both before and afterwards. This is done with negative lookbehind (\@ in Vim's regular expression syntax) and lookahead (\@!) enclosing the match itself (a\{2}):

    /\%(a\)\@

    Simplification

    Those assertions are hard to type, if the border around the match is also a non-keyword / keyword border, you can use the shorter \<\a\{2}\> assertions (as in MosteM's answer), but this doesn't work in the general case, e.g. with xaax.

提交回复
热议问题