Code Monkey home page Code Monkey logo

urgrep's Introduction

Urgrep - Universal Recursive Grep

GNU ELPA version

Urgrep is an Emacs package to provide a universal frontend for any grep-like tool, as an alternative to the built-in M-x rgrep (and other similar packages). Currently, ugrep, ripgrep, ag, ack, git grep, and grep/find are supported.

Why Urgrep?

One package, many tools

No matter which tool you prefer, you can use it with Urgrep. If a new tool comes along, you won't need to find a new Emacs package for it.

Rich minibuffer interface

Easily manipulate per-search options with Isearch-like key bindings within the Urgrep minibuffer prompt.

Seamless support for Tramp

Each host can use a different set of tools depending on what the system has installed without any special configuration.

Using Urgrep

The primary entry point to Urgrep is the interactive function urgrep. This prompts you for a query to search for in, by default, the root directory of the current project (or the default-directory if there is no project). By prefixing with C-u, this will always start the search within the default-directory. With C-u C-u, Urgrep will first prompt you for the search directory. Within the search prompt, there are several Isearch-like key bindings to let you modify the search's behavior:

Key binding Action
M-s h Describe key bindings
M-s r Toggle regexp search
M-s c Toggle case folding
M-s H Toggle searching in hidden files
M-s f Set wildcards to filter files¹
M-s C Set number of lines of context²
M-s B Set number of lines of leading context²
M-s A Set number of lines of trailing context²
M-s t Set the search tool
  1. Prompts with a recursive minibuffer
  2. With a numeric prefix argument, set immediately; otherwise, use a recursive minibuffer

In addition to the above, you can call urgrep-run-command, which works like urgrep but allows you to manually edit the command before executing it.

Modifying your search

After performing a search, you can adjust an existing query with C-u g, reopening the original search prompt. You can also adjust some of the search options, such as case folding, immediately:

Key binding Action
c Toggle case folding
H Toggle searching in hidden files
C Expand lines of context¹
B Expand lines of leading context¹
A Expand lines of trailing context¹
  1. Expand by one line by default, or by N lines with a prefix argument

Configuring the tool to use

By default, Urgrep tries all search tools it's aware of to find the best option. To improve performance, you can restrict the set of tools to search for by setting urgrep-preferred-tools:

(setq urgrep-preferred-tools '(git-grep grep))

If a tool is installed in an unusual place on your system, you can specify this by providing a cons cell as an element in urgrep-preferred-tools:

(setq urgrep-preferred-tools '((ag . "/home/coco/bin/ag")))

This also works with connection-local variables:

(connection-local-set-profile-variables 'urgrep-ripgrep
 '((urgrep-preferred-tools . (ripgrep))))

(connection-local-set-profiles
 '(:application tramp :machine "imagewriter") 'urgrep-ripgrep)

Using with wgrep

wgrep provides a convenient way to edit results in grep-like buffers. Urgrep can hook into wgrep to support this as well. To enable this, just load urgrep-wgrep.el.

Using with Xref

Xref lets you search through your projects to find strings, identifiers etc. You can make Xref use Urgrep to generate its search command by loading urgrep-xref.el.

Using with outline-minor-mode

Inside of Urgrep buffers, you can enable outline-minor-mode. This will let you toggle the visibility of each file's results.

Using with Eshell

In Eshell buffers, you can call urgrep much like you'd call any command-line recursive grep command. The following options are supported:

Option Action
-G, --basic-regexp Pattern is a basic regexp
-E, --extended-regexp Pattern is an extended regexp
-P, --perl-regexp Pattern is a Perl-compatible regexp
-R, --default-regexp Pattern is a regexp with the default syntax
-F, --fixed-strings Pattern is a string
-s, --case-sensitive Search case-sensitively
-i, --ignore-case Search case-insensitively
-S, --smart-case Ignore case if pattern is all lower-case
--group / --no-group Enable/disable grouping results by file
--hidden / --no-hidden Enable/disable searching hidden files
-Cn, --context=n Show n lines of context
-Bn, --before-context=n Show n lines of leading context
-An, --after-context=n Show n lines of trailing context

Programmatic interface

In addition to interactive use, Urgrep is designed to allow for programmatic use, returning a command to execute with the specified query and options: urgrep-command. This takes a query as well as several optional keyword arguments. For more information, consult the docstring for urgrep-command.

Contributing

This project assigns copyright to the Free Software Foundation, so if you'd like to contribute code, please make sure you've filled out the assignment form and that it's up to date. In any case, before submitting patches, it's probably best to file an issue first so that we can discuss the best way to do things.

urgrep's People

Contributors

jimporter avatar wavexx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

urgrep's Issues

[question] Toggle options and re-run search in result buffer

I've been testing urgrep with ugrep, coming from ripgrep with the rg mode.

There are a few things I liked from the rg package, and I wonder if I can achieve something similar in urgrep. I'll describe what I do: I often run a search with something selected while programming, so that searching in a project is usually pretty quick.

After the search I often want to change some settings, then re-run the query. I know I can toggle settings in the prompt, but I'd like to do so after the fact, when I see the results. For example, I'd love to be able to toggle "-w" easily to restrict the current search if I see too many spurious matches. Likewise, toggle search-case, then re-run. If I ran urgrep manually, searching for a regex, I would often want to modify the regex and just re-run the search immediately to see the results (ugrep "-Q" is actually one of the small things that prompted me to compare it to ripgrep). Right now I only see urgrep-search-again being bound, which does the job with a prefix arg, but feels kind of slow.

"rg" mode has quick one-keystroke toggles to modify/toggle the option, then re-run the query. Is there already a quick way to do so?

Just mentioning other ideas, "rg" also has an header bar showing the state of the of these toggles. Admittedly, I barely even watched it, but it does provide a convenient way to see the major toggles without parsing the command in the beginning. The only thing I was mostly looking at was the current file type (ug -t option).

Expansion of ~ on Windows

If the project is under HOME directory on windows (where HOME env variable has been defined), none of the grep utilities works correctly. But it works correctly under linux. Probably we may have to use (expand-file-name) to get the absolute path of the directory

-*- mode: urgrep; default-directory: "~/Repo/xxxx/AsyncFamily/" -*-
Urgrep started at Sun Dec 17 18:05:33

rg --color=always "--colors=path:none" "--colors=path:fg:magenta" "--colors=line:none" "--colors=column:none" "--colors=match:none" "--colors=match:fg:red" "--colors=match:style:bold" --heading -F -- myStore

Wgrep support

Thank you for working on this package!

For compatibility with the wgrep package (https://github.com/mhayashi1120/Emacs-wgrep), is there a plan to keep the traditional grep-mode output (at least as an option) so that this package can be used in tandem with wgrep in project-wide search and replace operations?

Thanks!

Minor suggestion about using `compat`

Hi Jim,

Well, how about that, another Emacser named Porter. :) I saw your post about urgrep on emacs-devel. Looks very interesting. Quick suggestion, rather than conditionally defining some functions from later Emacs versions, I think they should generally be available in the compat library on ELPA; so if you depend on that package and require it, you should be able to just call the expected functions directly.

Expansion of ~ on Windows

          I use project.el to open the project and files. I use ripgrep to search for pattern and it is not working. The project stores the path of project in .emacs.d/projects file as "~/Repo" etc. That could be the problem. This problem is not seen if I am using consult-ripgrep. This can be reproduced with ugrep also.

Originally posted by @elge70 in #7 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.