Emacs: Universal Argument (prefix arg)
Emacs has a mechanism for a command to have variant behavior if user calls universal-argument
【Ctrl+u】
first, before calling the command.
The purpose of
universal-argument
is:
- A convenient way to let user pass numerical argument to a command. (What the command do with number argument depends on the command. Usually repeating the command.)
- To have a alternate behavior of a command. (e.g. copy file name without dir path, or copy full path.)
- If a command is not designed for taking universal-argument,
universal-argument
has no effect.
digit-argument, negative-argument
These are variant of universal-argument.
digit-argument
is equivalent to callinguniversal-argument
【Ctrl+u】 then type a number.negative-argument
is equivalent to callinguniversal-argument
【Ctrl+u】 then type a negative number.
Keyboard shortcuts for digit-argument
- The keys Ctrl+1 to Ctrl+9 and Meta+1 to Meta+9 are bound to
digit-argument
. - The key Ctrl+- and Meta+- calls
negative-argument
.
Examples of universal-argument
Type Ctrl+u 60 - to insert hyphen, 60 times.
In dired,
dired-copy-filename-as-kill
【w】
copies the file name, but type Ctrl+u 0 first, then w, the copied name is file full path.