Search code examples
vimneovim

How to search for all occurrences of the string `**` in a file using vim?


How to search for ** in a file using vim like **kwargs , when I use a /** it highlights the entire file.


Solution

  • * is a meta-character that needs to be escaped to become a regular character:

    /\*\*
    

    Vim refers to this as "magic": if the pattern is "magic", you need to escape meta-characters, if it is "nomagic", you don't (this is a bit over-simplified, see :help magic).

    Therefore, you can force Vim to treat the pattern as "nomagic" with \M or even "very nomagic", with \V:

    /\V**
    

    Which doesn't help much, here, but well…

    As for why /** highlights the whole buffer, a hint can be found under :help /star:

    Exception: When "*" is used at the start of the pattern or just after
    "^" it matches the star character.
    
    • The first * matches (at least some of) the stars in your buffer,
    • The second * matches zero or more of the preceding atom, as many as possible, so, essentially, every character.