When it comes to Emacs, every programmer should have more or less heard of its name, after all, Emacs has a long history of nearly forty years. However, the high threshold for Emacs “entry” has led many beginners to abandon it before they can appreciate its essence. Before I became “proficient” in Emacs, I was also abused by it, so after using Emacs for four years, I think I can share my personal experience for readers who are interested in using Emacs.

This article will introduce how to get started with Emacs in two parts.

  • The first part introduces the core philosophy. The reason many beginners give up after a brief try with Emacs is that they don’t understand its design philosophy. Emacs is so unique that there is a lot of discomfort when switching from other editors. By understanding its philosophy, readers can determine whether Emacs is to their taste and whether it is worth the effort to master it.

  • The second part shares the personal process of using Emacs and gives specific advice to beginners in the context of this process, followed by extensions for everyday use. Many users choose Emacs because of a certain feature of a plugin, and even now I often marvel at the power of Emacs when I find some advanced uses.

Although this article will use ELisp as an example, the reader does not need to learn ELisp, just basic programming skills. Secondly, this article uses the more popular VSCode editor to compare the features of Emacs, which is also valid for other editors.

Emacs is Emacs. VSCode/Vim/Sublime… is yet just another editor.

Learning curve of common editors

Core concepts

GNU Emacs official site is described as follows.

An extensible, customizable, free/libre text editor — and more.

The first part is fairly straightforward, “an extensible, customizable, free text editor”, but the more part that follows is a matter of opinion and wisdom. It was once rumored that Emacs was an operating system disguised as an editor. This section will unravel the mystery of Emacs.

Text Editor

No matter how much Emacs’ fans deify it, Emacs is a text editor first and foremost. Unlike VSCode, Emacs is designed for keyboard-only operation, and the only editor similar to it is Vi. Keyboard-only operation is certainly more efficient than mouse-clicking. However, for beginners, it may be easier to use the mouse to click on the functions in the menu bar.

Emacs graphical interface

For text editing alone, Emacs offers a number of thoughtful features, two of which are listed here.

Backups

As a mature programmer, you need to be aware of the need for backups every now and then, as mistakes are inevitable. There are two main types of backups available in Emacs, here called static backup and automatic backup (auto saving).

  • Static backups occur when the file is first opened, and the backup file name is marked with ~ at the end; and it supports multi-version backups
  • Automatic backups, as the name implies, periodically save the file currently being edited, with # at the end of the file name. When saved, the automatically backed up file is deleted, while when Emacs crashes due to an unexpected reason (e.g. crash), the file is retained and can then be restored with the recover-this-file command to restore.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
静态备份的文件
!Users!liujiacai!.emacs.d!README.org.~1~
!Users!liujiacai!.emacs.d!README.org.~2~
!Users!liujiacai!.emacs.d!customizations!editing.el.~1~*
!Users!liujiacai!.emacs.d!customizations!editing.el.~44~*
!Users!liujiacai!.emacs.d!customizations!editing.el.~45~*
!Users!liujiacai!.emacs.d!customizations!editing.el.~46~*
!Users!liujiacai!.emacs.d!customizations!editing.el.~47~*
!Users!liujiacai!.emacs.d!customizations!editing.el.~48~*

自动备份的文件
#!Users!liujiacai!.emacs.d!customizations!misc.org#
#!Users!liujiacai!.emacs.d!customizations!navigation.el#*
#!Users!liujiacai!.emacs.d!elpa!lsp-java-20201105.1758!lsp-java.el#
#!Users!liujiacai!.emacs.d!init.el#

Above are some of the backup files on my computer. Thanks to these two features, I have been saved from the brink of collapse many times.

Undo/Redo

