What you need to know before try Emacs
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:
- 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.
- 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
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.
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 tocurrent buffer state
;; 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
;; o (initial buffer state)
;; |
;; |
;; x (current buffer state)
;; |
;; |
;; o
;; |
;; |
;; o
However, Emacs is not the case, its status is
;; 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:
;; (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, simplya d
, the state diagrams of editors are different, as shown below:
;; 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:
- keystroke will call self-insert-command.
- mouse click will call mouse-set-point.
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.
(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”
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.
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
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.
Magit
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
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
7 yy
And in Emacs requires
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)
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 .
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.
Steps:
counsel-ag world
search the current directory to search for files containingworld
C-c C-o (ivy-occur)
Open the occur interfaceC-x C-q (ivy-wgrep-change-to-wgrep-mode)
Enter edit mode:%s/world/emacs/g
Modify content with the help of evilC-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
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
multiple-cursors
ace-jump-mode
moves the cursor quickly according to the first character. The figure below is an example of fast jump according to p
yasnippet
template system, which simplifies input by defining abbreviations for code fragments
flycheck
syntax real-time check
treemacs
file directory tree navigation
projectile
project workspace management
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!
References
- Emacs Beginner's HOWTO
- Emacs As A Lisp Machine
- Ask HN: Is Visual Studio Code the Emacs of 21st century?
- What benefits does emacs offer over vs code
- What are the pros and cons of Vim and Emacs?
- Making Emacs popular again
- Buttery Smooth Emacs
- https://batsov.com/articles/2011/11/19/why-emacs/
- https://github.com/remacs/remacs#why-emacs