swiper
swiper copied to clipboard
counsel-rg from root of project?
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?
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))))
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.
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.
I think it's a good idea to simply search the current directory leave other things for the users.
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 |
...
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...)
perhaps adding a
counsel-git-grep-recurse-submodulesdefcustom 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).