How To Use the Emacs Editor in Linux
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 Copy link
To begin using Emacs, you first need to install it on your Linux system (you can try our Linux VPS hosting for your projects). 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 emacsInstall Emacs on Fedora
sudo dnf install emacsInstall Emacs on Arch Linux
sudo pacman -S emacsOnce installed, you can start Emacs by typing emacs in your terminal.
Basic Navigation in Emacs Copy link
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), andCtrl + p(previous line). -
Scroll through the text: Use
Alt + vto scroll up andCtrl + vto scroll down. -
Jump to the beginning or end of the line: Use
Ctrl + ato move to the beginning andCtrl + eto move to the end.
Editing and Saving Files Copy link
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 Copy link
Emacs is driven by keybindings, which can be a challenge for new users. Here are some essential commands:
- Undo the last action:
Ctrl + /orCtrl + _. - 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 Copy link
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 ~/.emacs3. The .emacs or init.el file is where you can place custom configurations. To edit this file:
emacs ~/.emacsHere, 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 Copy link
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 Copy link
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 Copy link
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 + xand typepackage-install, thenmagit.
Emacs and Version Control Copy link
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 + xand typemagit-status. -
Stage changes: Navigate to the file and press
s. -
Commit changes: Press
cthencto commit.
Tips and Tricks for Efficient Editing Copy link
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 + hto access help within Emacs. -
Customize your environment: Spend time tweaking your
.emacsfile to create a comfortable environment.
Conclusion Copy link
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.