0

I've installed smex through MELPA as a replacement for the default M-x (execute-extended-command). The issue is that in smex, certain global keybindings don't work. For instance, C-h should work as backspace, yet in smex it's still bound to default help.

(global-set-key (kbd "C-h") 'delete-backward-char)

Any idea why the above global-set-key might not work in smex, while it does elsewhere (in minibuffer, eshell, etc.)?

Addendum to Jesse's answer

When using ido-vertical-mode one should also rebind M-p:

(defun ido-vertical-define-keys ()
(define-key ido-completion-map (kbd "C-n") 'ido-next-match)
(define-key ido-completion-map (kbd "C-p") 'ido-prev-match)
(define-key ido-completion-map (kbd "M-p") 'previous-history-element))
Plankalkül
  • 250
  • 1
  • 7
Blaz
  • 199
  • 1
  • 8
  • IMO `C-h` is way more valuable as a help binding (especially `C-h v`, `C-h f` and `C-h k`/`C-h c`) unless you prefer to use `F1` instead. – Kaushal Modi May 22 '15 at 18:56
  • @kaushalmodi, but isn't backspace somewhat out of reach? Or do you use some other binding for `delete-backward-char` altogether? – Blaz May 22 '15 at 19:03
  • I've got used to `DEL` (backspace) and `C-DEL`; don't find it that inconvenient. Probably I use `C-d` and `M-d` equally or more. – Kaushal Modi May 22 '15 at 19:06
  • 1
    About the edit you made related to `ido-vertical-mode`.. note that you then are losing the `C-p` and `C-n` bindings which are more intuitive for up/down navigation in `ido-vertical-mode`. You should copy that whole function to your emacs config and then only edit the bindings you care about. – Kaushal Modi May 22 '15 at 19:14

2 Answers2

3

I also had this problem and couldn't fix. My workaround was overriding the smex keybindings function:

(defun smex-prepare-ido-bindings ()
  (define-key ido-completion-map (kbd "TAB") #'minibuffer-complete)
  (define-key ido-completion-map (kbd "M-p") #'previous-history-element)
  (define-key ido-completion-map (kbd "M-.") #'smex-find-function)
  (define-key ido-completion-map (kbd "C-h") #'delete-backward-char)
  (define-key ido-completion-map (kbd "C-a") #'move-beginning-of-line))
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
Jesse
  • 1,984
  • 11
  • 19
  • Nice. There's only one snag: `M-p` doesn't get defined and is still bound to "`ido-toggle-prefix`, which is an interactive compiled Lisp function in `ido.el'." – Blaz May 22 '15 at 18:37
  • 1
    @courteous I bet you have `ido-vertical` installed too because that sets `M-p` binding to `ido-toggle-prefix` (as it needs to use the original binding of `ido-toggle-prefix`.. `C-p` for `ido-prev-match` (ido match navigation)). To change that, you will need to override the `ido-vertical-define-keys` function in the same fashion as this solution for overriding bindings set by smex. – Kaushal Modi May 22 '15 at 18:51
  • @kaushalmodi, `ido-vertical` it is. That fixed the `M-p` as well. Regarding your edit of Jesse's answer: what difference do hashtags `#` make? Also, how did you go about finding that it is `ido-vertical-define-keys` that must be overridden? – Blaz May 22 '15 at 19:00
  • 1
    @courteous My approach to find such bindings is probably unconventional but I simply did `ag 'p.*ido-toggle-prefix'` in my `~/.emacs.d` and emacs source code `lisp/` directories... and [about the *sharp-quotes*](http://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html). *ag is like grep but faster.* – Kaushal Modi May 22 '15 at 19:09
  • How could I convert `C-s` to `C-n` and `C-r` to `C-p` ? – alper Jul 27 '20 at 00:37
0

I have used eval-after-load. I don't konw but @Jesse's solution was not changed the keybindings.

Code wrapped in eval-after-load will be executed only once. , so it is typically used to perform one-time setup such as setting default global values and behaviour.


(setq smex-save-file "~/.emacs.d/settings/smex-items")
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)

(eval-after-load 'smex
  `(defun smex-prepare-ido-bindings ()
     (define-key ido-completion-map (kbd "TAB") 'minibuffer-complete)
     (define-key ido-completion-map (kbd "C-,") 'smex-describe-function)
     (define-key ido-completion-map (kbd "C-w") 'smex-where-is)
     (define-key ido-completion-map (kbd "C-.") 'smex-find-function)
     (define-key ido-completion-map (kbd "C-a") 'move-beginning-of-line)
     (define-key ido-completion-map (kbd "C-p") 'previous-history-element)
     ))
alper
  • 1,238
  • 11
  • 30