Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 ยท Basics and Navigation

Prefix Arguments and Repeating

Give Emacs commands a count with C-u, C-u C-u and M-digit, understand negative and raw prefix arguments, and use them to repeat, move and insert text quickly.

Many Emacs commands do something slightly different, or do it several times, if you give them a prefix argument. The idea is simple: you type a number or C-u before the command. It is the most general repeat mechanism in the editor, and it is the reason a command can be both small and powerful.

Numeric arguments

Type C-u, then a number, then the command. Or hold Meta while you type the digits.

Keys Meaning
C-u 8 C-f Move forward 8 characters
M-8 C-f Same thing with Meta-digit
C-u 20 C-n Move down 20 lines
C-u 5 * Insert five asterisks (a self-inserting key repeats)
M-- M-f or C-u -1 M-f A negative argument: move backward one word

M-- (Meta and the minus key) alone is the argument -1. Negative arguments reverse many commands: M-- M-u upcases the previous word, which makes it easy to fix a word you just typed.

Writing C-u 8 costs two or three keys, and Emacs shows C-u 8- in the echo area while you type. If the command you want is not on a key you can also say C-u 5 M-x forward-word RET.

C-u on its own: a multiplier of four

C-u alone is the argument 4. Each extra C-u multiplies by four:

Keys Argument
C-u 4
C-u C-u 16
C-u C-u C-u 64

So C-u C-n moves 4 lines down and C-u C-u C-n moves 16. In many commands the plain C-u argument means "do the alternate version", not "four times". Examples:

  • C-u C-s starts a regexp search.
  • C-u M-x some-command often changes behavior, as documented by C-h f.
  • C-u C-x C-e inserts the evaluated value into the buffer.
  • C-u C-SPC jumps back to the previous mark (see the mark lesson).
  • C-u M-! inserts a shell command's output into the buffer.
  • C-u 0 C-k kills from the beginning of the line to point.

The command's docstring (C-h f) says exactly how it treats the prefix. Look for the words "With prefix argument" or "With a numeric argument".

JS Stand-in: how C-u prefixes combine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Repeating inserted text and keys

Ordinary typed characters accept an argument too. C-u 40 - inserts forty hyphens, perfect for a divider. C-u 3 C-o opens three blank lines (C-o inserts a newline and leaves point where it was). To redo the last command, use C-x z; pressing z again repeats it once more. For repeating sequences of commands, use keyboard macros, covered in the rectangles, registers and macros lesson.

JS Stand-in: a command with a count

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Raw and numeric prefixes in your own code

Commands defined in Emacs Lisp receive the prefix in their interactive spec: "p" gives the numeric value (1 if none), and "P" gives the raw value (nil with no prefix, (4) for a plain C-u, (16) for C-u C-u). You will use this when writing commands in the configuration chapter.

Practical uses

  • Scroll just a bit: C-u 3 C-v scrolls three lines.
  • Jump a paragraph or two: M-3 M-}.
  • Delete several words: M-3 M-d kills three words forward.
  • Indent a block of lines: select them and press C-x TAB, then use the arrow keys; or give C-u 4 C-x TAB to shift by 4.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What numeric argument does C-u C-u give?

    Each C-u multiplies by four, so two give 16.
  2. 2. Which keys move backward three words?

    A negative argument reverses direction, so both achieve the same result.
  3. 3. What does M-- (Meta and minus) alone provide?

    It sets the prefix to -1, which reverses the next command.
  4. 4. Where do you find out how a command uses its prefix?

    C-h f shows the docstring, which describes prefix behavior.

Summary

  • C-u N or M-N gives a command a numeric argument; C-u alone is 4 and each extra C-u multiplies by 4.
  • A negative argument (M--) reverses direction for many commands.
  • For some commands the prefix selects an alternate behavior instead of a count: read the docstring.
  • C-x z repeats the last command; macros repeat sequences.

Next: The Kill Ring and Yanking