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

Is it possible to search for the thing under the cursor with just one key binding? #1068

Closed
zzhjerry opened this issue Jun 13, 2017 · 10 comments

Comments

@zzhjerry
Copy link

I know C-s C-w or C-s M-n could do it. But I wonder if this can be done with just 1 key binding.

@abo-abo
Copy link
Owner

abo-abo commented Jun 14, 2017

There's no option to do this. But it's easy to write some custom code. I can give an example if you need it.

@haskelcurry
Copy link

@abo-abo Yes, could you please give an example?
I would like to implement the SPC-* functionality from the Spacemacs, i.e. when pressing SPC-* on any word, it performs a project-wide search.

I mapped the SPC-* to

(progn
    (counsel-projectile-ag)
    (ivy-next-history-element)))

But it doesn't work. I'm new to elisp. Please help.
P.S. Thank you for the great Ivy!

@abo-abo
Copy link
Owner

abo-abo commented Jul 26, 2017

Here's a code that you could use:

(defun ivy-with-thing-at-point (cmd)
  (let ((ivy-initial-inputs-alist
         (list
          (cons cmd (thing-at-point 'symbol)))))
    (funcall cmd)))

;; Example 1
(defun counsel-ag-thing-at-point ()
  (interactive)
  (ivy-with-thing-at-point 'counsel-ag))

;; Example 2
(defun swiper-thing-at-point ()
  (interactive)
  (ivy-with-thing-at-point 'swiper))

@haskelcurry
Copy link

@abo-abo tried it, doesn't work. The thing is that, unlike Swiper, the counsel-ag has no :inital-value argument (as well as counsel-projectile-ag). What to do?

@abo-abo
Copy link
Owner

abo-abo commented Jul 26, 2017

@mtuzinskiy You should update the package from Git, or wait a few hours and update from MELPA.

@wykurz
Copy link

wykurz commented Nov 29, 2020

This was working great for me until recently. Now running swiper-thing-at-point has an unfortunate side-effect of going to the last element found (rather than sticking to the one nearest to the cursor, as C-s M-n would.

@abo-abo do you have any suggestions what may be going on?

Thanks!

@basil-conto
Copy link
Collaborator

@wykurz If this is something you can reliably reproduce, please submit a bug report with a reproduction recipe. TIA.

@wykurz
Copy link

wykurz commented Dec 1, 2020

Yes, it's 100% reproducible. Created this: #2737

@wykurz
Copy link

wykurz commented Dec 3, 2020

For others trying to use this - swiper-thing-at-point is now defined in swiper.el itself. So to use it just create a key binding like:

(global-set-key (kbd "M-s") 'swiper-thing-at-point)

@stsquad
Copy link
Contributor

stsquad commented Mar 16, 2021

Here's a code that you could use:

(defun ivy-with-thing-at-point (cmd)
  (let ((ivy-initial-inputs-alist
         (list
          (cons cmd (thing-at-point 'symbol)))))
    (funcall cmd)))

;; Example 1
(defun counsel-ag-thing-at-point ()
  (interactive)
  (ivy-with-thing-at-point 'counsel-ag))

;; Example 2
(defun swiper-thing-at-point ()
  (interactive)
  (ivy-with-thing-at-point 'swiper))

Just for the historical record I found I needed to strip properties from the thing at point lest I get weird behavior searching for something from a "read-only" section of my cicre IRC buffer. So currently I have:

(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 (substring-no-properties (thing-at-point 'symbol))))))
    (funcall cmd nil dir)))

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

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

No branches or pull requests

6 participants