What you need to know before try Emacs

Published: 2020-11-29   Updated: 2024-02-24   Tags: emacs

Table of Contents

When it comes to Emacs, every programmer should have heard its name more or less. After all, Emacs has nearly forty years of history. However, many users abandon Emacs before comfort with it due to the steep learning curve. I understand their frustration, I also made many attempts before "skilled" in Emacs.

After spend honey moon with Emacs for four years, I think I can share what I learn from Emacs and shed some light for newcomers. This article will be divided into two parts:

  1. The first part will introduce core concept of Emacs. I think the reason why many beginners give up after simply trying is that they don’t understand its design concept. Emacs is so unique that there are a lot of discomfort when migrate from other editors. By understanding its concept, readers can judge whether Emacs is in line with their own taste and whether it is worth the effort to master it.
  2. The second part will give some specific suggestions to beginners who want to get started with Emacs. Some extensions I use daily will also be introduce since many users choose Emacs because of a certain function of a extension. Until now I'm still amazed at the power of Emacs because of the advanced usage of an extension.

Although this article will use ELisp, ELisp knowledge isn't required and basic programming skills are enough. Secondly, this article compares the features of Emacs with VSCode which is the most popular editor now, and it is also valid to change to other editors(Sublime/Vim to name a few).

Emacs is Emacs, VSCode/Vim/Sublime… is yet just another editor —- Source

https://img.alicdn.com/imgextra/i4/581166664/O1CN0156nQHc1z6A1VldH2x_!!581166664.png
Learning curves of different editors

Core concept

The official GNU Emacs website introduces this:

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

The first part is fairly straightforward, as for the more part, the benevolent see benevolence, and the wise see wisdom.

It was once joked that Emacs was an operating system disguised as an editor. This section will unveil the mystery of Emacs.

Text editor

No matter how Emacs fans deify it, Emacs is a text editor in the first place. Unlike VSCode, Emacs is primarily designed for use with the keyboard. While it is possible to use the mouse to issue editing commands through the menu bar and tool bar, that is not as efficient as using the keyboard.

As far as text editing is concerned, Emacs provides a lot of practical functions. I will give two examples in this section.

Backup

As a mature programmer, you need to be aware of backups at all times. After all, mistakes are inevitable.

In Emacs, there are mainly two backup methods, called backups and auto-saving:

  • Backups occurs when you first open the file, the end of the backup file name has ~ labeled; and multiple-version backup is support
  • Auto-saving periodically save the current file being edited, the file name of the head and tail have # labeled. When saving, the auto-saving files will be deleted, and when Emacs crashes due to unexpected reasons (such as system crash), the files will be retained, and you can use the recover-this-file command to restore.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Backups
!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 ~*

Auto-saving
#!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#

The above show part of the backup files in my computer. Thanks to these two functions, I have been rescued from the edge of a crash many times.

Undo/Redo

When editing the file, undo and redo is a very basic, important feature. In conventional editor operations are linear, whereas in Emacs with the help of undo-tree, it can be a tree. I will illustrate the difference between the two using state diagram below:

  • First, enter three characters a b c, and we come to current buffer state
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
;;                                o  (initial buffer state)
;;                                |
;;                                |
;;                                o  (first edit)
;;                                |
;;                                |
;;                                o  (second edit)
;;                                |
;;                                |
;;                                x  (current buffer state)
  • Then undo of two steps back to first edit state (only a is left), conventional editor will have state as
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
;;                                o  (initial buffer state)
;;                                |
;;                                |
;;                                x  (current buffer state)
;;                                |
;;                                |
;;                                o
;;                                |
;;                                |
;;                                o

However, Emacs is not the case, its status is

 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)

The state is append-only, and a undo means returning 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 you perform a new insertion (such as d), although the characters on the text are the same, simply a d, the state diagrams of editors are 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 we undo twice, conventional editor returns to initial state (no characters), but Emacs restore to second state (there are a b there).

It is very confusing when I first encountered tree-based undo, but fortunately undo-tree provide undo-tree-visualize to visualize the undo state. The state diagram above is taken from its comments.

