vgrep - a user-friendly pager for grep

vgrep logo

Build Status

vgrep is a pager for grep, git-grep, ripgrep and similar grep implementations, and allows for opening the indexed file locations in a user-specified editor such as vim or emacs. vgrep is inspired by the ancient cgvg scripts but extended to perform further operations such as listing statistics of files and directory trees or showing the context lines before and after the matches. vgrep runs on Linux, Windows and Mac OS.

Please, feel free to copy, improve, distribute and share. Feedback and patches are always welcome!

Install Instructions

Searching Patterns

The basic functionality of vgrep is to perform textual searches. On a technical level, vgrep serves as a front-end to grep or git-grep when invoking vgrep inside a git tree and uses less for displaying the results. All non-vgrep flags and arguments will be passed down to grep. Results of the last search are cached, so running vgrep without a new query will load previous results and operate on them.

An example call may look as follows:

By default, the output will be written to less to make browsing large amounts of data more comfortable. vgrep --no-less will write to stdout.

Opening Matches

vgrep can open the indexed file locations in an editor specified by the EDITOR environment variable. Opening one of the file locations from the previous example may look as follows:

# export EDITOR=gedit
# vgrep --show 4

The default editor of vgrep is vim with the default flag to open a file at a specific line being + followed by the line number. If your editor of choice hits the rare case of a different syntax, use the EDITORLINEFLAG environment variable to adjust. For example, a kate user may set the environment to EDITOR="kate" and EDITORLINEFLAG="-l".

Note that vgrep does not allow for searching and opening files at the same time. For instance, vgrep --show=files text should be split in two commands: vgrep text and vgrep --show=files.

IDE Support

Note that if you run vgrep inside a terminal of VSCode or Goland, the format of listed files changes to "$PATH:$LINE" to allow for opening the matches in the editor via a simple mouse click.

More Commands and the Interactive Shell

Once vgreped, you can perform certain operations on the results such as limiting the range of indexed matches, listing matching files and directories and more.

Enter a vgrep command: ?
vgrep command help: command[context lines] [selectors]
         selectors: '3' (single), '1,2,6' (multi), '1-8' (range), 'all'
          commands: print, show, context, tree, delete, keep, refine, files, grep, quit, ?

vgrep commands can be passed directly to the --show/-s flag, for instance as --show c5 1-10 to show the five context lines of the first ten matched lines. Furthermore, the commands can be executed in an interactive shell via the --interactive/-i flag. Running vgrep --interactive will enter the shell directly, vgrep --show 1 --interactive will first open the first matched line in the editor and enter the interactive shell after.

vgrep supports the following commands:

  • print to limit the range of matched lines to be printed. p 1-12,20 prints the first 12 lines and the 20th line.
  • show to open the selectors in an user-specified editor (requires selectors).
  • context to print the context lines before and after the matched lines. c10 3-9 prints 10 context lines of the matching lines 3 to 9. Unless specified, vgrep will print 5 context lines.
  • tree to print the number of matches for each directory in the tree.
  • delete to remove lines at selected indices from the results, for the duration of the interactive shell (requires selectors).
  • keep to keep only lines at selected indices from the results, for the duration of the interactive shell (requires selectors).
  • refine to keep only lines matching the provided regexp pattern from the results, for the duration of the interactive shell (requires a regexp string).
  • files will print the number of matches for each file in the tree.
  • grep start a new search without leaving the interactive shell (requires arguments for a vgrep search). For example, g -i "foo bar" dir/ will trigger a case-insensitive search for foo bar in the files under dir. The cache will be updated with the results from the new search.
  • quit to exit the interactive shell.
  • ? to show the help for vgrep commands.

vgrep command examples

Context lines

Tree

Files

fzf

If you desire a more interactive experience than running vgrep twice to first search and then to open an editor, you may have a look at fzf. The below function uses fzf to interactively search with vgrep and open your editor with one enter at the correct line. To use it add the following function to your .bashrc and install fzf alongside vgrep and ripgrep.

