Sign In
Sign In

How To Use the Emacs Editor in Linux

How To Use the Emacs Editor in Linux
Hostman Team
Technical writer
Linux
19.08.2024
Reading time: 6 min

Emacs is a powerful and extensible text editor that has been a staple in the development community for decades. Created by Richard Stallman as part of the GNU Project, Emacs offers a wide array of features that make it more than just a text editor. Whether you are writing code, editing text files, or managing tasks, Emacs can be customized to suit your needs. 

Emacs is often described as an "operating system disguised as an editor." This might sound exaggerated, but once you delve into its vast ecosystem of commands, extensions, and customizations, you'll understand why. Emacs can be daunting for beginners due to its non-intuitive interface and keybindings, but with practice, it becomes a powerful tool for text editing, programming, and much more.

This tutorial will walk you through the essentials of using Emacs, from installation to advanced features, ensuring you gain a solid foundation to build upon.

Installing Emacs on Linux

To begin using Emacs, you first need to install it on your Linux system. Emacs is available in the repositories of most major Linux distributions, making the installation process straightforward.

Install Emacs on Ubuntu/Debian

sudo apt update
sudo apt install emacs

Install Emacs on Fedora

sudo dnf install emacs

Install Emacs on Arch Linux

sudo pacman -S emacs

Once installed, you can start Emacs by typing emacs in your terminal.

Basic Navigation in Emacs

Emacs may feel unfamiliar if you're used to other text editors. Here's a quick guide to basic navigation:

  • Move the cursor: Use the arrow keys or Ctrl + f (forward), Ctrl + b (backward), Ctrl + n (next line), and Ctrl + p (previous line).

  • Scroll through the text: Use Alt + v to scroll up and Ctrl + v to scroll down.

  • Jump to the beginning or end of the line: Use Ctrl + a to move to the beginning and Ctrl + e to move to the end.

Editing and Saving Files

Editing text in Emacs is straightforward once you get the hang of the keybindings. Here’s how you can start.

Opening a File

To open a file in Emacs, use the Ctrl + x Ctrl + f command, then type the file path and press Enter.

Editing Text

You can start typing immediately to insert text.

To delete characters:

  • Delete the character under the cursor: Press Ctrl + d.
  • Delete the character before the cursor: Press Backspace.

Saving a File

To save the file you’re working on:

  • Save the current buffer: Use Ctrl + x Ctrl + s.
  • Save the buffer as a new file: Use Ctrl + x Ctrl + w, then type the new file name and press Enter.

Using Emacs Commands and Keybindings

Emacs is driven by keybindings, which can be a challenge for new users. Here are some essential commands:

  • Undo the last action: Ctrl + / or Ctrl + _.
  • Cut the selected text: Ctrl + w.
  • Copy the selected text: Alt + w.
  • Paste the text: Ctrl + y.

These keybindings allow you to perform text operations efficiently. As you become more comfortable with Emacs, you can explore more advanced keybindings and commands.

Customizing Emacs

One of Emacs's strengths is its customizability. You can modify almost every aspect of Emacs to better suit your workflow. You can create and edit the .emacs or init.el file. This is how it is done in Ubuntu.

1. Check if the .emacs or init.el file exists in your home directory:

ls -a ~ | grep -E '\.emacs|init\.el'

If nothing is returned, the file doesn't exist.

2. If the file doesn't exist, you can create it manually. Decide whether you want to use the .emacs file or the init.el file (located in the .emacs.d directory). Either will work, but init.el is more common in modern configurations.

To create .emacs:

touch ~/.emacs

3. The .emacs or init.el file is where you can place custom configurations. To edit this file:

emacs ~/.emacs

Here, you can add commands to change Emacs's behavior. For example, to enable line numbers by default, add the following line:

(global-display-line-numbers-mode t)

Installing Emacs Packages

Emacs has a vast ecosystem of packages that extend its functionality. To install packages, you’ll first need to enable the package manager:

1. Open the .emacs file.

2. Add the following lines to initialize the package system:

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

3. Save and restart Emacs.

4. Use Alt + x and type package-install, then the name of the package you want to install.

Working with Multiple Files and Buffers

Emacs allows you to open and work with multiple files simultaneously using buffers.

Switching Between Buffers

  • Switch to another buffer: Use Ctrl + x b, type the buffer name, and press Enter.
  • List all buffers: Use Ctrl + x Ctrl + b.

Splitting the Window

  • Split the window horizontally: Ctrl + x 2.
  • Split the window vertically: Ctrl + x 3.
  • Close the split: Ctrl + x 0.

Advanced Features: Macros and Extensions

Emacs shines when it comes to automation and extensibility.

Creating and Using Macros

Macros in Emacs allow you to record a series of commands and replay them.

  • Start recording a macro: Ctrl + x (.
  • Stop recording: Ctrl + x ).
  • Replay the macro: Ctrl + x e.

Using Extensions

Extensions, or packages, enhance Emacs's functionality. For instance, Magit is a popular Git interface for Emacs.

  • Install Magit: Use Alt + x and type package-install, then magit.

Emacs and Version Control

Emacs integrates well with version control systems like Git.

Using Magit

Magit is a powerful Git interface for Emacs. After installing it, you can:

  • Open Magit: Use Alt + x and type magit-status.

  • Stage changes: Navigate to the file and press s.

  • Commit changes: Press c then c to commit.

Tips and Tricks for Efficient Editing

Here are some tips to help you become more efficient in Emacs:

  • Learn the keybindings: Familiarize yourself with the essential keybindings to improve your speed.

  • Use Emacs’s help system: Use Ctrl + h to access help within Emacs.

  • Customize your environment: Spend time tweaking your .emacs file to create a comfortable environment.

Conclusion

Emacs is a versatile and powerful tool that can greatly enhance your productivity once you overcome its initial learning curve. By following this tutorial, you should now have a solid understanding of how to install, navigate, and customize Emacs to suit your needs. Whether you are coding, writing, or managing tasks, Emacs is a tool that can grow with you, offering limitless possibilities for customization and efficiency.

Start exploring Emacs today, and discover how this seemingly simple text editor can become an indispensable part of your workflow.

This tutorial provides a comprehensive guide for beginners on how to use the Emacs editor in Linux. By covering everything from basic navigation to advanced features, users can develop a strong foundation to leverage Emacs's full potential.

Linux
19.08.2024
Reading time: 6 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us