I use emacs for viewing and editing code and other text files. I wanted to know if there is a way to search forward or backward for text which is marked in the current buffer. S
Other answers describe how to search for copied text, or how to search for the word at point. But none of them actually describe how to "search with the marked text."
Adding the following hook will make it so that the currently-selected text is the text used for an isearch:
(defun jrh-isearch-with-region ()
"Use region as the isearch text."
(when mark-active
(let ((region (funcall region-extract-function nil)))
(deactivate-mark)
(isearch-push-state)
(isearch-yank-string region))))
(add-hook 'isearch-mode-hook #'jrh-isearch-with-region)
Tip: This pairs nicely with expand-region.