9

When searching for a string with C-s, I want to be able to hit some key to exit search mode but still maintain the selection of the match. That way I can immediately start typing and replace the last match.

For example, say I have the following document:

The quick brown fox jumps over the lazy dog.

If I search for "lazy", emacs will select that word like so (square brackets are selection, pipe is cursor):

The quick brown fox jumps over the [lazy|] dog.

Now, if I press return, emacs will maintain my cursor position, but it will deselect "lazy":

The quick brown fox jumps over the lazy| dog.

Instead of this, I want a shortcut that will exit search mode but maintain the current match selection, like in the second state above.

  • Isn't M-x query-replace what you're looking for? – Nsukami _ Mar 8 '17 at 15:00
  • 1
    @Nsukami_ I do not always know what I want my query to be or even what I want to replace it with before I search. It's much easier to just hit C-s and type what I think I want to search for, then change the query w/ backspace to zero in on the thing I want to change. Then I play around with what exactly I want to replace it with. M-% is too "formal." – Ben Mar 8 '17 at 15:04
  • The solution comes in handy for me, like when I search for a word to kill it and then yank it to a different place. query-replace doesn’t solve this. – GergelyPolonkai Mar 14 '17 at 22:44
7

Here's one approach:

(defun isearch-exit-mark-match ()
  "Exit isearch and mark the current match."
  (interactive)
  (isearch-exit)
  (push-mark isearch-other-end)
  (activate-mark))

(define-key isearch-mode-map (kbd "<C-return>") #'isearch-exit-mark-match)

This binds a different key (C-return) to exit the current isearch and also leave the last match selected. This will work with isearch-forward-regexp as well, which is handy.

  • 1
    I get "Symbol's function definition is void: bind-key." Where is bind-key coming from? – Ben Mar 10 '17 at 14:02
  • 1
    Sorry, bind-key comes from another library. I've changed the answer to use the built-in define-key function instead. – glucas Mar 10 '17 at 14:20
4

Library Isearch+ lets you do that easily, and even toggle it on/off on the fly.

  • Non-nil option isearchp-set-region-flag automatically sets the region (selection) around the last search target when you quit isearch.

  • Command isearchp-toggle-set-region, bound to M-s M-SPC during isearch toggles option isearchp-set-region-flag.

  • Command set-region-around-search-target manually sets the region around the last search target. (So even if the option is not set, so you don't get automatic selection, you can do it manually.)


Library Replace+ lets you do the same thing for query-replace etc. It puts the region around (i.e., selects) the last replacement occurrence. It uses the same option, isearchp-set-region-flag, to control this, and command set-region-around-search-target has the same effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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