Expansion and customization

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

The above section introduced two very useful basic functions in text editing. In fact, this is just the tip of the iceberg. Emacs's extensible and customizable features empower a creative community with countless powerful extensions. Readers may wonder, VSCode also has a rich extension market, so how is Emacs different from it? This is related to the design architecture of Emacs.

Emacs itself can be regarded as a virtual machine(Lisp Machine). At the core of Emacs is a full-featured Lisp interpreter written in C. Only the most basic and low-level pieces of Emacs are written in C. The majority of Emacs is actually written in ELisp. All operations, both built-in and user-defined, are actually just function call within the interpreter. For example:

So, in a sense, Emacs has an entire programming language built-in which you can use to customize, extend, and change its behavior. For example, if a variable such as foo is defined in source code of Emacs, the function written by the user can directly modify it. For those who do not know Lisp, this is very incredible, but it indeed can.

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: ")))))

Put the few lines of code above to init.el, We can do google search in Emacs!

In VSCode, even a Hello World level extension, the steps are much more complicated, for whose who interested can refer to

There are many wonderful extensions exploring the power of Emacs, such as listen to music , play games , read EPUB e-books , chat Telegram, and even any application can run in Emacs!

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

https://img.alicdn.com/imgextra/i1/581166664/O1CN01PiBSo01z6A1VA2Lvt_!!581166664.png
Listen to music in Emacs
https://img.alicdn.com/imgextra/i2/581166664/O1CN012SFAW41z6A1WeRU9M_!!581166664.png
Play Tetris in Emacs
https://img.alicdn.com/imgextra/i4/581166664/O1CN01hJslQh1z6A1VLFP7y_!!581166664.png
Read EPUB e-books in Emacs
https://img.alicdn.com/imgextra/i4/581166664/O1CN01DWl21B1z6A1UNsI4U_!!581166664.jpg
Telegram chat in Emacs
https://img.alicdn.com/imgextra/i1/581166664/O1CN01vpjexS1z6A1PICqIh_!!581166664.gif
Run aria2 using EAF in Emacs

Free/Libre

When it comes to Emacs, the person I have to mention is Richard Stallman. There are many versions of Emacs in the early days, but now GNU Emacs has basically unified the world.

https://img.alicdn.com/imgextra/i2/581166664/O1CN01VH3Txp1z6A1WcQQ05_!!581166664.jpg
Richard Stallman

Stallman strongly advocates free software. The definition of free software can be found on the official GNU website, so I won't repeat it here. Readers at least need to be clear that free in the GNU community stands for freedom, not free beer.

Free software has undoubtedly greatly promoted the development of the software industry. It gives programmers the opportunity to understand the implementation mechanism of the software used. As one of the early works of Stallman, Emacs undoubtedly inherits this idea. Every operation can be traced to the source, I like this feeling of freedom.

More Emacs Hackers can refer to:

Getting Started

Experience and suggestions

I come into Emacs because I learned Clojure. As a Lisp, Emacs is undoubtedly the best editor. However, vanilla Emacs is bloated but somehow surprisingly bare, I make many attempts before I fell comfortable with it. It is Emacs tutorial on braveclojure helps me overcome the hard days, I use emacs-for-clojure configuration as the basis, and I force myself to code in Emacs as much as possible.

It took about a month or two to get through the most difficult period of adaptation. Up to now, my configuration file has been enriched a lot, and there are many functions written by myself. Before learning a new language, the relevant Emacs extensions will be configured first, so that everything can be done in Emacs. Here I want to emphasize one point:

In terms of a single function, Emacs may not be the best, but how to organically combine various functions and reduce switching, Emacs is the best.

