How to Use Lisp in Emacs

With Emacs, you can utilize the Lisp programming language to extend different functionalities and customize various aspects of Emacs. The Emacs Lisp programming language is referenced as Elisp, and using it with Emacs is not as complicated as the other programming languages. However, you must learn a few basics to use Lisp in Emacs comfortably.

This post covers the basics that you should know on how to use Lisp in Emacs. We will provide different examples on some of the functionalities and features that you can use with Lisp to enhance your usage of Emacs. Let’s get started!

How to Access Elisp

Before we cover the different ways to use Lisp, the first step is accessing the Lisp environment on Emacs. Start by opening up your Emacs terminal.

On the Emacs window, press “M-x”. Then, type “ielm”. The Lisp environment will open up once you press the “Enter” key. The “M-x” implies you to press the “Alt” key with the “x”.

Once you successfully bring up the Lisp environment, you will get an interface like the one in the following. In this environment, we will see how to use Lisp in Emacs by giving different examples.

Examples on How to Use Lisp in Emacs

At first, using Lisp can be challenging due to the different commands that you should use to achieve various functionalities. As such, we present a few examples to get you started on some of the basics of using Elisp. For instance, let’s understand how you execute the code, use the functions, and utilize the Elisp init file to customize Emacs.

Example 1: Basic Lisp Expression
With Lisp, we use expressions which contain statements that define the way we create expressions. For instance, the following examples show how you can implement the arithmetic expressions in Lisp. The expression is enclosed with “()” and the arithmetic symbol comes before the numbers. Once you created the expression, pressing the “Enter” key will give the desired results.

Example 2: Working with Functions
Functions help with running the code numerous times without having to keep writing the code whenever you want to use it. Even with Elisp, it’s possible to create functions for different tasks. The function definition is different, but how we call it remains the same as in other programming languages.

To define a Lisp function, use the “defun” keyword and enclose the function with “()”. The following is an example of a function that takes a number as an argument, squares it, and returns the output:

You can also create an interactive function for more usability. For this case, when called, the function will prompt the user and take the input as arguments before giving the desired output. The following example creates a welcome function that asks the user to enter their name. It then takes the entered input and uses it to print a message.

Once you created your interactive function, press “M-x” and enter the function name that you want to call. Pressing the “Enter” key brings up the prompt where you interact and give the input.

Lastly, pressing the “Enter” key after adding the input in the prompt will display the expected output based on the message that you defined in your function.

Example 3: Customizing Emacs
When you want to customize Emacs, you must access the init file and write the Lisp code for the customization option that you want to use. To access the init file, press “M-x” and type “.emacs”.

The init file will open up and display the code for whatever customization you already have. For this example, the Lisp code fetches an Emacs theme from an archive, installs the theme and required packages, and loads them for use.

You can customize your Emacs by writing any Lisp code in this init file.

Example 4: Inserting a Text into the Buffer
A fun way to play with Emacs is to insert a text into the active buffer. For that case, we use Lisp to add an insert statement followed by the desired text. Once added, the expression will display the text at the cursor position.

Example 5: Conditional Statements
You can create various conditional statements depending on what your goal is. For our case, we created an “if” statement in a function. The function takes an argument and, based on the value, it uses the “if” statements to check if the condition is met before giving an output.

You can then call your function, add an argument, and let the conditional statement evaluate the argument and give the output.


This post presented different examples on how to use Lisp in Emacs. The examples focused on the basics of Lisp, but there is much more that you can dig deeper and learn about Elisp. Hopefully, this post has given you the needed insights to start your path on using Lisp in Emacs.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.