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


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.


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.

  • 1
    I meant that you should try searching for "Book\ JE". What does that give you?
    – db48x
    Nov 29, 2022 at 22:07
  • Thanks again for the help, @db48x. Unfortunately, the problem persists. I have updated the question with the suggested approach. Nov 30, 2022 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, 2022 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! Nov 30, 2022 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, 2022 at 17:42

3 Answers 3


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 \.

  • Thanks for the help. I tried your suggestion (if my interpretation is correct), but it did not work out. I documented my attempt updating my question. Nov 29, 2022 at 21:13
  • I believe I solved the problem. See my answer. Dec 2, 2022 at 0:55

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.


Please try this: use [ ] for a single space

rg: "Book[ ]JE"

rg book je

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.