vgrep() {
  INITIAL_QUERY="$1"
  VGREP_PREFIX="vgrep --no-header "
  FZF_DEFAULT_COMMAND="$VGREP_PREFIX '$INITIAL_QUERY'" \
  fzf --bind "change:reload:$VGREP_PREFIX {q} || true" --ansi --phony --tac --query "$INITIAL_QUERY" \
  | awk '{print $1}' | xargs -I{} -o vgrep --show {}
}
Owner
Valentin Rothberg
Principal Engineer @RedHatOfficial. Working on all things containers. Bavarian in France.
Valentin Rothberg
Comments
  • Seems like -A, -B and -C is broken in the latest release

    Seems like -A, -B and -C is broken in the latest release

    Seems like https://github.com/vrothberg/vgrep/issues/84 stopped working on the latest release. I'm getting stack traces errors or "failed to parse results" whenever I try to use one of those options.

  • Add option to install with `brew`

    Add option to install with `brew`

    Hi, For easier installation on Mac, brew install elazarl/homebrew-vgrep/vgrep, I added the repository http://github.com/elazarl/homebrew-vgrep

    Can you please fork this repository here, and mention it as a viable way to install vgrep on Mac?

  • Script fails with IndexError: list index out of range

    Script fails with IndexError: list index out of range

    $ vgrep main
    Traceback (most recent call last):
      File "/home/geertjohan/bin/vgrep", line 246, in <module>
        main()
      File "/home/geertjohan/bin/vgrep", line 104, in main
        slocs.append(SLOC(hit))
      File "/home/geertjohan/bin/vgrep", line 31, in __init__
        self.file = SLOC.parse_file(string)
      File "/home/geertjohan/bin/vgrep", line 38, in parse_file
        return RE_FILE.findall(string)[0]
    IndexError: list index out of range
    
  • interactive: switch from bufio Scanner to (readline-like) liner

    interactive: switch from bufio Scanner to (readline-like) liner

    Use the liner Go library to process commands in interactive mode. It provides better support for input (such as directional arrows) and a number of classic readline shortcuts. It also does command history, which can be handy for editing the range of a previous command for example. Autocompletion also exists but is not implemented in this commit (the one-letter commands hardly need any completion).

    Drawback: This obviously adds a dependency. Although I believe it really makes interactive mode more user-friendly, I would understand if this change is undesirable.


    Edit: Based on the discussion, I switched from readline library to liner

  • Why the dependency on build dir????

    Why the dependency on build dir????

    How to get rid of this annoying thing??

    bhaskar@Gentoo_04:39:21_Mon Jun 29:~> vgrep --interactive
    error loading cache: please cd into /home/bhaskar/git-linux/vgrep to use old cache
    bhaskar@Gentoo_04:39:50_Mon Jun 29:~> vgrep -d
    DEBU[0000] log level set to debug
    DEBU[0000] passed args: []
    DEBU[0000] loadCache(): start
    DEBU[0000] loadCache(): end
    error loading cache: please cd into /home/bhaskar/git-linux/vgrep to use old cache
    bhaskar@Gentoo_04:40:50_Mon Jun 29:~> cd -
    /home/bhaskar/git-linux/vgrep
    ✔ ~/git-linux/vgrep [master|✚ 1]
    04:41 $ make clean
    rm -rf ./build
    ✔ ~/git-linux/vgrep [master|✚ 1]
    04:41 $ cd ~
    bhaskar@Gentoo_04:41:07_Mon Jun 29:~> vgrep --interactive
    error loading cache: please cd into /home/bhaskar/git-linux/vgrep to use old cache
    
  • vgrep failed randomly on fedora 35

    vgrep failed randomly on fedora 35

    vgrep work normally before today,but failed with no output now.

    ~ which vgrep 1 х │ root@fedora │ 16:12:40 vgrep='vgrep --interactive' /usr/bin/vgrep ~ vgrep --interactive
    ~ vgrep -d
    DEBU[0000] log level set to debug DEBU[0000] passed args: [] DEBU[0000] loadCache(): start DEBU[0000] loadCache(): end

  • add man pages

    add man pages

    I think it's time to add man pages to vgrep. As many other go projects, we can use go-md2man and use markdown. The content can be borrowed from the README.

  • Need to eliminate things related to old `-w` options

    Need to eliminate things related to old `-w` options

    Hi @vrothberg 😄 vgrep looks great tool !!

    But I saw the description and example regrading -w option, I thought it is because of the-w option from 46db0da, however, it is removed at 6385bab , right ?

    So I suggest modifying some parts of README.md on https://github.com/vrothberg/vgrep/blob/master/README.md#searching-patterns

  • Build Instructions and Dependencies

    Build Instructions and Dependencies

    Hi,

    Thanks for writing this tool. It greatly boosts my productivity with working on any codebase that I know inside out.

    However, I am unable to build the latest version. I don't have much knowledge of Go (I jumped on the Rust bandwagon), and so don't know much about the toolchain. A simple list with the dependencies required to build vgrep and some build instructions would be a great addition!

    One of the issues I had was that running make invokes the check target. Could you please remove that so that make only compiles and that make check can run the tests. This is important for packaging it, since we can clearly define the runtime and build time dependencies of the package.

  • vgrep looking for path to executable in $EDITOR and failing

    vgrep looking for path to executable in $EDITOR and failing

    It seems vgrep expects EDITOR to point to an executable (a path) rather than a command. I run Emacs in daemon mode, and always use it from the terminal, so for me it is set to: emacsclient -nw -a="", and vgrep fails with:

    exec: "emacsclient -nw -a=\"\"": executable file not found in $PATH
    

    I would expect a behaviour equivalent to: $EDITOR <options>, not "$EDITOR" <options>.

  • "All" selector not working?

    IIUC from this example (https://github.com/vrothberg/vgrep#more-commands-and-the-interactive-shell) I could use an "all" selector to edit all the matched expressions, is that correct? I'm getting a "unsupported command 'all'" when trying to use that.

    $ vgrep -s all
    unsupported command "all"
    

    Am I missing something here?

  • more interactive experience in fish shell

    more interactive experience in fish shell

    I am using a fish shell and it seems like adding the following chunk:

    function vgrep
    set INITIAL_QUERY $1
    set VGREP_PREFIX "vgrep --no-header "
    set FZF_DEFAULT_COMMAND "$VGREP_PREFIX '$INITIAL_QUERY'" \
    fzf --bind "change:reload:$VGREP_PREFIX {q} || true" --ansi --phony --tac --query "$INITIAL_QUERY" \
    | awk '{print $1}' | xargs -I{} -o vgrep --show {}
    end
    

    Doesn't seem to do anything. After adding this vgrep just does nothing ie. no output for even vgrep --help

  • Return to vgrep from Editor

    Return to vgrep from Editor

    Not a real issue, more a question. :-)

    Is there a way to return to vgrep/fzf after opening a match in the editor? I'm using the bvgrep alias from the README for interactive searching and it would dramatically speedup things if I could go to a match by selecting it in the fzf list, then quit the editor and return to the (interactive) search that had before.

    Any chance that this could work?

