Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counsel-rg from root of project? #836

Open
parbo opened this issue Dec 22, 2016 · 7 comments
Open

counsel-rg from root of project? #836

parbo opened this issue Dec 22, 2016 · 7 comments

Comments

@parbo
Copy link

parbo commented Dec 22, 2016

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?

@joedicastro
Copy link
Contributor

joedicastro commented Dec 22, 2016

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))))

@abo-abo
Copy link
Owner

abo-abo commented Dec 24, 2016

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.

@jeberger
Copy link
Contributor

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.

@AbstProcDo
Copy link

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

@giaptx
Copy link

giaptx commented Sep 9, 2020

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 |
...

@cmm
Copy link

cmm commented May 6, 2021

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...)

@basil-conto
Copy link
Collaborator

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).

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

No branches or pull requests

8 participants