Emacs minibuffer completion
Asked Answered
B

2

6

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?

Buffer answered 26/7, 2012 at 11:55 Comment(0)
B
11

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.

Buffer answered 26/7, 2012 at 11:57 Comment(0)
R
4

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))
Rumery answered 26/7, 2012 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.