1

I am using counsel-git-grep to grep for a string in the current Git repository.

When I do M-x counsel-git-grep it opens following in the mini-buffer.

2    git grep:

3 chars more

Let say if the cursor on top of

def hello_word():
      ^
      |__cursor is here and when I entered `M-x counsel-git-grep`

I want following to open:

2    git grep: hello_word

3 chars more

[Q] How can I pass the word under cursor to the counsel-git-grep search?

2
  • Spacemacs has similar functionality, so maybe this helps you write a lisp function to do what you want? github.com/syl20bnr/spacemacs/blob/… – Croad Langshan Jan 20 at 19:39
  • ...in fact I see many functions in counsel.el also use thing-at-point, so quite likely that code would be useful if you were to implement this yourself. From the code I don't see something that does it already – Croad Langshan Jan 20 at 19:42
1

I've wrapped a couple of function in my dotemacs to achieve this:

;; from https://github.com/abo-abo/swiper/issues/1068
(defun my-ivy-with-thing-at-point (cmd &optional dir)
  "Wrap a call to CMD with setting "
  (let ((ivy-initial-inputs-alist
         (list
          (cons cmd (thing-at-point 'symbol)))))
    (funcall cmd nil dir)))

(use-package counsel
  :config (setq counsel-ag-base-command "ag --vimgrep -a %s"))

(defun my-counsel-ag-from-here (&optional dir)
  "Start ag but from the directory the file is in (otherwise I would
be using git-grep)."
  (interactive "D")
  (my-ivy-with-thing-at-point
   'counsel-ag
   (or dir (file-name-directory (buffer-file-name)))))

(defun my-counsel-git-grep ()
  (interactive)
  (my-ivy-with-thing-at-point
   'counsel-git-grep))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.