Emacs Lisp: Get universal-argument

By Xah Lee. Date: . Last updated: .

This page shows you how to make your emacs lisp command accept Emacs: Universal Argument (prefix arg)

To make your command aware of universal argument, there are 3 simple ways:


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

Possible Values of Universal Argument

Here's the possible values of current-prefix-arg.

Key InputValue of current-prefix-argNumerical 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
(defun xg ()
  "print `current-prefix-arg'"
  (interactive )
  (message "%s" current-prefix-arg))

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

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