Elisp: universal-argument (prefix argument)
What is universal-argument
How to Get Universal Argument
To make your command aware of Universal Argument, in the function body, check the builtin variable
current-prefix-arg
It holds the value of universal argument.
(defun my-test () "print value of `current-prefix-arg'" (interactive) (message "%s" current-prefix-arg))
alternatively, use
Possible Values of Universal Argument
Here's the possible values of
current-prefix-arg
and
(prefix-numeric-value current-prefix-arg)
| Key Input | current-prefix-arg | prefix-numeric-value |
|---|---|---|
| none | nil | 1 |
| Ctrl+u - | Symbol - | -1 |
| Ctrl+u - 2 | Number -2 | -2 |
| Ctrl+u 1 | Number 1 | 1 |
| Ctrl+u 4 | Number 4 | 4 |
| Ctrl+u | List '(4) | 4 |
| Ctrl+u Ctrl+u | List '(16) | 16 |
| Ctrl+u Ctrl+u Ctrl+u | List '(64) | 64 |
Convert current-prefix-arg to number
The function prefix-numeric-value converts current-prefix-arg to number.
(defun my-test () "print numerical prefix arg received" (message "%s" (prefix-numeric-value current-prefix-arg) ) )