Getting Started with Emacs

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

After almost 2 years of using Vim as my one and only text-editor, I have been finally convinced enough to start learning Emacs. The following series of articles document my journey with this feature-rich text-editor/operating system and the various tips and tricks I learn on the way. This will suit for beginner users since I am a complete newbie myself. I am using the book Mastering Emacs by Mickey Petersen for reference.

Installation

Installing emacs is as simple as running the install command through your package manager. I am using Arch Linux so that would be done through pacman.

Installing Emacs

We can launch it with the emacs command which will open up a new GUI window.

Initial Startup

The Emacs Daemon

When I ran that command, it took a while for the window to launch. As we start to configure our text-editor (eventually turning it into an OS), running emacs everytime will take significantly longer. This is because we’re launching fresh instance of the program everytime, which will have to load all of our configuration scripts. Fortunately, it can act as a server and serve GUI windows to the emacsclient program that it comes bundled with.
To run emacs as a server, run emacs --bg-daemon.

Starting the daemon

While this was fairly easy, we still have to run that comand after, say, every reboot. Fortunately, the package comes with a systemd service. We can enable the daemon with systemctl enable --user --now emacs. It is to be noted that this should not be run as root.

Starting the daemon with systemd

Now we can connect to the daemon with the emacsclient. Run emacsclient -c to open up a new scratch buffer.

Scratch buffer

Press C-x C-c to exit. We can also run this as a terminal application. Run emacsclient -nw <optional_file>.

Running in the terminal

Basic Configuration

When run in GUI mode, we will be able to see various options on the top of the window. I am configuring basic things like fonts, themes, etc. from there. Eventually though, I’ll have to build my own ~/.emacs.d/init.el file. For now, I have only put some basic stuff there. I have changed the theme to wombat and the font to JetBrains Mono.

Package Manager

Emacs comes with a built-in package manager. We can configure it to fetch packages from various repositories. There’s the official package repository, ELPA, but it doesn’t have most packages that we might need. Thankfully, there’s the volunteer-run package repository called MELPA. That suits us for most of the cases. We need to tell our editor to use that as the package repository by placing the following code in ~/.emacs.d/init.el. Currently, I do not understand Elisp (or even Lisp, for that matter) so the syntax is pretty confusing to me. I hope I’ll eventually be able to grok it, though.

;; Set up package.el to work with MELPA
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(package-initialize)
Evil Mode

Since I am a long time vim user, the default keybindings are taking a toll on my fingers. Thankfully, there’s the evil-mode package which emulates vim keybindings and some of its behaviours. Append the following to the init.el file.

;; Download Evil
(unless (package-installed-p 'evil)
  (package-install 'evil))

;; Enable Evil
(require 'evil)
(evil-mode 1)

This will make Emacs usable enough for me to alias vim as emacsclient -nw.

This concluded the first part of this who-know-how-long series. I will write more once I figure out the various things I have yet to learn. Thanks for reading this one.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Pratik Devkota
Pratik Devkota
Software engineering student interested in UNIX (GNU/Linux, *BSD, etc.), security as well as free and open-source software.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook