Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add /rg command (ripgrep for /add files) #540

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aleclarson
Copy link

NOTE: Using this command requires ripgrep installed on your machine.

How it works

It calls rg through a subprocess with the -l flag to return a list of filenames. These filenames are then fed into the /add command. Anything you pass after /rg is forwarded to the ripgrep tool.

…files.

# Aider chat conversation:

USER: I ran this command:

 rg '/run <command>' -B 8 -A 5 -H -n -- README.md

And got this output:

README.md-121-## In-chat commands
README.md-122-
README.md-123-Aider supports commands from within the chat, which all start with `/`. Here are some of the most useful in-chat commands:
README.md-124-
README.md-125-* `/add <file>`: Add matching files to the chat session.
README.md-126-* `/drop <file>`: Remove matching files from the chat session.
README.md-127-* `/undo`: Undo the last git commit if it was done by aider.
README.md-128-* `/diff`: Display the diff of the last aider commit.
README.md:129:* `/run <command>`: Run a shell command and optionally add the output to the chat.
README.md-130-* `/voice`: Speak to aider to [request code changes with your voice](https://aider.chat/docs/voice.html).
README.md-131-* `/help`: Show help about all commands.
README.md-132-
README.md-133-See the [full command docs](https://aider.chat/docs/commands.html) for more information.
README.md-134-

ASSISTANT: It seems you've provided the output of a `ripgrep` command that was run on a `README.md` file, which is not currently in the list of *read-write* files in this session. If you would like to make changes to the `README.md` file based on this output, please add it to the chat so I can assist you with making the necessary edits.

USER: Add `/rg` documentation to the README.md and docs/commands.md files

ASSISTANT: Here are the diffs to add the `/rg` documentation to the `README.md` and `docs/commands.md` files:

```diff
--- README.md
+++ README.md
@@ ... @@
 * `/undo`: Undo the last git commit if it was done by aider.
 * `/diff`: Display the diff of the last aider commit.
+* `/rg <pattern>`: Run ripgrep with the given pattern and add the matching files to the chat.
 * `/run <command>`: Run a shell command and optionally add the output to the chat.
 * `/voice`: Speak to aider to [request code changes with your voice](https://aider.chat/docs/voice.html).
 * `/help`: Show help about all commands.
@@ ... @@
```

```diff
--- docs/commands.md
+++ docs/commands.md
@@ ... @@
 - `/diff`: Display the diff of the last aider commit
 - `/commit <message>`: Commit edits to the repo made outside the chat (commit message optional)
+- `/rg <pattern>`: Run ripgrep with the given pattern and add the matching files to the chat
 - `/git <command>`: Run a git command
@@ ... @@
```

These diffs will add the new `/rg` command documentation to the respective sections in both files.
@aleclarson aleclarson changed the title feat: add /rg command feat: add /rg command (ripgrep for /add files) Apr 11, 2024
@avi-cenna
Copy link

This is nice. Would it be possible to make the /rg command pop up an interactive window for selecting a file? Otherwise, it seems like it would be easy to add too many files by accident.

Also, one thing you might want to check out in the interim is fzf shell integration. This way, you can type aider **<tab> and it will pop open an interactive fzf window where you can search for a file using fuzzy matching. I have found this quite effective, and much easier/faster than using the /add command from within aider.

@@ -495,6 +495,34 @@ def cmd_run(self, args, add_on_nonzero_exit=False):
)
return msg

# Add cmd_rg for calling ripgrep with -l and the user-provided arguments. Then call cmd_add with the returned file names.
def cmd_rg(self, args):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this library have unit/integration tests for commands? If not, I would paste a screenshot of this working locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants