Question

I am fairly new to emacs, and I'm having a problem with the minibuffer remaining active when I don't think it should be. I have the following mapping for 'other-window:

 (global-set-key (kbd "M-s M-s") 'other-window)

I know that this will cycle through the minibuffer if it is active, and I like that behavior. The problem is that my minibuffer keeps getting stuck in the following state:

Next element matching (regexp):

It gets in there sometimes when I am not even trying to do a regex search or even a search at all. When I hit C-g to kill it, I get the Quit message, but the minibuffer stays active and goes right back to

Next element matching (regexp): 

Also, when it is in this state, I cannot use M-s M-s to get to the next window in my frame. C-x o still seems to work though.

I also seem to be able to run other minibuffer commnands (such as file search) just fine, even when I'm stuck like this.

Is there a way that I can do one of the following:

  1. Kill the minibuffer when in that mode.
  2. Set my custom 'other-window M-s M-s function to get out of the minibuffer and on to the next window?

Either solution would be fine, though the 1st might be better since getting the minibuffer stuck may have other unexpected consequences.

Was it helpful?

Solution

Just do this:

(define-key minibuffer-local-map "\M-s" nil)

The problem is that M-s is locally bound in the minibuffer (in minibuffer-local-must-match-map and other minibuffer keymaps) to next-matching-history-element, which gives you that prompt and lets you search the history.

What you need to do is unbind M-s in each of the minibuffer keymaps: i.e., bind it to nil. Some of those maps inherit from others; minibuffer-local-map should take care of it, but you might want to do the same thing for minibuffer-local-ns-map. M-x apropos-variable minibuffer map tells you about all of the maps.

[You can use C-h M-k to see the bindings of any keymap, e.g., minibuffer-local-must-match-map -- it is available in library help-fns+.el.]

OTHER TIPS

Likely what is happening is that you're doing a search (thus the prompt Next element matching (regexp):), and using your M-s M-s to stop the search.

What this actually does is just switch to a different buffer, but leaves the search active.

One change you could do is change your behavior, and use C-g to quit out of the search, and return you to the buffer you were searching. This is good to know in case you're in an Emacs that doesn't have your customization. You can also use the M-s M-s to switch back into the minibuffer, and then quit out with C-g.

But, I think your binding could be updated by doing:

(global-set-key (kbd "M-s M-s") 'my-other-window)

(defun my-other-window (count)
  (interactive "p")
  (if (and (>= (recursion-depth) 1) (active-minibuffer-window))
      (abort-recursive-edit)
    (other-window count)))

Which will automatically quit the minibuffer if it is active (and implicitly switch back to the original buffer), otherwise it'll just switch windows as requested.

You've now heard where the problem is coming from. As for how to get out of it, use C-].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top