Skip to content

Creating simple commands

When creating simple commands you can either use ‘consult--read’ or directly rely on the built-in ‘completing-read’ function. ‘consult--read’ provides a nicer and more featureful API on top of ‘completing-read’. You can specify the completion candidates directly.

emacs-lisp
(consult--read
 '("String A"
   "String B"
   "String C")
 :sort nil
 :prompt "Select: ")

If you pass it an alist of ‘(completion-string . value)’, you can look up the value.

emacs-lisp
(consult--read
 '(("String A" . value-a)
   ("String B" . value-b)
   ("String C" . value-c))
 :prompt "Select: "
 :sort nil
 :lookup #'consult--lookup-cdr)

You can add the result as a text property.

emacs-lisp
(consult--read
 (list
  (propertize "String A" 'consult--candidate 'value-a)
  (propertize "String B" 'consult--candidate 'value-b)
  (propertize "String C" 'consult--candidate 'value-c))
 :prompt "Select: "
 :sort nil
 :lookup #'consult--lookup-candidate)

You can add other text properties.

emacs-lisp
(consult--read
 (mapcar (lambda (value)
           (propertize
            (format "%s %s"
                    (propertize "Option" 'face 'font-lock-comment-face)
                    value)
            'consult--candidate value))
         '("A" "B" "C"))
 :prompt "Select: "
 :sort nil
 :lookup #'consult--lookup-candidate)