When editing files, Undo (Undo) and Restore (Redo) are important functions. Both operations are linear in traditional editors, while in Emacs, they are The difference between the two is illustrated here by a set of state diagrams during text editing.

  • Enter a b c in order to enter the current state in the following diagram

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    ;;                                o  (initial buffer state)
    ;;                                |
    ;;                                |
    ;;                                o  (first edit)
    ;;                                |
    ;;                                |
    ;;                                o  (second edit)
    ;;                                |
    ;;                                |
    ;;                                x  (current buffer state)
    
  • At this point, the two steps are undone and the first edit state is returned (i.e., only the character a is entered), and the state of the traditional editor at this point is as follows.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    ;;                                o  (initial buffer state)
    ;;                                |
    ;;                                |
    ;;                                x  (current buffer state)
    ;;                                |
    ;;                                |
    ;;                                o
    ;;                                |
    ;;                                |
    ;;                                o
    
  • This is not the case with Emacs, whose status is as follows.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    ;;                                o  (initial buffer state)
    ;;                                |
    ;;                                |
    ;;                                o  (first edit)
    ;;                                |
    ;;                                |
    ;;                                o  (second edit)
    ;;                                |
    ;;                                |
    ;;                                x  (buffer state before undo)
    ;;                                |
    ;;                                |
    ;;                                o  (first undo)
    ;;                                |
    ;;                                |
    ;;                                x  (second undo)
    
  • Its state is appended, and a single undo means a return to the last state, so the following state diagram may be more appropriate.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    ;;        (initial buffer state)  o
    ;;                                |
    ;;                                |
    ;;                  (first edit)  o  x  (second undo)
    ;;                                |  |
    ;;                                |  |
    ;;                 (second edit)  o  o  (first undo)
    ;;                                | /
    ;;                                |/
    ;;                                o  (buffer state before undo)
    
  • At this point, if a new insertion is made (for example, the character d), the characters on the text are all a d, but the editor’s state diagram is different, as shown below.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    ;;            Undo/Redo:                      Emacs' undo
    ;;
    ;;               o                                o
    ;;               |                                |
    ;;               |                                |
    ;;               o                                o  o
    ;;               .\                               |  |\
    ;;               . \                              |  | \
    ;;               .  x  (new edit)                 o  o  |
    ;;   (discarded  .                                | /   |
    ;;     branch)   .                                |/    |
    ;;               .                                o     |
    ;;                                                      |
    ;;                                                      |
    ;;                                                      x  (new edit)
    
  • At this point, if you do two more undos, the traditional editor returns to the initial state (no characters), while Emacs returns to the second state (with a b characters).

It’s very easy to get confused when first encountering Undo and Restore in Emacs, and I’ve often been confused by this, but fortunately there is Undo Tree plugin to visualize the undo state, and the above state diagram is taken from the UndoTree code The above state diagram is taken from the comments in the UndoTree code, thanks to the author’s contribution.

Extensions, customizations

At its core is an interpreter for Emacs Lisp, a dialect of the Lisp programming language with extensions to support text editing.

The above subsection introduced two very useful basic features of text editing, which are actually just the tip of the iceberg. The extensible and customizable nature of Emacs has created a creative community with countless powerful plug-ins. The reader may wonder how Emacs differs from VSCode, which also has a rich plug-in marketplace. This has to do with Emacs design architecture.

Emacs itself can be seen as a Lisp Machine, with an ELisp interpreter implemented in C at its core. Except for the part that interacts with the operating system, which is implemented in C, the rest is implemented in ELisp and parsed by the core ELisp interpreter.

All operations in Emacs are equivalent to function calls in the interpreter. For example, character input calls the self-insert-command function. This means that the user-defined code is equal to the Emacs source code (the ELisp part), for example, if Emacs source code defines a variable like foobar, then the user-written function can modify it directly.

If the reader is not familiar with Lisp, the analogy is that opening Emacs corresponds to typing python in the terminal to access the interactive REPL, where keyboard input and mouse clicks correspond to a function call.

Based on the core ELisp interpreter, it is very easy to extend Emacs (if you are familiar with ELisp), for example, by defining the following lines of code in init.el, you can open a browser in Emacs and do a Google search for the specified keywords.

1
2
3
4
5
6
7
8
9
(defun my/google-search ()
  "Googles a query or region if any."
  (interactive)
  (browse-url
   (concat
    "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
    (if mark-active
        (buffer-substring (region-beginning) (region-end))
      (read-string "Google: ")))))

As for VSCode, even a Hello World level extension, the steps are much more complex, see.

Based on Emacs’ full-featured ELisp interpreter, the community has developed a variety of plugins to make everything as easy as possible to do in Emacs. For example, listen to music, play games, read EPUB eBooks, chat with Telegram, and even any application can run in Emacs!

Emacs, “a great operating system, lacking only a decent editor”

