Smex is a M-x enhancement for Emacs. Built on top of Ido, it provides a convenient interface to your recently and most frequently used commands. And to all the other commands, too.
You are now ready to call M-x smex. The commands are displayed in an Ido completion buffer, ordered by relevance. The 7 most recently executed commands come first, the rest are sorted by frequency of use, command length and in alphabetical order.
To auto-start Smex every time you open Emacs add these lines to your .emacs file:
(require 'smex) ; Not needed if you use package.el (smex-initialize) ; Can be omitted. This might cause a (minimal) delay ; when Smex is auto-initialized on its first run.
Bind some keys:
(global-set-key (kbd "M-x") 'smex) (global-set-key (kbd "M-X") 'smex-major-mode-commands) ;; This is your old M-x. (global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)
 Delayed Initiation
Use the following code to make emacs startup a little faster. This delays initializing smex until it’s needed. Just have smex call ‘smex-initialize’ when it’s needed instead of having the user do it.
(global-set-key [(meta x)] (lambda () (interactive) (or (boundp 'smex-cache) (smex-initialize)) (global-set-key [(meta x)] 'smex) (smex))) (global-set-key [(shift meta x)] (lambda () (interactive) (or (boundp 'smex-cache) (smex-initialize)) (global-set-key [(shift meta x)] 'smex-major-mode-commands) (smex-major-mode-commands)))
 Learn more
The documentation is on github: https://github.com/nonsequitur/smex/blob/master/README.markdown
 Show only major mode commands
smex-major-mode-commands runs Smex, limited to commands that are relevant to the active major mode. Try it with Dired or Magit.