     (setq commands (mapcar #'symbol-name commands))
     (smex-read-and-run commands)))
 
+(defun smex-extract-command-name (pretty-name)
+  "Given a string \"foo (C-c f)\", return \"foo\"."
+  (car (split-string pretty-name " ")))
+
+(defun smex-find-keybinding (command)
+  "Find the first keybinding for COMMAND, if one exists.
+Uses the currently active keymap."
+  (let* ((keybindings (where-is-internal command))
+         (first-binding (car keybindings)))
+    (when first-binding
+      (key-description first-binding))))
+
 (defun smex-completing-read (choices initial-input)
   (let ((ido-completion-map ido-completion-map)
         (ido-setup-hook (cons 'smex-prepare-ido-bindings ido-setup-hook))

  (setq smex-ido-cache (smex-convert-for-ido smex-cache)))

(defun smex-convert-for-ido (command-items)
  (mapcar (lambda (command-item) (symbol-name (car command-item))) command-items))

(defun smex-restore-history ()
  "Rearranges `smex-cache' according to `smex-history'"

        (ido-enable-flex-matching smex-flex-matching)
        (ido-max-prospects 10)
        (minibuffer-completion-table choices))
    (smex-extract-command-name
     (ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil
                          initial-input 'extended-command-history (car choices)))))

(defun smex-prompt-with-prefix-arg ()
  (if (not current-prefix-arg)
