swiper icon indicating copy to clipboard operation
swiper copied to clipboard

counsel-rg from root of project?

Open parbo opened this issue 8 years ago • 7 comments

I recently switched form counsel-git-grep to counsel-rg to be able to search into submodules. Now I have the opposite problem, when I'm in some folder, the search is done in just that folder. Is there a simple way make counsel-rg always start from the root of the project?

parbo avatar Dec 22 '16 15:12 parbo

You can use the prefix C-u to choose the directory, and the current one is the default, and there you simply can pres backspace until you reach the root.

By default I use hydra and to choose always the directory I have something like this:

("r" (let ((current-prefix-arg "-."))
       (call-interactively 'counsel-rg))))

joedicastro avatar Dec 22 '16 15:12 joedicastro

I have to think about a good way of going about this. It would be easy to check if the current default-directory is Git-controlled and then automatically use the Git root. But what to do if you specifically want to search in the current subdirectory of the project and not the whole project?

Additionally, if default-directory belongs to the submodule, the root of that would be used, instead of the full project root. It's a bit tricky to tell if the submodule belongs to a bigger project.

You can look at counsel-git-grep-projects-alist. It's an attempt to have a custom git-grep command per-project.

abo-abo avatar Dec 24 '16 09:12 abo-abo

Here's what I use (compatible with Git, Mercurial and Subversion):

(require 'dash)

(defun jmb/project-root ()
  "Locate the project root (i.e. where the VCS folder is located)
for the currend file."
  (let ((n nil))
    (--each-while '(".hg/" ".svn/" ".git/") (not n)
      (setq n (locate-dominating-file default-directory it)))
    n))

(defun jmb/counsel-rg-project-root (&optional extra-args)
  (interactive
   (list (when current-prefix-arg
           (read-string (concat
                         (car (split-string counsel-rg-base-command))
                         " with arguments: ")))))
  (counsel-rg nil (jmb/project-root) extra-args))

Then bind jmb/counsel-rg-project-root to whatever key you prefer.

jeberger avatar Oct 12 '17 07:10 jeberger

I think it's a good idea to simply search the current directory leave other things for the users.

AbstProcDo avatar Sep 03 '20 11:09 AbstProcDo

I have same mindset with @AbstProcDo

I want to use counsel functions for current directory scope and using counsel-projectile for project scope. So I'm forking and customize counsel package for myself

| Current directory | Project                    |
|-------------------+----------------------------|
| counsel-ag        | counsel-projectile-ag      |
| counsel-jump-file | counsel-project-find-file  |
| counsel-rg        | counsel-projectile-rg      |
| counsel-compile   | counsel-projectile-compile |
...

giaptx avatar Sep 09 '20 04:09 giaptx

regarding the head post: git grep has a --recurse-submodules flag. perhaps adding a counsel-git-grep-recurse-submodules defcustom would be a good idea? (an even (way) better idea would be, of course, a grep.recurse_submodules git config...)

cmm avatar May 06 '21 14:05 cmm

perhaps adding a counsel-git-grep-recurse-submodules defcustom would be a good idea?

Why not add it to your counsel-git-grep-cmd-default or counsel-git-grep-projects-alist instead?

Note that you can also change the counsel-git-grep command on-the-fly via C-c C-m (counsel-git-grep-switch-cmd).

basil-conto avatar May 06 '21 14:05 basil-conto