Emacs Notes

☁️ Ümit Eroğlu 🌍🛰
8 min readOct 12, 2023

--

Extensible, customizable free/libre text editor

-and more.

“From the GNU Emacs page”

Short History of Emacs

  • 1970: MIT AI Lab — TECO editor
  • 1976: Richard Stallman — Macro and buffer added to TECO
  • 1976: Guy Steele and David Moon — Unified macro editing (Emacs)
  • 1981: James Gossling — Gossling Emacs
  • 1986: Richard Stallman — GNU Emacs
  • 2023: Latest Version — Version 29.1

Features and Abilities

  • Content-sensitive editing.
  • Built-in documentation and tutorials.
  • Tools for compiling, running, and testing programs.
  • Highly customizable.
  • Built-in package manager for extensions.
  • Integrated shell.

Emacs Buffer

“In Emacs, all files are buffers, but not all buffers are files.”

A buffer holds the text you are editing. Only one buffer can be active. Each buffer has a unique name, and some have special names.

Windows and Frames

Emacs predates modern “windowing” operating systems.

Emacs Frames: Frames are Emacs “windows.” Frames are also present in terminals.

Emacs Windows: Windows are subdivisions of frames. Buffers are displayed in windows.

Point and Mark Point: Emacs term for the cursor. Tracked independently of location. Each buffer has a current point.

Mark: Works with the point. Represents the boundary of a region. Returns to a point in the buffer.

Killing and Yanking (Probably not what you’re thinking :)

Kill in Emacs: Equivalent to cut in other editors. Yank in Emacs: Equivalent to paste in other editors. What about copying? Known as saving to the kill ring.

Emacs Kill Ring:

  • Similar to the clipboard in other editors.
  • Holds a list of previously killed text.
  • Shared among all buffers.

Emacs Modes

A mode is a set of definitions that customize Emacs behavior in useful ways.

-From the GNU Emacs Manual page

Two types: major and minor modes

Major Modes

Every buffer should have a major mode. Only one major mode can be active. Usually language- or task-oriented.

Minor Modes

Optional. Multiple minor modes can be active simultaneously. Often independent of major mode.

For Mac Users: (Change iTerm2 profile for Emacs)

  • Most Mac keyboards lack the Alt key for use as Meta.
  • Create a new profile and modify the key mapping.

Installing Emacs

For apt-based systems:

  • sudo apt-get update
  • sudo apt install emacs

For rpm-based systems:

  • sudo dnf install emacs

Note: Use yum for older systems.

  • sudo yum install emacs

Building Emacs from Source:

wget https://ftp.gnu.org/pub/gnu/emacs/emacs-VERSION.tar.xz

Replace VERSION with the version you want to install.

tar -axvf emacs-VERSION.tar.xz

cd emacs-VERSION

./configure

make

make install

Starting and Quitting

Emacs From the Terminal:

  • emacs or emacs -nw

From a Window:

  • Use the “Start” menu.

To Quit:

  • ctrl-x, ctrl-c

For windowed option:

  • Click “X” on the window.

Commands and Keys

Commands

  • Custom Lisp functions.
  • Named by the programmer.
  • Write your own versions.

Keys

  • One or more input sequences.
  • Complete Keys and example Keys.
  • Associated with commands (key bindings).

Emacs Mini Buffer

The minibuffer is where Emacs is. It reads complex commands like file names, variables, Emacs command names, or Lisp expressions. Displays errors and general information.

Emacs Help

Emacs has extensive built-in documentation and an introductory tutorial.

  • Help system for help.
  • Multiple ways to get help.
  • Explore and answer your own questions.

Describe, Info, and Apropos

Three great ways to get help:

Describe:

  • Useful when you know what you’re looking for.
  • Explains keys, commands, functions, and more.

Info:

  • Access to Emacs’s own info manual (TeXinfo)
  • Access other installed TeXinfo manuals as well

Apropos:

  • Useful when you’re not sure what you’re looking for
  • Searches for commands, documentation, and more

Configuring Emacs

