Question

I have a function that launches programs asynchronously:

(defun app (app-name)
  (interactive "sRun application: ")
  (async-shell-command app-name))

And i have a list of all executables from which to choose. I want the app function to behave as switch-to-buffer, providing TAB-completion for user. How do i use minibuffer completion in Emacs?

Was it helpful?

Solution

Use completing-read command. The function will look like

(defun app ()
  (interactive)
  (let ((app-name (completing-read "Run application: " program-list)))
    (async-shell-command app-name)))

Possibly more idiomatic is to use interactive instead of assigning to a variable according to Emacs Lisp Idioms: Prompting for User Input:

(defun app (app-name)
  (interactive (list (completing-read "Run application: " app-list)))
  (async-shell-command app-name))

Also you can use (start-process app-name nil app-name) instead of (async-shell-command app-name), if you don't care for process output according to Run a program from Emacs and don't wait for output.


See Minibuffer Completion for more ideas on completion in Emacs and Asynchronous Processes for calling processes from Emacs, both from GNU manuals.

OTHER TIPS

If you want completion for possible shell commands without needing to maintain a list yourself, and you're using Emacs 23 or newer, you can use read-shell-command:

(defun app (app-name)
  (interactive (list (read-shell-command "Run application: ")))
  (async-shell-command app-name))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top