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
I am using the following which does not have the problem of having to type more then one successive C-s to find later occurences:
(defun search-selection (beg end)
"search for selected text"
(interactive "r")
(kill-ring-save beg end)
(isearch-mode t nil nil nil)
(isearch-yank-pop)
)
(define-key global-map (kbd "<C-f3>") 'search-selection)
The disadvantage of the previous code is that the selected text is copied to the stretch. The following code does not have this problem:
(defun search-selection (beg end)
"search for selected text"
(interactive "r")
(let (
(selection (buffer-substring-no-properties beg end))
)
(deactivate-mark)
(isearch-mode t nil nil nil)
(isearch-yank-string selection)
)
)
(define-key global-map (kbd "<C-f3>") 'search-selection)
The answers above (including the accepted one) are too cumbersome IMHO. I found the following info and like it well better:
“Ctrl+s Ctrl+w”. This will search the current word, but you must move your cursor to the beginning of the word first.
http://xah-forum.blogspot.com/2009/08/search-word-under-cursor-in-emacs.html