0

I am using trying to use a command called counsel-rg. This is the associated describe-command:

counsel-rg is an autoloaded interactive compiled Lisp function in
‘counsel.el’.

(counsel-rg &optional INITIAL-INPUT INITIAL-DIRECTORY EXTRA-RG-ARGS
RG-PROMPT)

Grep for a string in the current directory using ‘rg’.
INITIAL-INPUT can be given as the initial minibuffer input.
INITIAL-DIRECTORY, if non-nil, is used as the root directory for search.
EXTRA-RG-ARGS string, if non-nil, is appended to ‘counsel-rg-base-command’.
RG-PROMPT, if non-nil, is passed as ‘ivy-read’ prompt argument.

Example input with inclusion and exclusion file patterns:
    require i -- -g*.el

Although the definition does not mention it, for me I use this command as an interface of ripgrep.

It is a command that helps me find stuff on the code base.

There is a weird behavior happening, though.

If I type to search "Book, I can see:

enter image description here

As you see, one of the results retrieved is "Book JE". Hence, I tried to search for it:

enter image description here

This feels weird to me since the output does not show the result that I was expecting to see. In addition, as you see, the mini-buffer indicates that an "error code 2" happened.

  1. Why is this happening? Why I can't index an expression that was actually previously found?

  2. What is an "error code 2"?

Obs.: If this is relevant, this is my init file in Emacs. And I am using Macbook Air M1 Monterey 12.6.

UPDATE

I tried the approach suggested by db48x, but it did not work out:

enter image description here

enter image description here

Observation: Follow another suggestion that appeared on the comment section, I also tried writing (trace-function 'call-process) on the scratch buffer, putting the cursor after the last parenthesis, and executing the command eval-last-sexp. It returns nil.

Pedro Delfino
  • 1,369
  • 3
  • 13
  • 1
    I meant that you should try searching for `"Book\ JE"`. What does that give you? – db48x Nov 29 '22 at 22:07
  • Thanks again for the help, @db48x. Unfortunately, the problem persists. I have updated the question with the suggested approach. – Pedro Delfino Nov 30 '22 at 15:42
  • 1
    Well, it was worth a shot. You should figure out a way to see what arguments are given to rg. Try running `(trace-function 'call-process)` in the *scratch* buffer. – db48x Nov 30 '22 at 17:06
  • Do you mean writing ` (trace-function 'call-process) ` in the scratch buffer, putting the cursor after the last parenthesis, and executing the command `eval-last-sexp`? I did that and it returns `nil`. Thanks for another iteration! – Pedro Delfino Nov 30 '22 at 17:14
  • 1
    Yes. Now try your search again and a trace buffer will appear that shows you when the `call-process` function was called, and with what arguments. – db48x Nov 30 '22 at 17:42

3 Answers3

1

If you look in the man page for rg, you will find that the exit status will be 2 when there was an error. This can be a syntax error in your search regex, or an error while reading some file (a disk error, permissions, etc).

You should debug this problem further by figuring out what arguments this passes to rg. I wasn’t able to get counsel-rg to work on my first try, so I cannot really help you much there.

However, my guess would be that the space is breaking your search into two arguments, one of which is then being treated as a file name by rg. Since that file doesn’t exist, rg exits with status 2. Try escaping it with a \.

db48x
  • 15,741
  • 1
  • 19
  • 23
1

This problem is now solved with the following addition to my init file:

(setq ivy-re-builders-alist '((counsel-rg . ivy--regex-plus)
                              (t . ivy-prescient-re-builder)))

This issue on GitHub helped me find the solution.

Pedro Delfino
  • 1,369
  • 3
  • 13
1

Please try this: use [ ] for a single space

rg: "Book[ ]JE"

rg book je

JimMoen
  • 11
  • 1