Kiwi
Kiwi

Reputation: 73

Interactively select multiple items

How can I let the user select multiple items from a list instead of just one? Like in the C-x b menu in helm.

Currently I can only select a single item instead of getting a complete list:

(defun test-main ()
  (interactive)
  (let ((choice (completing-read "Select: " '("item1 item2 item3"))))
    (message choice)))

Upvotes: 5

Views: 2118

Answers (2)

legoscia
legoscia

Reputation: 41568

You can do that with completing-read-multiple:

(defun test-main ()
  (interactive)
  (let ((choice (completing-read-multiple "Select: " '("item1" "item2" "item3"))))
    (message "%S" choice)))

It returns the selected items as a list, so if you type item2,item3 at the prompt, it returns ("item2" "item3").

Upvotes: 6

Drew
Drew

Reputation: 30701

Because that's what vanilla completing-read does. It reads and returns a single choice, providing completion to help you choose.

You can do what you are asking for with Icicles. It redefines function completing-read when Icicle mode is on.

Many Icicles commands are multi-commands, which means that you can make multiple input choices in a single command execution: a multiple-choice command. You can define your own multi-commands, using any actions.

And for any completion (not just for a multi-command), you can manipulate, save, and restore sets of completion candidates.

(You can also enter multiple inputs in the minibuffer. This is useful even for commands that read input without completion.)

Upvotes: 2

Related Questions