Listen to NetEase Music in Emacs

Play Tetris in Emacs

Read EPUB eBooks in Emacs

Telegram chat in Emacs

Running aria2 with EAF in Emacs

Freedom

When it comes to Emacs, the person who must be mentioned is Richard Stallman, known as “The Godfather”. There were many versions of Emacs in the early days, but now GNU Emacs has basically unified the jungle.

Richard Stallman

The definition of free software can be found on the official GNU website (https://www.gnu.org/philosophy/free-sw.html), and will not be repeated here. The reader needs to be clear that free in the GNU community stands for free, not free.

Free software has undoubtedly contributed greatly to the development of the software industry by giving programmers the opportunity to understand the mechanics of the software they use, and Emacs, one of the early works of the godfather, undoubtedly inherits this idea. Every operation can be traced back to its roots, and I like this feeling of freedom.

More Emacs Hackers can be found at

Getting Started Guide

Experience Sharing

I came across Emacs because I was learning Clojure, and as a Lisp, Emacs is undoubtedly the best editor. However, due to the “rudimentary” functionality of the native Emacs, I couldn’t get the hang of it until I came across braveclojure on the Emacs tutorial and put emacs- for-clojure clone to the local configuration, then I think Emacs is not that “hard to use”. Out of my love for Lisp, I forced myself to code in Emacs as much as possible, and it took me about a month or two to get through the hardest adjustment period, and then I couldn’t live without it.

By now, my configuration file is much richer, and I have many functions written by myself. Before learning a new language, I configure the relevant Emacs extensions to make everything work in Emacs. Here I would like to highlight one point.

Emacs may not be the best in terms of individual features, but Emacs may be the best in terms of how to combine features organically and reduce switching.

The following suggestions are given in conjunction with my own experience in using them.

  • Find a mature configuration to put Emacs to use first, don’t bother with the details at first, and then try to customize it yourself when you get a feel for it. Spacemacs, Doom Emacs are two of the most popular in the community, we recommend beginners to try them both to find the most suitable for themselves.
  • Find a month to focus on the blitz, do not use Emacs intermittently, otherwise it is difficult to adapt to it, but once you get past this month, the later is infinite “springtime”
  • Learn ELisp when the various plugins don’t meet your needs and have bugs (I’m probably after two to three years), after all, that’s the essence of it. I myself refer to Learn X in Y minutes, Xah Lee’s Practical Emacs Tutorial
  • Good use of C-h i, Emacs’ own documentation, and How do I find out how to do something in Emacs?
  • Contact the community, Emacs China is the highest quality Chinese forum I’ve seen, which has many experienced Emacs Hackers to solve various problems encountered by beginners.
  • As of early 2020/11, I use Emacs to pursue “originality” and try to use Emacs’ own shortcuts (C-x C-s and so on), although the pain in my pinky started a year ago, when I mapped the CAPS key to Ctrl, and the problem could not be cured after about a year. Although the community recommended using evil (with Vim’s mode of operation) to solve this problem, I didn’t think it was “faithful” enough, so I didn’t use it until recently when I discovered viper mode and realized the naivety of this idea. There is no such thing as a standard answer. Emacs will absorb the advantages of other editors. So I immediately configured evil, which completely freed my little finger. After using Emacs for more than four years, I still can learn some life lessons from Emacs, which I guess other software can’t do. This point also prompted me to write this article to prevent beginners from falling into this mindset.

Of course everyone’s learning path is different, and readers can adjust to their own situation.

Org-mode

Edit UML in org-mode

Org-mode is the main reason why many non-programmers choose Emacs, simply speaking it is a markdown-like markup language, many users use it to take notes, manage GTD, and with the powerful extensions of Emacs, programmers use it to do literate programming, deservedly ranked first in the plug-in list. 🥇

At the moment I use org-mode in a simple way, I just use it as markdown, and this alone, together with Emacs’ shortcuts, has thrown all kinds of md editors out of the window. org-mode’s support for tables is also very good, for example, you can use the org-table-transpose-table-at-point command to switch the rows of the table over.

Table row conversion

Magit

magit status

Magit provides an interface for Emacs users to operate git, and is the first Emacs plugin I relied on heavily, and the second most popular plugin in the community. Magit is deeply integrated into the Emacs shortcuts, and everything git does flows like water. Without it, I wouldn’t even know how to rebase.

Evil

Evil Emacs steal my heart

As mentioned above in personal experience and in evil, it is not “evil”, but Extensible VI Layer for Emacs. Evil takes vi’s Evil adds the emacs state to disable all vi functions, in addition to porting the normal/insert/visual state (mode is another term in Emacs) over. Since it is in Emacs, we can completely customize the shortcuts to override vi’s default shortcuts and achieve the best Emacs/Vi integration, having both h j k l and C-a, C-e, M-s, M-f, M-b. Copying 7 lines of text, in the normal state of evil, requires only

1
7 yy

Native Emacs, on the other hand, requires

1
C-a C-SPC C-u 6 C-n C-e M-w

The best editor is neither Emacs nor Vim, it’s Emacs with Vim binding!

Dired

Dired is short for directory editor, a built-in plugin for Emacs, similar to the file manager Finder on macOS. In the Dired interface, you can easily move/delete/create files, and more powerfully, you can directly modify files in batches. The image below shows how to rename test_foo_*.dat to test_bar_*.dat in batches, manipulating files as smoothly as text.

dired batch rename files

Ivy/Counsel/Swiper

The Ivy/Counsel/Swiper trio is a complementary framework that makes it easy to display the candidates for the current operation in an interactive way, similar to the Command Palette in VSCode and the Double Shift in Intellj.

Ivy/Counsel/Swiper

Although other editors have similar functions, their functions are either limited or split from other functions, and there is no unified experience. Emacs is different, and even more plugins can have a unified experience, which is very influential to the user experience. The power of the ivy suite is illustrated here by the contents of ivy-occur + wgrep + evil batch modify multiple files.

The demo directory has 1.txt 2.txt two files, the content is hello world, here the batch modification is hello emacs.

ivy-occur Batch modification of content in multiple files

Steps.

  1. counsel-ag world search for files containing world in the current directory
  2. C-c C-o ivy-occur Open the occur screen
  3. C-x C-q ivy-wgrep-change-to-wgrep-mode Enter edit mode
  4. :%s/world/emacs/g Modify the content with the help of evil
  5. C-c C-c wgrep-finish-edit Save the contents

Of course, you can define shortcuts for the above operations according to your own habits.

Lsp-mode

lsp-mode

Before the advent of LSP, there was no unified framework to address the basic functionality of modern IDEs such as highlighting and completion for different languages, and Microsoft’s LSP has undoubtedly become the industry standard, eliminating the need to use regularity, which is both inadmissible and costly. There are two extensions in Emacs that support LSP, namely

  • Lsp-mode, which is out-of-the-box and provides all the experience of a traditional IDE by default
  • EGlot, which is small and delicate

Currently I am using lsp-mode, beginners can try them all and choose the one that suits their taste.

lsp-mode

More

In addition to the extensions described above, there are several extensions for everyday use, and of course many more interesting extensions for the reader to discover on their own.

  • company Complementary framework, can be used with lsp-mode

    company

  • multiple-cursors Multi-cursor point operations

    multiple-cursors

  • ace-jump-mode Moves the cursor quickly according to the character. The following figure shows an example of fast jumping based on p

    ace-jump-mode

  • yasnippet Template system that simplifies input by defining abbreviations for code snippets

    yasnippet

  • flycheck Syntax real-time check

    flycheck

  • treemacs File directory tree navigation

    treemacs

  • projectile Project Workspace Management

    projectile

    The above diagram shows how to find files in a project, switch between implementation and testing, and switch between different projects.

Summary

Emacs may not be as popular as VSCode, but that’s not a bad thing. For example, those who reach for Emacs are not suitable for Emacs, so letting them in will only lower the overall level of the community; moreover, Emacs is an open system, and will draw on the good design of VSCode, Emacs and other editors are not mutually exclusive. There was a discussion in the forum about Taiwan’s Emacs Twitter account maintainer “defecting” to VSCode, which is a controversial topic. But don’t forget the free spirit of Emacs, what’s best for you is best for you, there’s no need to be obsessed with something. A comfortable OS editor will make this tedious process fun. Finally, I’ll end this article with a quote from Mastering Emacs

Your patient mastery of Emacs is well-rewarded. I assure you.