Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using ripgrep in Emacs using helm-ag (Spacemacs)

Why

Ripgrep is a fast search tool like grep. It's mostly a drop-in replacement for ag, also know as the Silver Searcher.

helm-ag is a fantastic package for Emacs that allows you to display search results in a buffer. You can also jump to locations of matches. Despite the name, helm-ag works with ripgrep (rg) as well as with ag.

How

  1. Install ripgrep

  2. Make Spacemacs use helm-project-do-ag directly when pressing SPC / without preselecting the symbol under the cursor.

    Type SPC f e d to bring up ~/.spacemacs.

    (defun dotspacemacs/user-config ()
      ;; ....
      (evil-leader/set-key "/" 'spacemacs/helm-project-do-ag))
    
  3. Force ripgrep as search tool

    SPC SPC customize-set-variable

    Select variable helm-ag-base-command

    Set value to rg --vimgrep --no-heading --smart-case

Usage

SPC / should allow you to search in the current project.

You can prefix patterns with options

-G*.cljs -w time - search for the word "time" in all .cljs files

-tclojure time - search for "time" in all .{clj,cljs,cljc} files

uno\ due\ tre - search for the string "uno duo tre"

-C5 foo - search for "foo" but show 5 lines of context before and after the match

(?:^|[^\w-])time(?:[^\w-]|$) - search for lisp-word "time", i.e. search for the full word "time" while considering "-" to be a word characer

@bhb

This comment has been minimized.

Copy link

bhb commented Feb 15, 2018

This is very useful, thanks!

One note - for anyone using helm-projectile, if you switch to ripgrep, you may see the following errors when running helm-projectile-ag (C-c p s s)

.#*: No such file or directory (os error 2)
*.o: No such file or directory (os error 2)
*~: No such file or directory (os error 2)
*.bin: No such file or directory (os error 2)
*.lbin: No such file or directory (os error 2)
*.so: No such file or directory (os error 2)
*.a: No such file or directory (os error 2)
*.ln: No such file or directory (os error 2)
*.blg: No such file or directory (os error 2)
*.bbl: No such file or directory (os error 2)
*.elc: No such file or directory (os error 2)
*.lof: No such file or directory (os error 2)
*.glo: No such file or directory (os error 2)
*.idx: No such file or directory (os error 2)
*.lot: No such file or directory (os error 2)
*.fmt: No such file or directory (os error 2)
*.tfm: No such file or directory (os error 2)
*.class: No such file or directory (os error 2)
*.fas: No such file or directory (os error 2)
*.lib: No such file or directory (os error 2)
*.mem: No such file or directory (os error 2)
*.x86f: No such file or directory (os error 2)
*.sparcf: No such file or directory (os error 2)
*.dfsl: No such file or directory (os error 2)

The issue is that helm-projectile automatically adds options like --ignore *.o to ag. When it tries to send the same options to rg (which doesn't have an --ignore option), you get the following errors. This behavior currently is not configurable.

A workaround is to create a wrapper script that removes these options, named rg-wrapper

#!/usr/bin/env bash
set -euo pipefail

newargs="$(echo "$@" | sed 's/\-\-ignore .* //')"
rg $newargs

Make sure that file is in your path, then change helm-ag-base-command to rg-wrapper --vimgrep --no-heading --smart-case

@well1791

This comment has been minimized.

Copy link

well1791 commented Apr 1, 2018

Hi there, 👋

Thanks for sharing this info (I really appreciate it). So, I was trying to set this up by myself but, as a brand new to spacemacs (and lisp) I'm still struggling hard with all those things in the .spacemacs file (This is my 2nd day with spacemacs/emacs). Hope any of you can give me a hand.

(defun dotspacemacs/user-config ()
  ;; ....
  (evil-leader/set-key "/" 'spacemacs/helm-project-do-ag))

later, when I run SPC SPC customize-set-variable and search for helm-ag-base-command, it doesn't exist (should it exist?).

Could you point me what I'm doing wrong? Thanks in advance for your help! 🙏

Info-Versions

  • Spacemacs: master - Latest commit: 2018-03-04
  • Emacs: 25.3.2
@well1791

This comment has been minimized.

Copy link

well1791 commented Apr 2, 2018

Ok, quick update
Looks like I have been using counsel-grep so my approach was this

  1. put:
    (defun dotspacemacs/user-config ()
      ;; ....
      (evil-leader/set-key "/" 'counsel-grep))
    
  2. run: SPC SPC customize-set-variable
  3. search: counsel-grep-base-command
  4. put: rg --vimgrep --no-heading --smart-case %s

It works, although it's too verbose. (: thanks folks!

@longshi0

This comment has been minimized.

Copy link

longshi0 commented Mar 15, 2019

My solution was simpler, although it probably works because counsel got some upgrade since the last post.

(defun dotspacemacs/user-config ()
  ;; ....
  (evil-leader/set-key "/" 'counsel-rg))

Works like a charm.

@mr337

This comment has been minimized.

Copy link

mr337 commented Aug 22, 2019

Having issue similar to https://gist.github.com/pesterhazy/fabd629fbb89a6cd3d3b92246ff29779#gistcomment-2397946 but not using counsel-grep. When trying set the helm-ag-base-command not finding anything.

Screen Shot 2019-08-22 at 2 06 18 PM

This is how I am confirming I am using helm-ag.
Screen Shot 2019-08-22 at 2 06 46 PM

@mr337

This comment has been minimized.

Copy link

mr337 commented Aug 22, 2019

Following up here is what I did to fix it. Add the following to .spacemacs files in user-config section.

(defun dotspacemacs/user-config ()
  ;; ....
  (evil-leader/set-key "/" 'spacemacs/helm-project-do-ag)
  (setq helm-ag-base-command "rg -S --no-heading")
)

I still don't know why it won't show up when using the customize-set-variable.

@jokem59

This comment has been minimized.

Copy link

jokem59 commented Nov 3, 2019

This looks like a sleeker approach then my current deadgrep approach. Would you happen to have instructions on how to set this up from a non-spacemacs configuration?

@pesterhazy

This comment has been minimized.

Copy link
Owner Author

pesterhazy commented Nov 3, 2019

@jokem59, at this point I'm not using the hack anymore. I've also switched away from Spacemacs because problems like this were too hard to fix for me in Spacemacs.

helm-ag (or helm-projectile-ag, I forget which) has support for rg in its own right. There's also helm-rg, confusingly :-)

@jokem59

This comment has been minimized.

Copy link

jokem59 commented Nov 5, 2019

Thanks @pesterhazy! Out of curiosity, how do use rg with emacs now?

@pesterhazy

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.