I wrote a little search function a long time ago, binded to ctrl-f. It has been working perfectly until I tried Emacs 24.4.

The purpose of the function is to highlight (region) the found word. Furthermore I have other functions that can select/look backward/forward for the same text. This set of dunctions are pretty useful and I am use to it.

The problem: Any key I hit (right arrow for example) after searching with this function with v24.3.1 will automatically disable the region highlighting. With v24.4 and above the region is still active and I need to disable it manually with ctrl-space before doing anything which is annoying.

Questions: Any idea on what change concerning this behavior between those versions? Is there a work around to have the old behavior?



(defun mp-search (string)
"Search using search-forward instead of i-search, this search
cannot search whole word only"
(interactive "sSearch for : ")

; backup point
(setq mp_backup_point (point))
(set-mark nil)

; search forward the selection if there is a selection
(if (search-forward string nil t)

    (progn (set-mark (point))
           (backward-char (length string))

    (progn (message "Return at the file's beginning...")
           (goto-char 0)
           (if (search-forward string nil t)
           (progn (set-mark (point))
                  (backward-char (length string))
           (progn (goto-char mp_backup_point)
                  (message "Nothing was found...")))))

Binding: (global-set-key [(ctrl f)] 'mp-search)

Picture: enter image description here

  • The region stays activated for me also with Emacs 24.3.
    – npostavs
    Commented Oct 22, 2016 at 23:01
  • I am seeing somewhat similar behaviour in emacs 24.5.1, but it's intermittent, and therefore very hard to diagnose; part of it is indeed a marked region not being unmarked by the next keypress (desired behaviour), but staying marked and being extended (undesired) by cursor or mouse movement. In my case I had the vague impression --without thorough investigation-- that the odd behaviour occured when, when marking a region with the mouse, the mouse (with button1 pressed down to mark the region) ended up in or past the fringe... But this is al preliminary and I need to look further into that
    – Roadowl
    Commented Oct 24, 2016 at 13:27

1 Answer 1


Works for me, out of the box - emacs -Q (no init file). Works in both old and new Emacs versions, including Emacs 24.4.

Does it not work for you, if you start Emacs using emacs -Q? If that works then recursively bisect your init file to find the culprit. You can use M-x comment-region to comment out the active region (with a prefix arg it uncomments the region).

(Typically, if the region becomes inactive after your command, you just need to add (setq deactivate-mark nil) to the end of the command code. But your code works as is, as far as I can tell.

In (global-set-key [(ctrl f)] 'mp-search), (ctrl f) is currently undocumented syntax. [(control f)] is documented, as is this, which lets you reuse the external syntax (e.g. of C-h k):

(global-set-key (kbd "C-f") 'mp-search)
  • I'd phrase the commentary on key notation differently. [(ctrl f)] works fine and there's code out there using it, however it is considered deprecated as this is "lucid" style key notation. Better use the "kbd" notation unless it's inable of expressing the keys in question.
    – wasamasa
    Commented Sep 22, 2016 at 16:55
  • @wasamasa: Edited; thx.
    – Drew
    Commented Sep 22, 2016 at 22:34
  • Drew, I have tried using emacs -Q. I have copied the function above and bound it to "ctrl-f". I have also added (transient-mark-mode t). So I can see the selected text after a search.
    – zOmBy4245
    Commented Oct 22, 2016 at 21:06
  • I have tried the same thing in version 24.5.1 and version 24.3.1. Let say I search for "point", "point" is found and highlighted in both versions. However, if I press the down arrow, in version 24.5.1 it will highlight the next line along with "point". In version 24.3.1, point will stop being highlighted and cursor will move to next line and this is the behaviour I am expected. I guess the question remains: what changed in transient-mark-mode? regards
    – zOmBy4245
    Commented Oct 22, 2016 at 21:15
  • 1
    I think I misunderstood what you want. I thought you wanted to keep the region active. Cursor movement always extends the region, and that is apparently what you do not want - you want cursor movement to deactivate the region. FWIW, extending the region is what happens in earlier Emacs versions also - ever since transient-mark-mode has existed. I don't see how you saw deactivation in Emacs 24.3 - that's not what I see (with emacs -Q with 24.3, 23.4, 22.3,...).
    – Drew
    Commented Oct 22, 2016 at 23:24

Your Answer

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

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