Elisp: Get User Input

By Xah Lee. Date: . Last updated: .

Get User Input with Name Completion and Input History

The most useful functions for getting user input with completion or command history support are:

Command history means, user can press key to enter previous input. (For example, shell-command) Also, some commands provide name completion (For example, dired).

read-file-name example:

(defun ff ()
  "Prompt user to enter a file name, with completion and history support."
  (message "String is %s" (read-file-name "Enter file name:")))

Try it. You'll have file name completion feature. Pressing will show previous file name you used.

read-directory-name example:

(defun ff ()
  "Prompt user to enter a dir path, with path completion and input history support."
  (message "Path is %s" (read-directory-name "Directory:")))

read-string example:

(defun ff ()
  "Prompt user to enter a string, with input history support."
  (message "String is %s" (read-string "Enter your name:")))

read-regexp example:

(defun ff ()
  "Prompt user to enter a elisp regex, with input history support."
  (message "Regex is %s" (read-regexp "Type a regex:")))

The most general command is read-from-minibuffer. All the above are implemented on top of it.

(info "(elisp) Minibuffers")

Select from a List

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

(require 'ido)

(setq choices '("cat" "doc" "dragon" "tiger"))

(defun my-pick-one ()
  "Prompt user to pick a choice from a list."
  (message "%s" (ido-completing-read "Open bookmark:" choices )))

Query User for Yes/No

y-or-n-p → Ask user a “y or n” question. Return t if answer is “y” and nil if it is “n”.

(if (y-or-n-p "Do it?")
      ;; code to do something here
    ;; code if user answered no.

(info "(elisp) Yes-or-No Queries")

Get User Input as Function's Arguments

Elisp: Interactive Form

Get User Input from universal-argument

Elisp: Get universal-argument

Writing Emacs Command

  1. Elisp: How to Write Commands
  2. Elisp: Region, Active Region
  3. Elisp: Get Buffer String
  4. Elisp: Functions on Line
  5. Elisp: Cut Copy Paste to/from kill-ring
  6. Elisp: Get User Input
  7. Elisp: Get universal-argument
  8. Elisp: Find Replace String in Buffer
  9. Elisp: Using thing-at-point
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.