Nearly every aspect of Emacs can be configured and customized.

  • Customize the interface.
  • Configure or modify most behaviors.
  • Write your own Elisp code.

Customize the Interface

Built-in:

  • Best way to configure or customize Emacs.
  • Doesn’t support changing all aspects.

Layout:

  • Comprises groups and subgroups.
  • Groups typically represent a package, mode, or function.

Access:

  • Use M-x customize to launch the interface.
  • Available shortcuts, e.g., M-x customize-face.
  • Apply and save changes.
  • Using the mouse can be helpful.

Package Manager

Emacs extends with additional features through packages with the Emacs Lisp Library.

Overview:

  • Best way to install and view packages.
  • Multiple repositories available.
  • Add repositories to your startup file (~.emacs).
  • Learn more at the address below:

https://www.emacswiki.org/emacs/ELPA

Commands:

  • M-x package-list-packages
  • M-x package-install
  • M-x package-delete

Evaluating Elisp Code

Evaluate Elisp code you’ve written or downloaded without restarting Emacs.

  • M-x eval-buffer
  • M-x eval-region
  • Restart Emacs for a clean environment.

Working with Files

Finding a file:

  • Emacs “finds” files, but this term can be synonymous with “open files.”
  • C-x C-f (Find file).

Creating a new file:

  • Emacs doesn’t distinguish between finding an existing file and creating a new one.
  • If you find a non-existent file, Emacs opens an empty buffer.

Major Mode

  • Emacs has elaborate mechanisms for selecting major modes.

Detecting Major Mode

File-local variables

  • Header or footer to load a mode and variables

