Emacs Micro Lesson #9: Start a Process From Emacs Lisp

That’s right. Let’s take a common scenario. You’re doing some work in Emacs. Maybe you’re programming. Maybe you’re writing a novel. It doesn’t matter what you’re doing. You want to start playing some music or run a backup of your files in the background. You can open up a different app or your terminal emulator and run a command.

What if you could run that background process from Emacs with a simple function? One and done. That’s how we like to do things in the Emacs world. The best way to get this done is to use a function called (you guessed it already), start-process.

In the example below, I want to start the Audacious media player. But specifically, I’d like it to open a particular playlist. For example, I have a playlist for the Outrun video game soundtrack (Sega Genesis version). This bit of Emacs lisp will start up Audacious and provide a file path to the program as an argument:

(defun outrun-play ()
"Startup Audacious music player, playing Outrun playlist."
(start-process "Outrun soundtrack" nil "audacious" "/home/user/Music/outrun.m3u" ))

Let’s break with this function down:

the function to define the function
an optional documentation string, to briefly explain what the function does
sets this function as an interactive function, able to be called as a command in the Emacs minibuffer
The actual meat of the function, what the function is doing

The start-process function accepts a series of arguments:

  • NAME: The name of the process, which you can use to identify it. If you try to close Emacs while a process is running, you’ll get a warning, and you’ll be asked if you want to stop the process. The name you put here will appear in the warning test.
  • BUFFER: nil because we don’t use this argument in the function we’re writing here.
  • PROGRAM: The name of the program you are invoking with this function. In our example, audacious.
  • PROGRAM-ARGS: Arguments you want to provide to the program. In our example, we are merely providing a file path.

🕶 Yes!

Sign up to get an occasional email from me with the content you crave.

We’ll never send you spam or share your email address.
Find out more in our [link]Privacy Policy[/link].

Be the first to comment

Leave a Reply

Your email address will not be published.