4

How would I be able to mark a specific occurrence of a searched phrase? In other words, I can use incremental search to search for a phrase, but would like to be able to mark that phrase once I found it. I don't believe this is already implemented into emacs but would be simple to do if I was able to retrieve the location of the searched word in elisp.

Below, you can see that highlighted is the result of searching for the word "buffer." In pink, is the occurrence the cursor is currently on. I would like to edit this word directly. Currently, this can only be done by stopping search, which causes the cursor to position itself at the beginning of "buffer", and then I would need to use ctrl-space to set mark and move forward a word to mark "buffer". This is relatively simple to do for a word, but tedious if I searched for a phrase (which needs to be edited) such as "is for notes" in this example.

emacs_incrementalsearch

4
  • Take a look at the options on the EmacsWiki node on "Highlight Temporarily".
    – Dan
    Mar 25, 2015 at 23:15
  • Highlighting basically just highlights the occurrences of the phrase (so that I may locate them later). This does not allow me to directly edit the phrase I searched for (after highlighting, I would still have to navigate to one of the highlighted regions to actually edit it).
    – gkumar7
    Mar 25, 2015 at 23:38
  • If you hit RET during search, the search will quit with the point at that last location. Then proceed with highlighting as you would do. Mar 25, 2015 at 23:41
  • @kaushalmodi Yes, I mentioned this in the second paragraph. The issue I am describing is easier seen when searching for a phrase. Search already highlights the phrase for you (so there should not be a reason to have to mark it again after RET).
    – gkumar7
    Mar 25, 2015 at 23:47

2 Answers 2

2

Not sure exactly what you're looking for. If what you want to do is to set the mark at the beginning of the final search hit when you exit Isearch with point at the end of that hit, then that means that you want to have Isearch select that search hit when you exit.

If that's what you want then you can get that using library Isearch+ and customizing option isearchp-set-region-flag to non-nil. Or if you want to select the search hit when you exit sometimes, but only on demand, then you can use M-x isearchp-set-region-around-search-target after you exit.

2

If the use-case is to edit the text you've searched for, then maybe you don't need to worry about marking it at all.

M-% (whilst isearching) starts an interactive search and replace for the search string.

If it's a straight replacement, just enter the new text.

If you want to edit the original text, use the minibuffer history to populate the search string before editing it for the replacement.

The interactive help (? once you've invoked the command) tells you the rest.

1
  • This is a good way if the use-case is only to edit the text. If I need to use the phrase for some other purpose (for example looking for its reference in a separate file, possibly using anaconda-mode for python references) I would need to mark it.
    – gkumar7
    Mar 26, 2015 at 19:07

Your Answer

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

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