How can I define a command that searches the web for the evil visually selected text using google or some other search engine? counsel-search already does this if you insert a string, but how can I define a command to do this using the selected text? (I'm using Evil.) enter image description here

  • Your text was ambiguous. Instead of searching the selected text, in Emacs, I think you're asking about searching the web for the selected text. If I misunderstand, please edit to make clear what you're asking. Thx.
    – Drew
    Feb 17, 2022 at 22:00
  • This is not a command, but thanks to EWW, to search for a selected text on the web, you can do M-s M-w & Jul 2, 2022 at 10:07

1 Answer 1


Try this function:

(defun counsel-search-selected (start end)
  (interactive "r")
  (let ((region (buffer-substring-no-properties start end)))
    (counsel-search-action region)))

However, instead of search visual selected text. I prefer use thing-at-point: M-x counsel-search and press M-n.

  • Wow, that's neat, I have no idea what buffer-substring-no-properties is, but it does the job. Also haven't know about thing-at-point, how would you implement the command which takes automatically the first suggested string of thing-at-point and counsel search it on the web in one click?
    – Rareform
    Feb 18, 2022 at 7:28
  • buffer-substring-no-properties remove text properties (like color etc.). For automatically insert thing-at-point you can refer to with-emacs.com/posts/ui-hacks/execute-commands-like-marty-mcfly and github.com/seagle0128/.emacs.d/blob/master/lisp/… Feb 18, 2022 at 8:48
  • @TianshuWang: you should look up the r specification for the interactive form.
    – NickD
    Feb 18, 2022 at 20:53
  • @NickD Thank you, I have updated this function. Feb 19, 2022 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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