Program-specific loading instructions •

  • Shebangs (e.g., #!/bin/bash)

Magic mode detection

  • Uses magic-mod-alist for mode matching

Automatic mode detection

  • Common implementation for most modes.
  • Uses filename extensions, names, and paths.

Writing to Files

Save the current buffer:

  • C-x C-s (Save buffer)
  • Saves the buffer to the associated file.
  • If no associated file, Emacs will ask for a name.

Write to a new file:

  • C-x C-w (Write buffer to a new file)
  • Think of it as Save As.

Save all files:

  • C-x s (Save some buffers)
  • Emacs will prompt to save all unsaved files individually.

Navigating in Emacs

  • Start with familiar arrow keys.
  • Mouse is supported. Use
  • Emacs navigation keys.

Moving by Line

Visual Lines

  • What you see on the screen.
  • Wrapped lines are counted visually.

Logical Lines

  • Controlled by buffer contents.
  • Treats a long line this way.
  • Sometimes called screen lines.

Modern Emacs moves with visual lines.

Working with Buffers

Switching Buffers:

  • C-x b switches buffers.
  • Similar to Alt+Tab in other window managers.
  • Requires buffer to switch to.
  • Default is the previous buffer.

Listing Buffers:

  • C-x C-b lists buffers.
  • M-x ibuffer is an alternative that also lists buffers.

Killing Buffers:

  • C-x k kills a buffer.

Undoing/Reverting Operations

Esc Esc Esc

  • Useful when stuck.
  • Deletes (hides) all windows.
  • Exits prompts.
  • Cancels prefix keys.

When Mistakes Happen

Undoing:

  • C-/ , C — _ , or C-x u undo the last change.
  • All operations are recorded for undo.
  • Losing undo history is hard.
  • “Avoid the undo loop.”

Window Management

Emacs Windows:

  • Subdivisions of frames.
  • Buffers are displayed in windows.

Window Splitting:

  • C-x 2 splits the window vertically.
  • C-x 3 splits the window horizontally.

Deleting Windows:

  • C-x 0 deletes the active window.
  • C-x 1 deletes all other windows.

Switching Windows:

  • C-x o switches between windows.

“The Other” Window

  • When you run C-x o, it’s the window right after the current one.

Working with Other Windows:

  • C-x 4 C-f finds files in other windows.
  • C-x 4 C-o displays a buffer in another window.
  • C-x 4 b switches to and updates a buffer in another window.
  • C-x 4 0 kills the buffer and the other window.

Text Selection

  • Known as the region.
  • Consists of a point and mark.
  • Use mark as a point of reference.

Setting the Mark

Emacs Keys:

  • C-<SPC> sets the mark and activates or deactivates the region.
  • C-x C-x swaps the mark and point.
  • S+<arrow keys>, similar to selection in other editors.

Mouse Selection:

  • Mouse-1 + <drag> sets the point and mark around the text you move over.
  • Mouse-3 sets the mark to where the point is and then moves the point where you click.

Mark (Highlighting) History Overview:

  • Similar to the undo loop.
  • Each buffer has its own mark ring.
  • Configure how many entries to retain.
  • C-u C — <SPC> cycles through marks.

Global Mark Ring

  • Keeps track of visited buffers and where you placed a mark for each buffer.

Words, Buffers, Pages, and Paragraphs Object Marking:

  • M-@ sets the mark at the end of the next word.
  • M-h marks the next paragraph.
  • C-x h marks the whole buffer.
  • C-x C-p marks the current page.
  • Adding to existing regions.

Incremental Search

  • Directional search.
  • Matches as you type.
  • Display and choose matches.

Non-Incremental Search

  • Directional search.
  • Enter entire search string.
  • Display and choose matches.

Emacs Occur

  • Uses regex for search.
  • Provides a list of matching lines.
  • Can be activated within the current incremental search string.

Emacs Grep

  • Used to search through unopened files.
  • Provides a list of matching files and lines.
  • Files can be opened as buffers for editing.

Emacs and Killing

  • Equivalent to cut in other editors

What about copying?

  • Known as saving to the kill ring Kill Ring
  • Similar to the clipboard in other editors
  • Holds a list of previously killed text
  • Shared among all buffers

Difference Between Killing and Deleting

  • Killing Text is sent to the kill ring.
  • Successive kills are added.
  • Killing is also a form of deletion.

Deleting

  • Text is NOT sent to the kill ring.
  • Few delete commands.
  • Usually works on a character or spaces.

Kill, Ring, and Yank

Yanking

  • Retrieving from the kill ring.
  • Adds the entry from the current kill ring to the point.
  • Equivalent to pasting in other editors.

Search and Replace

  • Use with or without GNU-standard regex.
  • Operate on whole buffer or region.
  • Searches can be case-insensitive.
  • Display and select matches.

Changing Case

  • Change the case of text.
  • Works on a region or among words.

Transposing in Emacs

  • Changing the “units” of text
  • Can be used on characters, words, sexps, and more

Working with Lines

Joining and Splitting Lines:

  • Doesn’t modify your kill ring.
  • Point stays in its original position.

Deleting Lines

  • Ignores the current line.
  • Operates before the point.
  • Removes trailing whitespace and tab lines.

Managing Spaces

  • Useful for some programming languages.
  • After copying text to buffers.

Whitespace Minor Mode

  • Shows whitespace.
  • Uses highlighting and symbols.
  • Mode is customizable.

Working with Tabs and Spaces

Indented Lines:

  • Add or adjust indentation at the beginning of a line.
  • The simplest way is to press the TAB key.
  • Behavior is determined by the major mode.
  • Can operate on regions.

Tab Stops:

  • By default, a tab stop is every eighth column.
  • Can be customized.
  • In text mode, the position of the non-whitespace first character on the previous line is treated as the tab stop.

Running Commands

Shell Commands in Emacs:

  • Execute single command lines in a subshell.
  • Create output in a buffer (custom or stream).
  • Synchronous and asynchronous forms.

Interacting with the Shell Emacs Shell Types:

Shell mode.

  • Emacs shell.
  • Terminal emulator.

All leverage Emacs buffers.

Shell Mode

  • Runs an external shell (system default shell).
  • Appears in a new window.
  • Interactive programs may not work well.

Launched by M-x shell.

Built-in Lisp Shell

  • Bash-like features, but its own shell.
  • Cross-platform portability.
  • Use built-in commands or system commands.
  • Launched by M-x shell.

Running Interactive Programs

  • Acts like a regular terminal emulator.
  • May be slow.
  • Line and char modes.
  • Launched by M-x ansi-term.

--

--