Mastering Basic Emacs Commands

Mastering Basic Emacs Commands

Mastering Emacs: Boosting Productivity with Powerful Commands and Shortcuts

·

8 min read

Play this article

GitHub Logo

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. 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.

  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:

Get-ExecutionPolicy
  • If the output is "Restricted", enter one of the commands below:
Set-ExecutionPolicy AllSigned

or

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):
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:
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. Once you find the Emacs package, execute the following command:
choco install emacs
  • Open any terminal of your choice and type emacs to start using Emacs:
emacs

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

emacs <filename>

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

emacs README.md

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

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:

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. The command emacs -Q -nw 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

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

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

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

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

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

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

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