DEV Community

Cover image for ugrep: an interactive grep for the terminal
Axel Navarro for Cloud(x);

Posted on

ugrep: an interactive grep for the terminal

The ugrep tool is a faster, user-friendly and compatible grep replacement written in C++11. Let's check how to install ugrep and how to use the interactive query mode, fuzzy search, and other options.

What is grep?

If you are a terminal user, grep is a basic tool that you must learn. The grep command is a filter that prints lines from a file that contain a match for one or more patterns.

E.g. grep can help you to find specific output in a log file like the following:

grep 10.0.0.1 server.log
Enter fullscreen mode Exit fullscreen mode

In this post we're going to focus in the ugrep app, an extension of this tool.

Installation

In Windows, you can just download the full-featured ugrep.exe from the official repository, or use a tool like: choco install ugrep or scoop install ugrep.

In MacOS, you could use brew install ugrep.

Or, find your destiny here: https://github.com/Genivia/ugrep#install.

The basics

The basic usage is start the interactive query mode and look for a specific text in recursively in sub-directories using plain-text or regex.

ugrep -Q
Enter fullscreen mode Exit fullscreen mode

ugrep query mode in tldr pages repository

💡 Tip: you can add the --ignore-case argument to make case-insensitive matches.

If you want to see only the files that contains the search term you can use the following arguments:

ugrep -Q --ignore-case --files-with-matches
Enter fullscreen mode Exit fullscreen mode

ugrep query mode listing filenames in tldr pages repository

The fuzzy search

The fuzzy search is the technique of look for a text that match a pattern approximately, instead of exactly. We can look for a text within the specified Levenshtein distance.

ugrep -Z3 android
Enter fullscreen mode Exit fullscreen mode

💡 Tip: -Z3 matches up to three extra, missing or replaced characters; -Z+~3 allows up to three insertions (+) or substitutions (~), but no deletions (-).

You can start a query mode with fuzzy search like the following:

ugrep -Q -Z3
Enter fullscreen mode Exit fullscreen mode

Interactive TUI

Once you started the query mode, you can toggle fuzzy search, list only filenames, or more in options panel pressing F1.

ugrep help and options

Press the upper case Z to toggle on/off the fuzzy search mode, and [ to increase and ] to decrease the fuzziness.

Also, you can scroll the result of a search using the arrow keys or ^S, then with F2 you can open the editor for the file of the current matched text (nano, vim, etc).

More ugrep

You have a lot of possible customization options you could investigate in the README or use ugrep with Vim.

Thanks to Dr. Robert van Engelen for this awesome tool! and if you 💛 it too, leave a ⭐ in https://github.com/Genivia/ugrep.

Discussion (0)