Elisp: Get User Input

By Xah Lee. Date: . Last updated: .

Ask for File Name

read-file-name
Ask user to give a file name. Return a string.
(read-file-name "Enter file name:")

Ask for Directory

read-directory-name
Ask user to give a directory name. Return a string.
(read-directory-name "Directory:")

Ask for String

read-string
Ask user to type a string. Return a string.
(read-string "Enter name:")

Ask for Regex

read-regexp
Ask user to type a Regex . Return a string.
(read-regexp "Type a regex:")

Ask for Number

read-number
Ask user to type a number. Return the answer.
(read-number "Type a number: " 10)

Select from a List

The best way to ask user to select from a list, is by completing-read.

(let ((xlist '("cat" "dog" "dragon" "tiger")))
  (message
   "%s"
   (completing-read "pick one:" xlist nil t)))

Query User for Yes/No

y-or-n-p
Ask user a yes or no question. Return t if user types ā€œyā€ and nil if user types ā€œnā€.
(if (y-or-n-p "Do it?")
    (progn
      ;; code to do something here
    )
  (progn
    ;; code if user answered no.
  )
)

Reference

Emacs Lisp, Get User Input