Elisp: Get User Input

By Xah Lee. Date: . Last updated: .

Ask for File Name

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

Ask for Directory

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

Ask for String

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

Ask for Regex

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

Ask for 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")))
   (completing-read "pick one:" xlist nil t)))

Query User for Yes/No

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?")
      ;; code to do something here
    ;; code if user answered no.


Elisp, Get User Input