>> Become my 1st Sponsor on GitHub <<
(click to expand)
TODO: [#1]: add gif demo image here
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
Both rgr
and rgf
are done and fully implemented and work well.
For now, just see the top of each file for instructions in the comments.
TODO:
- 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, 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.
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)
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:
- Allow live toggling between
rg
(ripgrep search) andfzf
(fuzzy finder search) to choose your search engine: https://github.com/junegunn/fzf/blob/master/ADVANCED.md#switching-between-ripgrep-mode-and-fzf-mode - 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
- 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 ofbat
to the top ofrgf
once you add this dependency. - 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 inless -RFX filename
(see here).
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:
- Allow setting a default editor as well inside the
~/.sublf_config.sh
configuration file. For now, you can simply change thesubl "${files_selected_array[@]}"
command near the bottom of thesublf.sh
script to explicitly use the editor of your choice. Ex: open in vim withvim "${files_selected_array[@]}"
.
TODO: see: #1 (comment)
- My eRCaGuy_dotfiles repo.
- My eRCaGuy_hello_world repo.