👀 A modern watch command. Time machine and pager etc.
👀 A modern watch command. Time machine and pager etc.

Viddy Modern watch command. Viddy well, gopher. Viddy well. Demo Features Basic features of original watch command. Execute command periodically, and

Jan 2, 2023
🚀 goprobe is a promising command line tool for inspecting URLs with modern and user-friendly way.

goprobe Build go build -o ./bin/goprobe Example > goprobe https://github.com/gaitr/goprobe > cat links.txt | goprobe > echo "https://github.com/gaitr/

Oct 24, 2021
A fast and powerful alternative to grep

sift A fast and powerful open source alternative to grep. Features sift has a slightly different focus than most other grep alternatives. Code search,

Jan 3, 2023
Ddg - DuckDuckGo terminal search that plays nice with grep and fzf
Ddg - DuckDuckGo terminal search that plays nice with grep and fzf

ddg duckduckgo terminal search for use with fzf (or grep) What? With this tool y

Apr 1, 2022
lsp is like ls command but more human-friendly
lsp is like ls command but more human-friendly

lsp: list files in a mildly human-frendlier manner lsp lists files, like ls command, but it does not attempt to meet that archaic POSIX specification,

Dec 12, 2022
Zenstring - Randomly generated, friendly & zen strings for your app

zenstring Randomly generated, friendly & zen strings for your app. Started as a

