emacs

2024-04-30

Emacs’s navigation commands form the foundation of efficient text manipulation. Learning these shortcuts boosts productivity.

Example: Let’s say you have the following text:

This is a sample text string.

To delete the word “sample”, you would place your cursor on the ‘s’ in “sample” and press Alt-d.

Powerful Search and Replace

Emacs’s search functionality is incredibly robust, allowing for both simple and complex searches and replacements.

Example: To replace all occurrences of “text” with “data” in the sample text above:

  1. Press M-%.
  2. Enter “text” as the search string.
  3. Enter “data” as the replacement string.
  4. Press % to replace all occurrences.

Utilizing Regular Expressions

Emacs’s support for regular expressions expands its text manipulation capabilities. This allows for complex pattern matching and replacement.

Example: Let’s say you have a file with email addresses in the format name@domain.com. You want to extract only the domain names. You can use the following regular expression within Emacs’s search and replace functionality:

Search String: [^@]+@([^.]+)\.[^.]+ Replacement String: \1

This regular expression uses capturing groups ((...)) to extract the domain name. \1 refers to the first captured group.

Working with Regions

Emacs allows you to mark regions of text and perform operations on them.

Example: To copy a paragraph, mark the region of the paragraph using Ctrl-Space and then move the cursor to the end of the paragraph. Then, press M-w to copy it, and C-y to paste it elsewhere.

Macros for Automation

Emacs’s macro functionality enables the recording and replaying of keystrokes, automating repetitive tasks.

Example: If you need to perform the same sequence of edits on multiple lines, record those edits as a macro and replay it on each line, saving significant time and effort. This is particularly useful for tasks like reformatting text or applying consistent changes across multiple lines.