Mastering Emacs: Boosting Productivity with Powerful Commands and Shortcuts
Tags: linux, tutorial, beginners, softwareengineering
Published: 2023-06-04 21:19:17

[![GitHub Logo](https://res.cloudinary.com/bizstak/image/upload/v1685060686/github_f9ljwi.svg)](https://github.com/techstackmedia/software-engineering-series/tree/08-mastering-the-basic-of-emacs-commands)

## Introduction

Emacs is a versatile and highly customizable text editor that has been a favorite among programmers and writers for decades. With its extensive collection of commands and shortcuts, Emacs provides an efficient and powerful editing experience. This blog post will explore some essential Emacs commands and shortcuts that can significantly enhance your productivity. From basic navigation to advanced features, we will cover a range of commands to help you master Emacs and streamline your workflow.

## Installation

For detailed instructions on installing Emacs, please refer to the [official Emacs website](https://www.gnu.org/software/emacs/download.html). However, if you're a Windows user, I'll provide you with a few commands to help you install Emacs.

### Steps

To install Chocolatey and use it to install Emacs, follow these steps:

1. Search for PowerShell and run it as an administrator on Windows.
2. Go to the Chocolatey [installation page](https://chocolatey.org/install).
3. If this is your first time installing Chocolatey on your operating system, execute the following command in your terminal, based on the instructions provided on the installation page:

```powershell
Get-ExecutionPolicy
```

- If the output is "Restricted", enter one of the commands below:

```powershell
Set-ExecutionPolicy AllSigned
```

or

```powershell
Set-ExecutionPolicy Bypass -Scope Process
```

- Regardless of whether "Restricted" was the previous output or not, run the following command (copy and paste it into your terminal):

```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
```

- If there are no errors, check if Chocolatey was successfully installed by running the following command:

```powershell
choco
```

If the output displays the version of Chocolatey you are using, it means the installation was successful.

- Now, search for the package you need (in this case, Emacs) by visiting the [package search page](https://community.chocolatey.org/packages). Once you find the Emacs package, execute the following command:

```powershell
choco install emacs
```

- Open any terminal of your choice and type `emacs` to start using Emacs:

```powershell
emacs
```

Opening files and directories in Emacs is a straightforward process. To open a specific file, you can use the following command:

```sh
emacs <filename>
```

For example, if you want to open the file named "README.md," you would use the command:

```sh
emacs README.md
```

On the other hand, if you wish to open a directory, use the following command:

```sh
emacs <directory>
```

For instance, let's say you have a directory called "myProject," and you want to open it in Emacs. You would execute the command:

```sh
emacs myProject
```

By following these steps, you will be able to install Emacs on your Windows system and begin using it for your text editing needs.


## Getting Started with Emacs

To open Emacs in the terminal, use the `emacs -Q -nw` command, and then use `Ctrl + x Ctrl + f` to open a file by typing the file name at the bottom of the terminal. This command starts Emacs in the terminal without loading any configuration files, ensuring a clean environment. Once Emacs is running, you can access the drop-down menu by pressing `F10`.

**Note**: To save changes on Emacs use `Ctrl + x Ctrl + s`.

## Navigating and Editing

### Moving within the Document

- `Ctrl + f` moves the cursor forward one character.
- `Ctrl + p` moves the cursor backward one line.
- `Alt + f` moves the cursor forward one word.
- `Alt + b` moves the cursor backward one word.
- `Ctrl + a` moves the cursor to the beginning of the line.
- `Ctrl + e` moves the cursor to the end of the line.
- `Alt + a` moves the cursor to the beginning of the sentence.
- `Alt + e` moves the cursor to the end of the sentence.
- `Alt + Shift + }` moves the cursor to the previous paragraph.
- `Alt + Shift + {` moves the cursor to the next paragraph.
- `Alt + Shift + <` moves the cursor to the beginning of the document.
- `Alt + Shift + >` moves the cursor to the end of the document.
- `Alt + g g` prompts for a line number and jumps to that line.

### Copying, Cutting, and Pasting

- `Ctrl + space + arrow` or `Ctrl + n/p/f/b` selects text.
- `Alt + w` copies the selected text to the _kill-ring_.
- `Ctrl + y` pastes the text from the _kill-ring_.
- `Ctrl + w` cuts the selected text and adds it to the _kill-ring_.
- `Ctrl + k` cuts text from the cursor position to the end of the line.
- `Ctrl + x backspace` cuts text from the cursor position to the beginning of the line.
- `Alt + Z .` cuts until the first occurrence of a specified character (e.g., `.`).
- `Esc + 2 Esc + d` deletes the next two words.

## Undoing changes and Quitting
- To undo your last action, press `Ctrl + x u`.
- To quit Emacs, press `Ctrl + x Ctrl + c`.

### Searching and Replacing

- `Ctrl + s` initiates a search. Type the desired search term and press `Ctrl + s` to find the next occurrence.
- `Ctrl + r` searches for the previous occurrence.
- `Alt + Shift + %` activates the search-and-replace functionality.

### Buffers and Frames

- `Ctrl + x Ctrl + f` opens Dired (Directory Editor) file manager to open a document named `text.txt`.
- `Ctrl + x 0` removes or closes the current buffer frame.
- `Ctrl + x b` selects a different buffer.
- `Ctrl + x k` kills (closes) the current buffer.
- `Ctrl + h v` followed by a specific buffer name (e.g., `kill-ring`) opens the buffer frame.

## Advanced Features

### Zooming

- `Ctrl + x Ctrl + +` zooms in.
- `Ctrl + x Ctrl + -` zooms out.

### Splitting Windows

- `Ctrl + x 2` splits the window horizontally.
- `Ctrl + x 3` splits the window vertically.
- `Ctrl + x 0` closes the split window.
- `Ctrl + x 4 f` opens a new frame on the right side in a vertical split.
- `Ctrl + x o` tabs over to the other frame.
- `Ctrl + x Ctrl + c` closes the current window.

## Customizing Emacs Appearance with Plugins

Emacs is known for its highly customizable nature, allowing users to tailor the editor to their preferences. There are several plugins available that can enhance the appearance of Emacs and make it look better. Here are some popular plugins you can use to customize the appearance of Emacs:

### doom-themes

- **GitHub**: [https://github.com/hlissner/emacs-doom-themes](https://github.com/hlissner/emacs-doom-themes)

Doom-themes is a collection of customizable themes for Emacs that provide a sleek and modern look. It offers a variety of dark and light themes with different color schemes. To install doom-themes, you can use the package manager built into Emacs, such as `package.el` or `use-package`.

### spacemacs-theme

- **GitHub**: [https://github.com/nashamri/spacemacs-theme](https://github.com/nashamri/spacemacs-theme)

Spacemacs-theme is another popular theme for Emacs inspired by the Spacemacs editor. It offers a visually appealing and well-balanced color scheme. Similar to doom-themes, you can install spacemacs-theme using Emacs' package manager.

### all-the-icons

- **GitHub**: [https://github.com/domtronn/all-the-icons.el](https://github.com/domtronn/all-the-icons.el)

all-the-icons is a package that provides a set of scalable icons for Emacs. It enhances the visual appearance of various Emacs packages by adding icons next to file names, buffer names, and more. This package is commonly used in conjunction with other themes to provide a more visually appealing experience.

### powerline

- **GitHub**: [https://github.com/milkypostman/powerline](https://github.com/milkypostman/powerline)

Powerline is a plugin that creates a status bar with informative and customizable segments. It displays various information such as the current mode, line number, git branch, and more. Powerline enhances the overall look of Emacs by adding a stylish and informative status bar.

### rainbow-delimiters

- **GitHub**: [https://github.com/Fanael/rainbow-delimiters](https://github.com/Fanael/rainbow-delimiters)

Rainbow-delimiters is a simple but effective plugin that highlights parentheses, brackets, and braces with different colors. This plugin makes it easier to identify matching pairs and improves the readability of Lisp-like languages. It can be especially useful when working with complex nested structures.

### which-key

- **GitHub**: [https://github.com/justbur/emacs-which-key](https://github.com/justbur/emacs-which-key)

Which-key is a helpful plugin that displays key bindings and their associated commands in a popup window. It helps you discover and remember Emacs commands by showing the available options whenever you press a prefix key. Which-key improves the usability of Emacs by providing visual guidance and reducing the need to remember complex key sequences.

### beacon

- **GitHub**: [https://github.com/Malabarba/beacon](https://github.com/Malabarba/beacon)

Beacon is a small but useful plugin that highlights the cursor position whenever it jumps to a new location. It provides a visual indicator, such as a flashing beacon or a temporary background color change, to draw attention to the cursor. This can be particularly helpful when working with large documents or unfamiliar codebases.

These are just a few examples of the many plugins available for customizing the appearance of Emacs. To install these plugins, you can follow the installation instructions provided in their respective GitHub repositories. Remember to

 consult the documentation and readme files of each plugin for configuration options and additional features.

By leveraging these plugins, you can transform the appearance of Emacs to suit your preferences and create a visually appealing and personalized editing environment.

## Conclusion

Emacs provides extensive commands and shortcuts to enhance your editing experience. By mastering these commands, you can navigate through documents, edit text efficiently, search and replace, manage buffers and frames, and take advantage of advanced features such as zooming and window splitting. With time and practice, Emacs can become a powerful tool in your arsenal, enabling you to become a more productive and efficient software engineer.

_Happy Emacs editing!_

---

## References

- [GitHub Repository](https://github.com/techstackmedia/software-engineering-series/tree/08-mastering-the-basic-of-emacs-commands)
- [GNU RefCard](https://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf)
- [YouTube](https://www.youtube.com/watch?v=jPkIaqSh3cA)

---