Bozhidar Batsov has a post that mentions how M-x shows a lot of commands many of which make no sense in the current context. This has never bothered me because after inputting a few (fuzzy) letters, the display converges on the command I’m looking for. Others, are not so sanguine and find the long list annoying.
It turns out that there’s an easy fix for this: read-extended-command-predicate. As of Emacs 28, You can set this variable to one of 3 values—or nil—to control the filtering of commands in the candidate list. These are:
command-completion-default-include-p- Exclude on those commands tagged for a different mode.
command-completion-using-modes-and-keymaps-p- Include only commands tagged for the current buffer and those commands that have a keybinding active in the current buffer.
command-completion-using-modes-p- Show only commands tagged for the current mode.
The first option is the most conservative and general and is what Batsov recommends for everyday use.
Batsov also explains how functions can declare what mode or modes they’re appropriate for. That’s simply a matter of listing them in the interactive declaration. There are a lot more details in Batsov’s post so be sure to take a look. The read-extended-command-predicate mechanism is similar to the execute-extended-command-for-buffer mechanism that I wrote about previously.
As I said, the long list doesn’t bother me but if it annoys you, this may be the answer.
Update : prefix → predicate