2009-05-30

finding commands with smex

If you're like me, you'll usually have a lot of buffers open in emacs. To quickly switch between them, you press C-x b (Ctrl-X and b), and type the beginning of buffer name. This is made much more convenient with e.g. ido (see Switching Buffers) or icicles. They allow you switch to a buffer by typing just some characters appearing anywhere in the buffer name, autocompletion etc.

The question for today is: what to do if am not searching for a buffer or file, but for some emacs command?

Often, I search for a certain command, ie. the ones that come after M-x. After typing M-x there's autocompletion if you know the beginning of the command… but maybe I remember only it was something with string, but exactly how? Was it replace-string or string-replace?

For these deep problems, there is smex (think: search-M-x). smex brings ido-style completion to choosing commands.

After installing the smex package, you can put something like the following in your .emacs:

(setq smex-save-file "~/.emacs.d/smex.save") ;; keep my ~/ clean                                      
(require 'smex)                                                  
(smex-initialize)                                                               
(global-set-key (kbd "M-X") 'smex)                                             

As you can see, I have set the keybinding to M-X, that is (for most people) Alt-Shift-x; of course you could override the normal M-x ('execute-extended-command', note the small x), but usually the normal completion is fine.

Admittedly, smex serves a niche use-case, but I still use it a couple of times a day.

[ Note that packages like icicles and anything can do similar things; they do much more as well, and I haven't had the chance to play with those in detail, so for the time being I stay with smex. ]

10 comments:

Anonymous said...

icicles does that too without the need for any extra packages

djcb said...

@Anonymous: well, icicles itself is the extra package, or?

perusio said...

You have an extra parentheses closing the key binding function invocation.

It should read:

(global-set-key (kbd "M-X") 'smex)

djcb said...

@perusio: thanks, fixed.

altxwally said...

That looks interesting though, I have always done it like this: I type M-x and then type an asterisk before the keyword I remember and it works in kind of the same way.

M-x *string

But it does open another buffer showing you the results so, taking that into account smex is much better. Keep up the good work! Nice blog!

Bill said...

It's not as flexible as these extra packages, but I like (partial-completion-mode t). Then you can do stuff like M-x q-r-r TAB, and it expands to query-replace-regexp.

Works for filenames also.

Anonymous said...

This is what I was looking for. Thanks :)

finsprings.org said...

How does this differ from using the apropos command?

djcb said...

@finsprings: M-x apropos does not autocomplete.

Ryan said...

If you want to make ido completion work in even more places, I've written some code for this that eventually grew into a full package:

https://github.com/DarwinAwardWinner/ido-ubiquitous