Emacs Lisp code: Easily Search for Text You've Selected |
Tuesday, December 1st, 2015 13:42:54 GMT |
Emacs Lisp |
(Updated at 9:27 AM EST.) I finally ran across a nice, slick way to be able to immediately search for the text I've selected in Emacs.
Most good text editors have something like that feature by default - but not Emacs, which was another maddening nuisance for me until today.
I found two solutions in this StackOverflow page. I prefer this solution, reproduced below.
I prefer the above because it even makes backward searching with C-r work right, not just forward searching with C-s, and acts normal when you search without selecting any text.
Prior to finding the above solutions in this StackOverflow post, the closest thing I had to a solution was the excellent highlight-symbol.el add-on.
Th highlight-symbol.el add-on is still very useful to me. The way I have it set up, I can press a single key - F4 - to search for whatever word (or function name, or whatever - even if it contains dashes and/or underscores) the point is resting inside. I can press Ctrl-F3 to easily replace that text. And I can press F3 to highlight that text throughout a buffer.
But, the highlight-symbol.el add-on doesn't provide a way to search for any arbitrary selected text, since that add-on doesn't require you to select text, nor pay any attention if you do select some text.
I'm still looking for a way to easily replace selected text, and will update this post if/when I find a way.
The original solution I tried only fixes forward searching, and erroneously selects blocks of text if you try to search without selecting any text. The second "search-selection" function from this StackOverflow post, reproduced below, along with the commands I added to my .emacs file to change the keychord C-s to make it use the "search-selection" function instead of "isearch-forward":