Here are some suggestions based on my own experience after using Emacs for 4 years:

  • To deal with the discoverability problem, find a mature configuration instead of configuring all by yourself. You don't have to worry about the details at first. Spacemacs and Doom Emacs are the most popular in the community. It is recommended that beginners try both to find which one is suitable for them.
  • Find a month to focus on familiarizing with Emacs. Don't use it intermittently, otherwise it will be difficult to adapt to it. Once this month has passed, there will be unlimited "spring breeze".
  • When various extensions cannot meet your needs and have bugs (I'm in this status probably after two to three years), learn ELisp. After all, this is its essence. Recommend resources: Practical Emacs Tutorial by Xah Lee and Learn X in Y minutes
  • Make good use C-h i, the documentation that comes with Emacs, especially do I find out how to do something in Emacs?
  • As of the beginning of 2020/November, I use Emacs to pursue the "authentic" and try to use Emacs's own shortcut keys (C-x C-s). Although my little finger started to hurt a year ago, I mapped the CAPS key to Ctrl at that time to overcome this. Problem still remains but I'm this mode for about one more year. Although the community recommends the use of evil to solve this problem, I thought it isn't "loyal" enough, and never use it. Until recently I discover the viper mode and realize how naive I'm. Emacs's core concept is that you can customize it according to your own needs, there is no so-called standard answer. So I immediately install evil and completely liberated my little finger. After more than four years, I can still learn some life experience from Emacs. It is estimated that this is not possible with other software. This also prompted me to write this article to prevent beginners from falling into this kind of thinking.

Of course, everyone's learning path is different. Readers can adjust according to their own situation.

Extensions recommendation

Org-mode

https://img.alicdn.com/imgextra/i4/581166664/O1CN01gzJi7t1z6A1OkLTZ9_!!581166664.png
Edit UML in org-mode

Org-mode is the main reason why many non-programmers choose Emacs. Simply put, it is a markdown-like language. Many users use it to take notes and manage GTD. With the help of Emacs's powerful expansion capabilities, programmers use it for literate programming, and it deserves to be ranked first in the extension list. 🥇

At present, I use org-mode relatively simply, just use it as markdown at the time. Just this point, coupled with the shortcut keys of Emacs, it has been a few blocks ahead from various editors.

One thing I demo here is table support with org-mode. You can use the org-table-transpose-table-at-point command to transpose row and column of a table.

https://img.alicdn.com/imgextra/i1/581166664/O1CN01VDVZEM1z6A1UOtSm0_!!581166664.gif

Magit

https://img.alicdn.com/imgextra/i1/581166664/O1CN01GeC6rw1z6A1VdWuEW_!!581166664.png

Magit provides an interface for Emacs to git. It is the first Emacs extension I rely on heavily and it's the second-ranked extension in the community. All git operations are extremely easy within magit. Without it, I wouldn't even be able to do rebase.

Evil

https://img.alicdn.com/imgextra/i3/581166664/O1CN01TjEFRp1z6A1U4MagS_!!581166664.png
Evil Emacs steal my heart

I mentioned evil in my personal experience above. It is not "evil" but Extensible VI Layer for Emacs. In addition to porting vi's normal/insert/visual state, Evil also adds emacs state to disable all vi functions. Because it is in Emacs, we can customize the shortcuts to override vi's, we can have both h j k l and C-a, C-e, M-s, M-f, M-b.

Copy 7 lines of text, in the normal state of evil, only need

1
7 yy

And in Emacs requires

1
C-a C-SPC Cu 6 Cn C -e Mw

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

Dired

Dired is the abbreviation of directory editor and is the built-in extension of Emacs, similar to the file manager Finder on macOS. In Dired interface, you can easily move/delete/create the file just like edit text. The following figure shows how to test_foo_*.dat rename test_bar_*.dat in bulk. (source)

https://img.alicdn.com/imgextra/i2/581166664/O1CN01QIzFM91z6A1TiEdB0_!!581166664.gif
dired rename files in bulk

Ivy/Counsel/Swiper

Ivy/Counsel/Swiper is a completion framework, which can easily display the candidates of the current operation in an interactive way, similar to the Command Palette in VSCode and Double Shift in Intellj .

https://img.alicdn.com/imgextra/i1/581166664/O1CN01BnQ5pp1z6A1NIcJrL_!!581166664.png

Although other editors have similar functions, their functions are either limited or separated from other functions, and there is no unified experience. Emacs is different, no matter how many extensions we have, we can still have a unified experience, this greatly affects the user experience.

Below, ivy-occur + wgrep + evil is used to modify the contents of multiple files in bulk to illustrate the powerful functions of the ivy suite.

There are two files 1.txt 2.txt in current directory, whose content are all hello world, and modify to hello emacs at last.

https://img.alicdn.com/imgextra/i1/581166664/O1CN01dS73W31z6A1Tk5UwK_!!581166664.gif

Steps:

  • counsel-ag world search the current directory to search for files containing world
  • C-c C-o (ivy-occur) Open the occur interface
  • C-x C-q (ivy-wgrep-change-to-wgrep-mode) Enter edit mode
  • :%s/world/emacs/g Modify content with the help of evil
  • C-c C-c (wgrep-finish-edit) Save files

Of course, you can define shortcut keys for the above operations according to your own habits. The above five steps are done in one go.

Lsp-mode

https://img.alicdn.com/imgextra/i1/581166664/O1CN01EeQOpy1z6A1U5lWzk_!!581166664.png
lsp-mode

Before the emergence of LSP , there was no unified framework to solve the basic functions of modern IDEs such as highlighting and completion of different languages. The LSP launched by Microsoft has undoubtedly become the industry standard, and there is no need to use regular, which is both inaccurate and rude. There are two extensions in Emacs that support LSP, namely

  • Lsp-mode, provides all the experience of traditional IDE by default
  • EGlot, the main focus is small and exquisite

Currently I use lsp-mode, beginners can try it, and then choose the one that suits their taste.

More

In addition to the extensions introduced above, there are some more "small" extensions I use daily. Of course, the list can go on and on, readers can find out more by yourselves.

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

https://img.alicdn.com/imgextra/i3/581166664/O1CN01F3lxtC1z6A1RlueN7_!!581166664.png

  • multiple-cursors

https://img.alicdn.com/imgextra/i4/581166664/O1CN01ceUOar1z6A1OW1MMp_!!581166664.gif

  • ace-jump-mode moves the cursor quickly according to the first character. The figure below is an example of fast jump according to p

https://img.alicdn.com/imgextra/i1/581166664/O1CN019sHvUm1z6A1R9QtHn_!!581166664.gif

  • yasnippet template system, which simplifies input by defining abbreviations for code fragments

https://img.alicdn.com/imgextra/i2/581166664/O1CN01lQVIpx1z6A1Wz6Th3_!!581166664.png

  • flycheck syntax real-time check

https://img.alicdn.com/imgextra/i4/581166664/O1CN01gL8IST1z6A1WZt3Dk_!!581166664.png

  • treemacs file directory tree navigation

https://img.alicdn.com/imgextra/i3/581166664/O1CN01oTtvbz1z6A1RykfJ8_!!581166664.png

  • projectile project workspace management

https://img.alicdn.com/imgextra/i3/581166664/O1CN01yHD9GD1z6A1Tmi66V_!!581166664.gif The above illustration shows how to find files in a project, switch between implementation and testing, and switch between different projects

Conclusion

Perhaps the popularity of Emacs is far less than VSCode, but this is not a bad thing. For example, free riders are not suitable for using Emacs. Let them in will only lower the overall level of the community; and Emacs is an open system, it will learn from excellent design in VSCode, Emacs and other editors are not mutually exclusive.

There will always be posts telling switch between Emacs than other editors in the Internet, this kind of controversial topic will undoubtedly attract everyone’s attention, but don’t forget the free spirit of Emacs. The one that suits you is the best. There is no need to indulge in something.

After all, Emacs/VSCode are just tools. Solving practical problems is the most important thing. Of course, a comfortable operating system editor will make this boring process fun.

Finally, I want to share with you a sentence from Mastering Emacs:

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

That's all, Happy Emacs!

Discussion on Lobste and Reddit.

References