Emacs Tutorial: A Comprehensive Guide for Beginners
Introduction to Emacs
Emacs is a highly customizable and extensible text editor that is renowned for its powerful features and its use as an integrated development environment (IDE). Developed initially in the 1970s by Richard Stallman, Emacs has grown into a versatile tool used by programmers, writers, and many other professionals.
This tutorial will guide you through the basics of Emacs, covering its installation, basic usage, key bindings, customization, and more.
Installation
On Linux
Most Linux distributions include Emacs in their package repositories. You can install it using the package manager of your distribution.
For Debian-based distributions (like Ubuntu):
sudo apt-get update
sudo apt-get install emacs
For Red Hat-based distributions (like Fedora):
sudo dnf install emacs
On macOS
You can install Emacs using Homebrew:
brew install emacs
On Windows
You can download the latest version of Emacs from the GNU Emacs website: https://www.gnu.org/software/emacs/download.html. Follow the installation instructions provided.
Getting Started
Starting Emacs
To start Emacs, simply type `emacs` in your terminal or open it from your applications menu if you are using a GUI.
The Emacs Interface
When you first open Emacs, you will see the *scratch* buffer. This is a temporary workspace where you can write and evaluate Lisp expressions.
Basic Commands
Emacs commands are often combinations of keys. The most common prefixes are:
- `C-` stands for the `Ctrl` key.
- `M-` stands for the `Meta` key, which is usually the `Alt` key or `Esc` key.
Here are some basic commands to get you started:
- `C-x C-f`: Open a file.
- `C-x C-s`: Save a file.
- `C-x C-c`: Close Emacs.
- `C-g`: Cancel the current command.
Basic Navigation
Moving the Cursor
- `C-f`: Move forward one character.
- `C-b`: Move backward one character.
- `C-n`: Move to the next line.
- `C-p`: Move to the previous line.
- `M-f`: Move forward one word.
- `M-b`: Move backward one word.
- `C-a`: Move to the beginning of the line.
- `C-e`: Move to the end of the line.
Scrolling
- `C-v`: Scroll down.
- `M-v`: Scroll up.
Searching
- `C-s`: Start an incremental search.
- `C-r`: Start a reverse incremental search.
Editing Text
Basic Editing Commands
- `C-d`: Delete the character under the cursor.
- `M-d`: Delete the next word.
- `C-k`: Kill (cut) the rest of the line.
- `C-y`: Yank (paste) the last killed text.
- `M-y`: Cycle through the kill ring after yanking.
Undo and Redo
- `C-/` or `C-x u`: Undo.
- `C-g C-/`: Redo (after undoing).
Copy and Paste
- `M-w`: Copy the selected text.
- `C-w`: Cut the selected text.
- `C-y`: Paste the text.
Buffers and Windows
Working with Buffers
- `C-x b`: Switch to another buffer.
- `C-x C-b`: List all buffers.
- `C-x k`: Kill (close) the current buffer.
Working with Windows
- `C-x 2`: Split the window horizontally.
- `C-x 3`: Split the window vertically.
- `C-x o`: Switch to the other window.
- `C-x 1`: Close all other windows.
Customization
Editing the Emacs Configuration File
Emacs uses a configuration file called `.emacs` or `init.el` located in your home directory. You can customize Emacs by adding Lisp code to this file.
For example, to change the theme:
(load-theme 'wombat t)
Installing Packages
Emacs has a built-in package manager called `package.el`. To install a package, you first need to add package repositories. Add the following to your `.emacs` or `init.el`:
(require 'package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("gnu" . "https://elpa.gnu.org/packages/")))
(package-initialize)
To install a package, use the following commands:
- `M-x package-refresh-contents`: Refresh the package list.
- `M-x package-install RET package-name RET`: Install the desired package.
For example, to install the popular `magit` package:
M-x package-install RET magit RET
Using Emacs for Programming
Syntax Highlighting
Emacs provides syntax highlighting for many programming languages out of the box. When you open a file with a recognized extension, Emacs will automatically enter the appropriate mode.
Autocompletion
To enable autocompletion, you can install the `company-mode` package:
M-x package-install RET company RET
(add-hook 'after-init-hook 'global-company-mode)
Version Control
Emacs integrates with version control systems like Git through packages like `magit`:
M-x package-install RET magit RET
(global-set-key (kbd "C-x g") 'magit-status)
Running Code
You can run code within Emacs using various modes. For example, in Python mode:
- `C-c C-c`: Execute the current Python script.
Help and Documentation
Built-in Help
Emacs has extensive built-in help:
- `C-h t`: Open the built-in Emacs tutorial.
- `C-h k`: Describe the function bound to a key.
- `C-h f`: Describe a function.
- `C-h v`: Describe a variable.
- `C-h a`: Search for functions and commands by keyword.
Conclusion
Emacs is a powerful and versatile tool that can greatly enhance your productivity. While it has a steep learning curve, the effort invested in learning Emacs pays off with increased efficiency and customization. Use this tutorial as a starting point, and explore the vast ecosystem of packages and configurations to tailor Emacs to your needs.