2024-09-13    Share on: Twitter | Facebook | HackerNews | Reddit

Notes on using ripgrep for projects with python, jupyter (ipynb) notebooks and markdown files

Notes on using ripgrep (rg) mainly for use cases as a Python developer working with .py, .ipynb, and .md files:

Basic search

   rg "pattern" path/to/search

Search only Python files

   rg --type py "pattern"

Search Jupyter notebooks

   rg --type-add 'ipynb:*.ipynb' --type ipynb "pattern"

Search Markdown files

   rg --type md "pattern"

Case-insensitive search

   rg -i "pattern"

Search for whole words

   rg -w "word"

Show context around matches

   rg -C 3 "pattern"  # 3 lines before and after

Search for multiple patterns

   rg "pattern1|pattern2|pattern3"

Exclude specific directories:

   rg "pattern" --glob '!{venv,__pycache__}'

Search for Python functions

    rg "def \w+\("
 ```

## Search for Markdown headers
```sh
    rg "^#{1,6} .+"
 ```

## Count matches
```sh
    rg -c "pattern"
 ```

## Display only the filenames where a pattern appears

To display only the filenames where a pattern appears, you can use the `-l` or `--files-with-matches` option in ripgrep. Here's how to do it:

```sh
rg -l "pattern"

Search for files with a specific name pattern

rg --files -g "*.py"

Use AND logic for multiple patterns:

rg "pattern1" | rg "pattern2"