I've read Grep Searching from the manual. The basic that I've found out are that I can use grep command in Emacs. I was wondering if I could use silversearcher-ag as an alternative to grep in M-x grep

I tried to just remove grep --color -nH --null -e and use plain ag command and it does actually work, i.e. it fills up the *grep* buffer. But it's not formatted and thus Emacs can't use that buffer to jump around the matches.

I know about some packages, but I really want to know how I can change the ag command so that the output is compatible for Emacs. Any help would be appreciated.

2 Answers 2


You can customize settings like this:

  (setq grep-command "ag --vimgrep "
        grep-use-null-device nil)

I've tested it with emacs -Q --eval "(setq grep-command \"ag --vimgrep \" grep-use-null-device nil)"

  • Got error: (void-variable \"ag). It works okay without the backslashes though. Maybe you used escaping because you were invoking the command from bash.
    – 3N4N
    May 3, 2019 at 10:10
  • My bad, you're right, string escaping is only needed in bash. Have corrected my answer, thanks.
    – whatacold
    May 3, 2019 at 10:23
  • 1
    Another query of mine is, what is the differences between normal ag and --vimgrep output that Emacs recognizes one and doesn't another?
    – 3N4N
    May 3, 2019 at 10:24
  • 2
    Because grep-mode only recognises lines like A:B:C , where A is file path, B is line number, and C is the matched line, which ag doesn't default to.
    – whatacold
    May 3, 2019 at 10:29

M-x grep RET ag --nogroup PATTERN RET almost works, if you search against only one file, because ag --nogroup PATTERN FILE doesn't print the filename in such case, e.g.,

~/.emacs.d $ ag --nogroup user-init-file init.el
1404:         (not (eq (current-buffer) (get-file-buffer user-init-file)))

you can append a empty file such as /dev/null to force ag to print the file name:

ag --nogroup PATTERN FILE /dev/null

or set grep-use-null-device to t so Emacs will add the empty file for you:

(setq grep-use-null-device t)

Besides, colors doesn't work and you can jump the correct line but not the right column. Now I know more or less why ag.el is created, even I haven't tried it.

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.