Skip to content

ripgrep_replace, or rgr, is a light-weight wrapper around ripgrep, supporting 100% of ripgrep's features + adding '-R' to enable on-disk find-and-replace; rgf2 is an interactive regex finder and viewer with syntax highlighting

License

Notifications You must be signed in to change notification settings

ElectricRCAircraftGuy/ripgrep_replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hits

>> Become my 1st Sponsor on GitHub <<

Ripgrep Replace (rgr), and fuzzy finder search tools

Table of Contents

(click to expand)
  1. Help needed
  2. Status
  3. Installation
  4. ripgrep_replace (rgr)
    1. Example usage:
  5. ripgrep_fuzzyfinder (rgf)
  6. Fuzzy searching for files by filename: manually, and with sublf
  7. Alternatives to rgr
  8. Other, very-useful repos:

TODO: [#1]: add gif demo image here

Help needed

I need some testers on Mac and Windows! If you have those operating systems, please get this script running and document how you did it in a new Issue on this repo. I can then add your instructions to the "Installation" section in this readme, below.

  • test on Linux Ubuntu
  • test on MacOS
  • test on Windows

Status

Both rgr and rgf are done and fully implemented and work well.

Installation

For now, just see the top of each file for instructions in the comments.

TODO:

  1. add an installation script named install.sh that can be run in one single command, even withOUT downloading the repo manually first! Ex. cmd of what this might look like (see here for a demo of this type of cmd):
    wget https://github.com/ElectricRCAircraftGuy/ripgrep_replace/blob/main/install.sh -O /tmp/ && /tmp/install.sh

ripgrep_replace (rgr)

ripgrep_replace, or rgr, is a light-weight wrapper around ripgrep (rg), supporting 100% of ripgrep's features + adding -R to enable on-disk find-and-replace!

Finally, we can use ripgrep, the world's fastest grep (regular expression search) tool to do rapid code refactoring via find-and-replace name changes from the command-line!

The Ripgrep author says here:

I am never going to add this [replace in files] to ripgrep. It isn't happening.

Normally I would lock this issue. But folks are still posting helpful content about other approaches. So I'm going to leave this open, but I'm going to mark comments asking for this to be in ripgrep as off topic.

There is no hope. It will never happen. Final decision.

Ripgrep_replace's primary purpose, therefore, is to add "replace in files" to ripgrep via the -R option. See rgr -h for details.

Example usage:

Let's standardize the delimiters in a CSV data log file. Imagine it used commas (,) and tabs (\t) as column delimiters, and you just want to force all groups of commas and tabs to be replaced by simple commas so that you can parse the data easier. Here is how:

# Find all files which end in .log. This allows you to ensure it will only
# affect the files you want it to affect.
rg '' -g '*.log' -l | sort -V

# do a dry-run replacement to replace all delimiters of commas or tabs
# (treating sequential chars as a single delimiter), to replace them with a
# comma
time rg '[,\t]+' -r ',' -g '*.log'

# Now ensure rgr has the same dry-run output as above
time rgr '[,\t]+' -r ',' -g '*.log'

# CAUTION: DO A DRY-RUN ABOVE FIRST! Now use `-R` to actually do the
# replacement. THIS WILL OVERWRITE YOUR FILES! It updates them in-place.
time rgr '[,\t]+' -R ',' -g '*.log'

Note: even better, for the above usage, just use Python and the Pandas data analysis module though. See: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/python/pandas_read_csv_write_to_csv__convert_csv_delimiters.py. Simplified example:

import pandas as pd 

file_in = "data.log"
file_out = "data.csv"

# read a file in which may have any number or mixed combination of commas or tabs as separators
dataframe = pd.read_csv(
    file_in,
    sep=r"[,\t]+", # allow any number of either commas or tabs as the separator
    header=0,  # the first non-blank row is the header
    # NB: using the "python" engine instead of the default "c" engine is much slower, but allows
    # us to have a regex `sep` separator value above, instead of just specifying a single
    # character. When logging files, just use a plain, single comma (`,`) as the separator in
    # order to avoid this problem.
    engine="python",
    skip_blank_lines=True,
)

# write a pure CSV file out, using only a single comma as the separator
dataframe.to_csv(file_out, sep=",", index=False)

# Now you can import it next time like this:
file_in = "data.csv"
dataframe = pd.read_csv(file_in)

ripgrep_fuzzyfinder (rgf)

Here's a script worth trying: Ripgrep Fuzzyfinder: rgf.sh: https://github.com/ElectricRCAircraftGuy/ripgrep_replace/blob/main/rgf.sh.

Installation instructions are in the top of the file.

Just type rgf once installed, and it allows you to interactively, recursively search the whole repository or directory you are in. It uses rg (ripgrep) under the hood as the regex search engine to search for any string, and fzf (fuzzy finder) in this case provides just the interactive interface.

TODO:

  1. Allow live toggling between rg (ripgrep search) and fzf (fuzzy finder search) to choose your search engine: https://github.com/junegunn/fzf/blob/master/ADVANCED.md#switching-between-ripgrep-mode-and-fzf-mode
  2. Allow rgf to search the file names too, which it currently doesn't. As a temporary work-around, you can do this, however, to search file names as well:
    rg search_string | fzf
    # with color and line numbers
    rg --color always -n search_string | fzf --ansi
    
    # OR, to fuzzy search **everything**! 
    # NB: this can potentially take a lot of RAM (many gigabytes), because it 
    # loads **all file contents** in this folder and down into RAM, to be
    # searched.   
    rg --hidden -L '' | fzf
    # With color and line numbers
    rg --hidden -L --color always -n '' | fzf --ansi
  3. Add a preview window to see inside the file live while searching. This requires the bat tool. See this example and screenshot, for instance: https://github.com/junegunn/fzf/blob/master/ADVANCED.md#using-fzf-as-interative-ripgrep-launcher. Be sure to add the installation commands of bat to the top of rgf once you add this dependency.
  4. Add a feature that keeps the last screen of fzf output still on the screen even after killing the program and exiting. This would be like what the -X does in less -RFX filename (see here).

Fuzzy searching for files by filename: manually, and with sublf

To search for files in a directory or repo, do:

find | fzf -m

Then, use the arrow keys to move the selector up and down. Press Tab to toggle a file selection on and off. Press Enter to return from the fzf program and print out the paths to your files you selected. Open these files manually in whatever editor you see fit.

OR, use my sublf program (Sublime Text file fuzzy finder) to find, select, and automatically open all selected files in either Sublime Text or the editor of your choice (see below).

Add a ~/.sublf_config.sh config. file to allow customizing the default options passed to find inside sublf. For the most-correct find -not type syntax to excluded certain files or directories, see my detailed answer here: How do I exclude a directory when using find?

TODO:

  1. Allow setting a default editor as well inside the ~/.sublf_config.sh configuration file. For now, you can simply change the subl "${files_selected_array[@]}" command near the bottom of the sublf.sh script to explicitly use the editor of your choice. Ex: open in vim with vim "${files_selected_array[@]}".

Alternatives to rgr

TODO: see: #1 (comment)

Other, very-useful repos:

  1. My eRCaGuy_dotfiles repo.
  2. My eRCaGuy_hello_world repo.

About

ripgrep_replace, or rgr, is a light-weight wrapper around ripgrep, supporting 100% of ripgrep's features + adding '-R' to enable on-disk find-and-replace; rgf2 is an interactive regex finder and viewer with syntax highlighting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages