Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active September 20, 2023 13:59
  • Star 51 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
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

@pesterhazy
Copy link
Author

@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
Copy link

jokem59 commented Nov 5, 2019

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

@pesterhazy
Copy link
Author

@Antiarchitect
Copy link

Antiarchitect commented Nov 18, 2019

Gentlemen, how can I reduce my search with SPC-/ to the current directory in Treemacs only - no the entire project?

@sbseltzer
Copy link

sbseltzer commented Dec 10, 2019

Does anyone know if there's a way to specify the rg [PATH ...] segment? I work on a lot of projects where directories outside the current project are useful to search in addition to the project and have not been able to find anything.

@Antiarchitect
Copy link

Gentlemen, how can I reduce my search with SPC-/ to the current directory in Treemacs only - no the entire project?

SPC-s-f

@demalworkshop
Copy link

demalworkshop commented Feb 19, 2021

If I restart spacemacs, I have to re-run step 3. I tried putting (setq helm-ag-base-command "rg --vimgrep --no-heading --smart-case") in my dotspacemacs/user-config() in .spacemacs, but that does not help. What can I do to set the variable helm-ag-base-command to the desired value automatically upon restarting spacemacs?

@AtomicNess123
Copy link

Why would you replace ag with rg?

@fperies
Copy link

fperies commented Jul 5, 2023

Hello,
Is there a trick from Helm prompt to use option -g/--glob so to filtered out some globs ?
Something is misinterpreted when I try this:
2023-07-05 14_06_34-sq50-05_78 (frperies) - TigerVNC
and with pattern:
2023-07-05 14_08_05-sq50-05_78 (frperies) - TigerVNC
Value of my variable helm-do-ag--commands is:
(("rg" "--smart-case" "--no-heading" "--color=never" "--line-number" "--max-columns=512" "--ignore=.#" "--ignore=.o" ...some ignore entries skipped... "--ignore={arch}"))

Thank you for your answers!

@apmiller108
Copy link

In order to use flags, appending = to the flag works for me. For example, to search for a term only in *.js files:

foo -g=*.js

or

foo --type=js

@fperies
Copy link

fperies commented Sep 20, 2023

@apmiller108 , thank you for your reply. This is good to know!

Also, regarding my previous question, the trick is that simple quotes were useless: foo -g!ut/ will exclude all paths with ut/ and does the job !
But your proposal works as well: -g=!ut/.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment