:vimgrep is Vim’s built-in search command for powerful, pattern-based searches across files or buffers. It leverages Vim’s regex capabilities and settings like ignorecase and smartcase.

Features of vimgrep

1. Vim-Specific Patterns

Vim extends standard regex with unique patterns for more flexible searches:

  • \v: “Very magic” mode. Treats most characters as regex metacharacters, reducing the need for backslashes.
    • Example: :vimgrep /\vTODO|FIXME/ **/*.c
  • \zs and \ze: Define the start (\zs) and end (\ze) of the match within a larger pattern.
    • Example: Match numbers after “ID:”: :vimgrep /ID:\zs\d\+/ file.txt
  • \|: Logical OR for matching multiple patterns.
    • Example: Match “error” or “warning”: :vimgrep /error\|warning/ log.txt
  • \%l: Match at specific line numbers.
    • Example: Match “TODO” in lines <20: :vimgrep /TODO\%<20l/ main.c

2. Behavior with Settings

  • ignorecase: Makes searches case-insensitive.
    :set ignorecase
    :vimgrep /error/ file.txt
  • smartcase: Overrides ignorecase if the search pattern contains uppercase characters.
    :set ignorecase smartcase
    :vimgrep /Error/ **/*.log

3. Using the Argument List (args)

Vimgrep supports the Argument list


Examples of vimgrep Usage

Basic Searches

  1. Search for “TODO” in all .py files in the current directory and subdirectories:

    :vimgrep /TODO/ **/*.py
  2. Search for “main” in a single file:

    :vimgrep /main/ main.c

Advanced Searches

  1. Match “FIXME” only in the first 50 lines of a file:
    :vimgrep /FIXME\%<50l/ code.c
  2. Search for “TODO” followed by numbers:
    :vimgrep /TODO\zs\d\+/ tasks.txt
  3. Search with an OR pattern:
    :vimgrep /error\|warning/ logs/*