Feb 3, 2022
A command-line tool and library for generating regular expressions from user-provided test cases
A command-line tool and library for generating regular expressions from user-provided test cases

Table of Contents What does this tool do? Do I still need to learn to write regexes then? Current features How to install? 4.1 The command-line tool 4

Jan 9, 2023
Heroku Terminal User Interface
Heroku Terminal User Interface

_ _ _ | |_| |_ _ _(_) | ' \ _| || | |

Nov 24, 2022
A command tool to help user install oh-my-zsh plugins fast in a comfortable way

zshx A command tool to help user install oh-my-zsh plugins fast in a comfortable way. in other way, it is a zsh plugin package manager. How to use the

Feb 11, 2022
User interface engine and widget library for Ebiten
User interface engine and widget library for Ebiten

Ebiten UI A user interface engine and widget library for Ebiten Ebiten UI is an extension to Ebiten that provides an engine to render a complete user

Nov 5, 2022
Github user stats fetch written in golang
Github user stats fetch written in golang

TACOMA It's like neofetch, but for github users. I saw something similar on reddit and decided to recreate it using only golang. Original inspiration:

Dec 24, 2021
Go library for detecting and expanding the user's home directory without cgo.

go-homedir This is a Go library for detecting the user's home directory without the use of cgo, so the library can be used in cross-compilation enviro

Jan 5, 2023
Light weight Terminal User Interface (TUI) to pick material colors written by Go.
Light weight Terminal User Interface (TUI) to pick material colors written by Go.

mcpick Light weight Terminal User Interface (TUI) to pick material colors. You do NOT need to take your hands off the keyboard to pick colors. Getting

Dec 27, 2022
cview - Terminal-based user interface toolkit

cview - Terminal-based user interface toolkit This package is a fork of tview. See FORK.md for more information. Demo ssh cview.rocketnine.space -p 20

Jan 23, 2022
Podman-tui - A Terminal User Interface to interact with the podman (v3.x)
Podman-tui - A Terminal User Interface to interact with the podman (v3.x)

podman-tui podman-tui is a Terminal User Interface to interact with the podman (

Dec 23, 2022
ghcv-cli makes it easy to view the user-created issues, pull requests, and repositories in the terminal.
ghcv-cli makes it easy to view the user-created issues, pull requests, and repositories in the terminal.

ghcv-cli ghcv-cli makes it easy to view the user-created issues, pull requests, and repositories in the terminal. About Show a list of pull requests c

Mar 13, 2022
K-Mesh is an experimental Knative distribution which provides a fresh, CLI-focused, holistic user experience of running and managing Knative.

K-Mesh is an experimental Knative distribution which provides a fresh, CLI-focused, holistic user experience of running and managing Knative. N

Feb 14, 2022
GOSNIFF - A Textual User-Interface Network Sniffer

GOSNIFF - A Textual User-Interface Network Sniffer gosniff-gif.mp4 GOSNIFF is a TUI-based, tcpdump-inspired tool used to provide some graphical insigh

Dec 17, 2022
Terminal user interface for nyaa.si with support of peerflix

nyaa-cli Terminal user interface for nyaa.si with support of peerflix. Peerflix can be enabled with the --peerflix flag. By default the tool will only

Jun 25, 2022