Silver surfer (
ag) Linux tool is the fastest way to find where a word or phrase appears in the files on a directory.
I've been using
ag in two ways from inside Emacs.
ag-modepackage, that asks me to input a search term and the root directory where I want it to search and then it performs the search.
projectile, that allows me to search on a whole project without the extra step of inserting the root dir.
Both of them are topic for another article. I had bound the
H-g keybinding to the
ag command and
C-c p s s to
This is nice, but I wanted to assign a single keybinding and let Emacs choose the best option according to the situation at hand. But to do that, I'd need to find a way to invoke an interactive command from within an interactive command. Here's the solution I found: use the
It works like this:
So here's the command I wrote in order to accomplish my goal:
It first finds out if the current buffer is visiting a file under a project recognized by
projectile. We do so by calling
Then, if we are, we call
(call-interactively #'projectile-ag) to search using projectile.
If we're not in a project, we just invoke
(defun fdx/ag-or-projectile-ag (&optional a b) (interactive) (if (projectile-project-p) (call-interactively #'projectile-ag) (call-interactively #'ag)))
Now I have
fdx/ag-or-projectile-ag bound to
H-g and I moved the plain
ag call to
H-G for special cases when I want my filter to be more specific even inside a project.
Use it at will.