The rapid progression of technology has allowed various programming tools to become prominent and come into the spotlight. Text editors are included in such tools because of massive development happening within the technology business. Their light-weight nature, together with their flexibility to edit and build files and strong out-of-the-box performance, has made text editors quite well-liked among the community. The advantages provided by text editors often lead developers to favor these tools over other similar tools, such as IDE. Text editors sit at the forefront of the work done by developers, so it is important to use an editor that provides a variety of features according to your needs.

Emacs is one such example of a text editor that, due to its versatility and customizable nature, has garnered quite the name for itself among the developer community. As one of the oldest text editors out there, Emacs is known for its stability and consistency.

What makes Emacs special is that it is not just a text editor, but rather, a fully-fledged machine. Emacs can be set up as a shell, an email client, an organizer, and so much more. The complexity of Emacs arises from the Lisp interpreter at its core, which in turn allows users to customize it with even more functionalities using the Lisp language.

This article covers how to use Lisp through various methods to configure and customize Emacs.

Basics of Lisp

Before seeing how Lisp can be used to configure Emacs, it is first important to look at some of the basics of the Lisp language to get a better understanding of this feature.

In Lisp, programs are made up of symbolic expressions, shortened to s-exps. These expressions can consist of either variables only, or include other functions, as well.

A function is notated by wrapping the text inside a parenthesis. For example, for calling the addition function, the following syntax is used:

The above statement says “add 2 to 2.” Nested s-exps will look something like this:

You can also store values inside of a variable using the setq command:

Functions can be defined using the defun keyword. For example, a function that calculates the square of a number is written as follows:

(defun square (x)


    (* x x))

(square 2)

You can evaluate functions defined by using the keys Ctrl x followed by Ctrl e. This will produce an output inside the mini-buffer. For example, the square function will have the following output:

Using Lisp in Emacs Emacs

Note: you must evaluate both the defun segment and the square segment.

That covers the basics. The following sections will show how to configure Emacs using Lisp.

Initialization File

When Emacs starts, the first processed file is the initialization file, or init file, which contains commands written in Lisp that allow users to configure Emacs. To open the initialization file, press Ctrl x, followed by Ctrl f, and then enter ~/.emacs. Inside this expression, you can insert additional code to customize Emacs.

1) Adding Support for Packages

Lisp can be used to add support in Emacs for packages of different sources. Melpa is one of the sources from which users can install these extensions. To add Melpa to Emacs, add the following lines to the init file:

(require ‘package)

(add-to-list ‘package-archives


             ‘(“melpa” . “http://melpa.org/packages/”) t)

(package-initialize)

(package-refresh-contents)

This code adds the Melpa archive to the list of package repositories, gives permission to Emacs to use these packages, initializes these packages, and refreshes the content for the changes to take place. If you open your package list by hitting Alt x and enter package-list-packages, you can see the installed packages in the Melpa archive.

Using Lisp in Emacs Emacs


­

2) Changing Theme of Emacs

Initially, when you load Emacs, you will get a screen welcoming you to Emacs and giving you various options, such as the Emacs Tutorial.

Using Lisp in Emacs Emacs

However, this page does not look that great. Lisp allows you to change the theme of Emacs and the startup page according to your preferences. Users can load various types of themes, change font sizes, and even add line bullets.

For example, say you want to replace the startup page to the scratch buffer, load the Material theme, and add line bullets. This can be done by adding the following lines to the init file:

(setq inhibit-startup-message t)

(load-theme ‘material t)

(global-linum-mode t)

As defined above, the setq syntax makes the inhibit-startup-message true, which removes the initial starting page. The load-theme loads the material theme. The global-linum-mode syntax is simply a function that is set to be true and is executed to produce line numbers. This is what Emacs should look like after inputting the above commands:

Using Lisp in Emacs Emacs

3) Bind Shortcuts to Key Bindings

Lisp can also be used to bind commands or shortcuts to keys. This allows users to customize Emacs according to their preferences, as well as run custom functions made by the user with just a click.

Suppose you have defined a function that simply produces a backslash character and you want to assign this to the keys Ctrl x followed by Ctrl o. This can be done by adding the following lines to the initialization file:

(defun insert_backslash ()


  (interactive)


  (insert ))

(global-set-key (kbd “

C-x C-o“)


                ‘insert_backslash)

Here, the function insert_backslash is defined, made interactive (this allows the function to be called interactively meaning with the key binding), and the output is a backslash. Then, you can bind the function with the keys given above using the global-set-key keyword.

Why Use Lisp?

Lisp is an integral part of Emacs, as it allows Emacs to have more power and functionality. Lisp provides an opportunity for users to customize Emacs and turn it into something that matches their interests and needs. Lisp is what makes Emacs truly powerful and unique compared to other text editors.

About the author

Using Lisp in Emacs Emacs

Zeeman Memon

Hi there! I’m a Software Engineer by degree, Blogger by skills who loves to write about tech, develop websites & do SEO. You can reach out to me on LinkedIn.