Notes on Chapter 2: The Way of Emacs

By Susam Pal on 18 Dec 2022

The following notes were taken while discussing Chapter 2 of the book Mastering Emacs, 2022 edition (written by Mickey Petersen) in book discussion group meetings.

An index of notes for all chapters are available at notes.html.

Notes

The following notes are related to the content of the chapter.

Magpie's Nest of Shiny Things

The following commands mentioned in the book invoke some interesting functions:

Cornerstone of Emacs

The chapter mentions the Elisp interpreter as the cornerstone of Emacs. In fact, Emacs is an editor program that runs in the Elisp interpreter. When we start Emacs, what really starts first is an Elisp interpreter. It executes the Elisp code of an editor program. When that program executes, we get Emacs, the editor!

This behaviour is quite the opposite of how Vim and other editors behave. For example, when we start Vim or Visual Studio Code, first the editor runs. The editor then provides an embedded programming language that we can use to write plugins and extend the editor. For example, Vim offers VimScript to let us write code using VimScript and extend the functionality of Vim. However, when we start Emacs, first the Elisp interpreter runs and this interpreter executes a program that results in Emacs, the editor.

Uptime

The key sequence M-x emacs-uptime RET shows how long the current instance of Emacs has been running. Many users have month-long uptimes.

RPN Calculator

To try out the calculator, type C-x * c or M-x calc RET to start the calculator. Then to calculate something like, say, 1 + (2 * 3), type 1 RET 2 RET 3 RET * +.

Point and Mark

To try out point and mark quickly, first open a buffer with some text in it. For example, open a file, say, foo.txt using the key sequence C-x C-f foo.txt RET. Make sure there are a few lines of text in the buffer. If it is a new buffer, type some lines of text into it. Then move the point (cursor) to any arbitrary place within the text. The motion keys like C-p, C-n, C-b, C-f, etc. or simply <up>, <down>, <left>, <right>, etc. may be used to do this.

Then type C-SPC (i.e., ctrl+space) to set the mark at the current position of the point. Then move the point around. Emacs should now be highlighting the region between the mark (set earlier) and the point. Then type M-w (i.e., alt+w) to copy the text in the selected region or type C-w to cut the text instead. Finally, move the point around again and type C-y to paste the copied/cut text.

Returning to Mark

Marks can also be used to remember a position in the buffer to which we may wish to return to later. To try it out, first move the point to some place in the buffer where a mark needs to be set. Then type C-SPC C-SPC to set the mark without activating it (activating a mark causes Emacs to highlight the region as explained in the previous section). Then move the point to some other place in the buffer. Finally, type C-u C-SPC to return to the marked position.

Font Locking

Font locking (syntax highlighting) in Emacs is made up of faces of properties (colour, font, size, etc.). Type C-u C-x = to describe the current character (the character the cursor is on). The output displays the face details as well. Alternatively, type M-x describe-face RET to describe the face of the character the cursor is on.

Change Major Mode

Emacs almost always sets the appropriate major mode by inspecting the filename or the content of the buffer. However, sometimes it can be useful to set the major mode manually. For example, consider a file named foo.html which is open in Emacs and has the following text:

<h1>Euler's Identity</h1>
<p>
  In mathematics, <em>Euler's identity</em> is the
  equality

  \[ e^{i \pi} + 1 = 0. \]

  Euler's identity is a special case of Euler's formula from complex
  analysis, which states that for any real number \( x \),

  \[ e^{ix} = \cos x + i \sin x. \]
</p>

This is an HTML file with some LaTeX snippets that would perhaps be rendered using a LaTeX rendering tool such as MathJax or KaTeX. By default, when this file is opened in Emacs, the major mode is set to HTML+. However, if one wants to focus on editing the embedded LaTeX content, it is possible to change the major mode to LaTeX by typing M-x latex-mode RET. To change the major mode back to HTML+, type M-x mhtml-mode RET.

The following list includes some links that were discussed during the book discussion group meetings: