LinuxCommandLibrary

vgrep

Search files for lines not matching pattern

TLDR

Recursively search the current directory for a pattern and cache it

$ vgrep [search_pattern]
copy

Display the contents of the cache
$ vgrep
copy

Open the "4th" match from the cache in the default editor
$ vgrep [[-s|--show]] [4]
copy

Display a context of "3" lines for each match in the cache
$ vgrep [[-s|--show]] [[c|context]][3]
copy

Display the number of matches for each directory in the tree
$ vgrep [[-s|--show]] [[t|tree]]
copy

Display the number of matches for each file in the tree
$ vgrep [[-s|--show]] [[f|files]]
copy

Start an interactive shell with cached matches
$ vgrep [[-i|--interactive]]
copy

SYNOPSIS

vgrep [options] pattern [file ...]
or
command | vgrep [options] pattern

PARAMETERS

-A NUM, --after-context=NUM
    Print NUM lines of trailing context after matching lines.

-B NUM, --before-context=NUM
    Print NUM lines of leading context before matching lines.

-C NUM, --context=NUM
    Print NUM lines of context around matching lines. Equivalent to -A NUM -B NUM.

-v, --invert-match
    Invert the match; select non-matching lines.

-i, --ignore-case
    Ignore case distinctions in patterns and data.

-F, --fixed-strings
    Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE). This is the default.

-G, --basic-regexp
    Interpret PATTERN as a basic regular expression (BRE).

-P, --perl-regexp
    Interpret PATTERN as a Perl regular expression (PCRE).

-w, --word-regexp
    Select only those lines containing matches that form whole words.

-x, --line-regexp
    Select only those matches that match the whole line.

-n, --line-number
    Prefix each line of output with the 1-based line number within its input file.

-h, --no-filename
    Suppress the prefixing of filenames on output when multiple files are matched.

-H, --with-filename
    Print the filename for each match. This is the default when there is more than one file.

-l, --files-with-matches
    Suppress normal output; instead print the name of each input file from which output would normally have been printed.

-L, --files-without-matches
    Suppress normal output; instead print the name of each input file from which no output would normally have been printed.

--exclude=GLOB
    Skip files whose base name matches GLOB (wildcard).

--exclude-dir=GLOB
    Exclude directories whose names match GLOB.

--include=GLOB
    Only search files whose base name matches GLOB.

-m NUM, --max-count=NUM
    Stop reading a file after NUM matching lines.

-s, --no-messages
    Suppress error messages about non-existent or unreadable files.

--color[=WHEN]
    Highlight matches. WHEN can be always, never, or auto.

--group-separator=SEP
    Specify a string to be printed between groups of matched lines; useful with context options.

--label=LABEL
    Specify the label to display for standard input.

--less-options=OPTS
    Pass additional options directly to less.

-V, --version
    Display version information and exit.

--help
    Display a help message and exit.

DESCRIPTION

vgrep is a powerful command-line utility that merges the pattern-matching capabilities of grep with the interactive file browsing features of less. Unlike grep which outputs results and exits, vgrep allows users to search for patterns within files or piped input, then interactively navigate through the matched lines and their surrounding context within a less-like interface. This makes it exceptionally useful for exploring large log files or codebases where you need to quickly locate specific information and then examine the surrounding lines without re-running grep and less separately. It highlights matches and provides common less navigation shortcuts, significantly streamlining the workflow for log analysis and code inspection by offering an immediate interactive view of search results.

CAVEATS

vgrep might not be installed by default on all Linux distributions; it's typically part of the moreutils package.
While it mimics grep's options, not all obscure grep options might be fully supported or behave identically.
Performance on extremely large datasets (many terabytes) or an enormous number of files might not be as optimized as tools like ripgrep or ag which are designed for raw speed. Its strength is interactivity rather than raw throughput for batch processing.

INTERACTIVE NAVIGATION

Once vgrep displays its output, users can navigate the results using standard less commands (e.g., j for next line, k for previous, G for end, g for beginning, / for search within results, n for next match, N for previous match, q to quit). This interactive mode is its primary advantage over traditional grep.

COMMON USE CASES

vgrep is ideal for debugging by sifting through large log files, exploring codebases to understand specific function calls or variable usages, and interactively filtering output from other commands where immediate, contextual viewing of results is beneficial.

HISTORY

vgrep is part of the moreutils package, a collection of small, useful Unix utilities. moreutils was primarily created and maintained by Wayne Marshall. The utilities in moreutils, including vgrep, aim to extend the standard set of Unix tools by providing small, single-purpose programs that complement existing functionality, often by combining the capabilities of two or more standard tools (like grep and less in vgrep's case) or providing missing conveniences. Its development philosophy aligns with the Unix tradition of small, composable tools.

SEE ALSO

grep(1), less(1), more(1), ack(1), ripgrep(1), find(1)

Copied to clipboard