Lesson 8 of 38 ยท Basics and Navigation
The Minibuffer and M-x
Use the Emacs minibuffer: run any command with M-x, complete names with TAB, browse history, repeat commands, and know how completion styles and fido-mode work.
Whenever Emacs needs something from you that is not a keystroke (a file name, a buffer name, a command name, a search string) it asks in the minibuffer, the bottom line of the frame. Learning to use it well is one of the biggest speed-ups in Emacs, because the same completion system works for every prompt.
M-x: run any command by name
M-x (execute-extended-command) prompts for a command name. Every command has a name, and most of them have no key binding at all. Type part of the name, press TAB to complete, then RET:
M-x toggle-truncate-lines RET M-x global-display-line-numbers-mode RET M-x emacs-version RET
If the command has a key binding, Emacs mentions it in the echo area after it runs ("You can run the command 'save-buffer' with C-x C-s"). That is a built-in reminder to learn the shortcut. If you ever forget how a command is bound, C-h w (where-is) tells you.
Commands behave exactly the same whether you start them with a key or with M-x, because keys are only shortcuts to named commands. This is why documentation always gives you the command name too.
Completion
In most minibuffer prompts these keys work:
| Key | Effect |
|---|---|
TAB |
Complete as far as possible; press again to show the candidates |
SPC |
Complete up to the next word (in some prompts; in file names it inserts a space) |
? |
Show the list of possible completions |
RET |
Accept what is typed (or the selected candidate) |
C-g |
Abort the prompt |
M-p / M-n |
Previous / next entry from the history |
C-a, C-e, M-b, M-f |
Edit the text as in any buffer |
C-k |
Delete to the end of the input |
Completion is decided by completion styles. The default list is basic, partial-completion and emacs22, and with partial-completion you can abbreviate each word: M-x g-d-l-m can reach global-display-line-numbers-mode. The flex style (add it with (setq completion-styles '(basic flex))) matches characters in order anywhere in the name.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
History and repeating
Emacs stores what you type at each kind of prompt. In a prompt, M-p brings back the last answer and M-n goes forward. C-x ESC ESC (repeat-complex-command) shows your last command with its arguments so you can edit and re-run it. C-x z (repeat) repeats the last command, and pressing z again repeats it again, which is perfect for a replace or a sequence of edits. Emacs 28 and newer also have repeat-mode, where certain commands allow a single key to repeat them after the first press (for example, after C-x o pressing o repeatedly switches windows). Turn it on with M-x repeat-mode.
Other special prompts
| Keys | Command | Prompt |
|---|---|---|
M-: |
eval-expression |
Evaluate a Lisp expression and show the result |
M-! |
shell-command |
Run a shell command and show its output |
M-| |
shell-command-on-region |
Pipe the region through a command |
M-x shell |
shell |
A shell inside Emacs |
C-x C-f, C-x b, C-x k |
File, buffer and kill prompts |
M-: is a quick calculator and test bench: M-: (* 6 7) RET prints 42. C-u M-: inserts the result at point.
Better completion with modern UIs
The default minibuffer is plain. Emacs 28 and newer ship fido-vertical-mode and icomplete-vertical-mode, which show candidates in a list below the prompt and filter as you type. Try M-x fido-vertical-mode. Many users install the add-on packages Vertico (vertical list), Orderless (match words in any order), Marginalia (annotations) and Consult (search commands built on completion). They all plug into the same completion system, so they improve every prompt at once. Packages are covered in the packages chapter.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Check your understanding
Quick check ยท 4 questions
of 4 answered You scored / 4 ยท perfect!
-
1. What does M-x do?
M-xreads a command name from the minibuffer and runs it. -
2. Which key shows the list of completions for a minibuffer prompt?
TABcompletes and shows candidates when there are several;?always lists them. -
3. How do you re-run your last command with its arguments, editable?
C-x ESC ESCis repeat-complex-command;C-x zrepeats without editing. -
4. Which key evaluates a Lisp expression you type in the minibuffer?
M-:is eval-expression.M-!runs a shell command.
Summary
- The minibuffer is the prompt line;
M-xruns any command by name. TAB,?,M-p,M-n, andC-gwork in almost every prompt; completion styles decide how matching works.C-x ESC ESC,C-x zandrepeat-moderepeat commands;M-:andM-!evaluate Lisp or shell commands.- Vertical completion UIs (fido, Vertico) improve every prompt at once.
Track your progress
Create a free account to tick off lessons and pick up where you left off on any device.
of Emacs lessons completed