Elisp: Get universal-argument

By Xah Lee. Date: . Last updated: .

How to Get Universal Argument

To make your command aware of Emacs: Universal Argument (prefix arg) , there are 3 simple ways:

(defun xf (x)
  "print value of `current-prefix-arg'"
  (interactive "P")
  (message "%s" x))

(defun xg ()
  "print value of `current-prefix-arg'"
  (message "%s" current-prefix-arg))

;; try
;; M-x xg
;; C-u M-x xg
;; C-u C-u M-x xg
;; C-u 1 M-x xg
;; C-u 2 M-x xg

Possible Values of Universal Argument

Here's the possible values of current-prefix-arg and (prefix-numeric-value current-prefix-arg)

Key Inputcurrent-prefix-argprefix-numeric-value
Ctrl+u -Symbol --1
Ctrl+u - 2Number -2-2
Ctrl+u 1Number 11
Ctrl+u 4Number 44
Ctrl+uList '(4)4
Ctrl+u Ctrl+uList '(16)16
Ctrl+u Ctrl+u Ctrl+uList '(64)64

Convert current-prefix-arg to number

The function prefix-numeric-value converts current-prefix-arg to number.

(defun xh ()
 "print numerical prefix arg received"
 (message "%s" (prefix-numeric-value current-prefix-arg) )


Emacs Lisp, Get User Input