How to Use Emacs in Linux

By
Sara Zivanov
Published:
October 9, 2025
Topics:

Emacs is a powerful and customizable text editor available on all major Linux distributions. It supports both command-line and graphical user interfaces (GUIs) and is used by developers, system administrators, and advanced users.

This tutorial will explain how to install and use Emacs in Linux, as well as its essential commands, shortcuts, and features introduced in Emacs 30.1.

How to Use Emacs in Linux

Prerequisites

How to Install Emacs in Linux

Emacs is available in the default repositories of all major Linux distributions. There are two ways to install it:

Install Emacs Using Package Manager

A distribution's package manager is the fastest and simplest way to install Emacs. This method ensures dependencies are handled automatically and maintains software compatibility with your system updates.

To accomplish this on Debian-based distributions, take the following steps:

1. Update the package list with:

sudo apt update
sudo apt update terminal output

2. Install the full Emacs package:

sudo apt install emacs
sudo apt install emacs terminal output

The command installs both the graphical and terminal versions of Emacs.

If you want to install the terminal-only version, without graphical libraries, run:

sudo apt install emacs-nox
sudo apt install emacs-nox terminal output

This option reduces resource usage.

3. Verify the installation

emacs --version
emacs --version terminal output

Other distros have slightly different installation commands. For example, to install Emac on RHEL, Fedora, AlmaLinux, and Rocky Linux, use dnf:

sudo dnf install emacs

To accomplish the same on Arch Linux, run:

sudo pacman -S emacs

After installation, verify the installation as before.

Build Emacs From Source

When you build Emacs from source, the process allows you to install the latest stable or development version. This method provides access to features not yet available in your distribution's repositories.

To accomplish this on Debian-based distributions, take the following steps:

1. Update the package list:

sudo apt update

2. Install build dependencies, the development tools and libraries required to compile Emacs:

sudo apt install build-essential texinfo libgtk-3-dev libxpm-dev libjpeg-dev libgif-dev \
libtiff-dev libgnutls28-dev libncurses-dev libxml2-dev libjansson-dev libharfbuzz-dev \
libtree-sitter-dev libgccjit-12-dev
Install build dependencies terminal output

Note: Package names may vary depending on your Linux distribution and version.

3. Download the source code with wget:

wget https://ftp.gnu.org/gnu/emacs/emacs-30.1.tar.xz
wget https://ftp.gnu.org/gnu/emacs/emacs-30.1.tar.xz terminal output

This command downloads the latest stable Emacs release from the GNU project.

4. Use tar to extract the source archive to a local directory.

tar -xf emacs-30.1.tar.xz

The command has no output.

5. Navigate into the source directory with the cd command:

cd emacs-30.1
cd emacs-30.1 terminal output

6. Configure the build environment with:

./configure
./configure terminal output

This prepares the build system and verifies all required libraries are available.

7. Compile the source code with make:

make
make terminal output

The command compiles Emacs from source. This step takes several minutes, depending on your system.

8. Install Emacs globally with:

sudo make install
sudo make install terminal output

The command installs the compiled Emacs binary to /usr/local/bin, which makes it available system-wide.

9. Verify the installation with:

emacs --version
terminal output for emacs --version

This displays Emacs 30.1 and confirms the installation was successful.

Other distributions have slightly different build dependencies:

On RHEL, Fedora, AlmaLinux, and Rocky Linux, first install development tools:

sudo dnf groupinstall "Development Tools"

Then install Emacs-specific libraries:

sudo dnf install gtk3-devel libXpm-devel giflib-devel libjpeg-turbo-devel libtiff-devel \
gnutls-devel ncurses-devel libxml2-devel jansson-devel harfbuzz-devel tree-sitter-devel \
libgccjit-devel texinfo

After the dependencies are installed, follow the same download, extract, compile, and install steps as above.

To uninstall a source-built Emacs later, run:

sudo make uninstall

How to Use Emacs in Linux

Emacs offers extensive customization, support for multiple programming languages, buffers, windows, and advanced text manipulation. It also includes built-in tutorials and help systems that make it approachable for beginners.

The following sections cover common use-case examples that demonstrate the essential operations a beginner needs.

How to Start Emacs

Start Emacs in two primary ways via the terminal or as a graphical application.

To launch Emacs in the terminal, enter:

emacs
GUI Emacs

This opens the GUI version if a display environment is available, but the terminal session remains occupied until you close Emacs.

To launch the graphical version of Emacs in the background so you can use the terminal while it runs, run:

emacs &

Alternatively, launch Emacs from the system application menu, if your desktop environment provides one.

To close the graphical Emacs window, use the menu option File and then Quit

To open Emacs in the terminal without attaching it to the terminal session, run:

emacs -nw
emacs -nw terminal output

The -nw option stands for no window and keeps Emacs running entirely in the terminal.

To exit Emacs, hold the Control key, press x, then press c.

Open a File, Create a New File, and Save

Emacs allows you to open an existing file or create a new one. In this example, we will create a sample file to be used throughout this tutorial.

Take the following steps:

1. Open a new file:

emacs ~/sample-emacs-tutorial.txt

The command launches Emacs in a GUI mode and opens the file. If the file does not exist, Emacs creates it.

2. Enter the following text into the new file:

Sample Text for Emacs Tutorial
===============================

This is the first paragraph of the sample file. It has multiple sentences for testing copy, cut, and paste commands.

Here is the second paragraph. Some words repeat: Emacs, tutorial, example, Emacs.

Line three contains numbers: 1234567890, symbols: !@#$%^&*(), and mixed CASE letters.

Finally, a short fourth paragraph to test search and navigation.
Sample text in Emacs

3. Press the Control key, press x, then press s to save the file.

4. To exit Emacs after saving, press Control key, press x, then press c.

These steps created the sample-emacs-tutorial.txt sample file, which is used for all subsequent examples.

How to Edit Text

Text editing in Emacs works similarly in both the graphical and terminal versions of the editor. You can type immediately after opening a file, and all text changes are applied in real time.

Emacs allows you to:

  • Insert text. Move the cursor to the desired location and type.
  • Delete a single character before the cursor. Press Backspace.
  • Delete the character under the cursor. Press Ctrl+d.
  • Delete the entire line from the cursor onward. Press Ctrl+k.
  • Join the current line with the next one. Press Alt+j.
  • Insert a new line below the current one. Press Enter.
  • Split a line at the cursor. Press Enter in the middle of the line.
  • Undo the last change. Press Ctrl+_.
  • Redo an undone change. Press Ctrl+g followed by Ctrl+_.

How to Mark Text

To edit or move sections of text in Emacs, mark a region first. A region is the area between the cursor position and the mark you set.

For example, in the sample file ~/sample-emacs-tutorial.txt, select the first paragraph. To do that, move the cursor to the beginning of the first line and press Ctrl+Space. Next, move the cursor to the end of the paragraph.

Select a paragraph in Emacs

The selected text becomes the active region, ready to be copied, cut, or modified.

To cancel the selection and deactivate the mark, press Ctrl+g.

Press Ctrl+x h to select the entire file. This highlights all the text in sample-emacs-tutorial.txt, which allows you to apply changes to the entire buffer.

Select all text in Emacs

Regions remain active until you perform another action, such as copying, cutting, or moving text.

How to Align Text

Emacs allows you to align text by specific characters or patterns, which helps organize code, configuration files, and tabular data for better readability.

To align text by a chosen character, follow these steps:

1. Move the cursor to the beginning of the text you want to align, press Ctrl+Space, then move the cursor to the end of the section.

2. Press Alt+x, type align-regexp, and press Enter.

3. Type the character to align by, depending on what separates the elements in your text:

  • = aligns variable assignments or configuration options.
  • : aligns keyโ€“value pairs in JSON, YAML, or dictionaries.
  • , aligns comma-separated values.
  • | aligns text formatted as tables or columns.

4. Press Enter to apply the alignment.

Note: The align command works only on structured text with repeating delimiters. It has no visible effect on plain sentences.

How to Copy, Cut, and Paste

Once you have marked a region, cut, copy, or paste it via the keyboard shortcuts.

To copy text, mark a region and press Alt+w. For example, in sample-emacs-tutorial.txt, select the first paragraph with Ctrl+Space, move the cursor to the end of the paragraph, and press Alt+w to copy it.

Move the cursor to the end of the file and press Ctrl+y to paste the copied paragraph.

Copy and paste paragraph

To cut text, select a region and press Ctrl+w. For instance, select the second paragraph and press Ctrl+w to remove it from its original position. Move the cursor to another location and press Ctrl+y to paste it.

Cut and paste a paragraph

Emacs stores copied or cut text in a temporary storage area called the kill ring. The kill ring keeps multiple previous cuts and copies, which allows you to retrieve older entries. After you paste with Ctrl+y, press Alt+y to cycle through earlier items in the kill ring.

How to Undo and Redo

Emacs lets you undo changes at any point in a file.

To undo the most recent change, press Ctrl+_ or Ctrl+x u. Each additional press of the keys undoes the previous action, step by step.

To redo a change that was undone, press Ctrl+g to cancel the current command, then press Ctrl+_ again. Emacs automatically steps forward through the changes that were previously undone.

Undo and redo operate in sequence, which allows you to backtrack multiple edits or move forward to reapply them. This lets you experiment freely without any previous content loss.

How to Navigate Text Efficiently

Emacs provides multiple commands to quickly move the cursor within a file.

To move the cursor:

  • Forward one character, press the Right Arrow key or Ctrl+f.
  • Backward one character, press the Left Arrow key or Ctrl+b.
  • Forward one word, press Alt+f.
  • Backward one word, press Alt+b.
  • To the beginning of a line, press Ctrl+a, and to move to the end of a line, press Ctrl+e.
  • Forward one paragraph, press Alt+}, and to move backward one paragraph, press Alt+{.
  • To a specific line number, press Alt+g g, then enter the line number and press Enter.

These commands enable fast and efficient navigation through long text files, particularly when combined with marking, copying, and pasting operations.

How to Use Buffers in Emacs

In Emacs, a buffer is a temporary workspace that holds the contents of a file, directory listing, or other text. Every open file has an associated buffer, and you can work with multiple buffers simultaneously without affecting the files on disk until you save them. Buffers exist in both the terminal and graphical versions.

To see a list of all open buffers, press Ctrl+x Ctrl+b. This opens the Buffer List in a separate buffer, which shows names, associated files, and status.

Emacs buffers

To create a new buffer without immediately associating it with a file, press Ctrl+x b, type a new name, and press Enter. Next, enter text and save it later with Ctrl+x Ctrl+s.

A new buffer

To switch to another buffer, press Ctrl+x b, then type the buffer name and press Enter.

Press Ctrl+x k to close the buffer. When prompted, confirm the buffer name and press the Enter key. When you close a buffer, it does not delete the file unless you explicitly save changes and then delete the file separately.

Buffers are essential in Emacs because they enable you to work on multiple files, which makes text editing more efficient.

How to Manage Windows and Frames

Emacs can display multiple views of one or more buffers using windows and frames. A window is a pane inside the Emacs interface that shows a buffer, while a frame is an entire Emacs window at the operating system level.

To split the current window horizontally, press Ctrl+x 2. This opens another pane below the current one, which shows the same buffer.

Split the current window horizontally

Another option is to split the window vertically. Press Ctrl+x 3 to create a new pane to the right of the current one.

Split the window vertically

Use Ctrl+x o to switch between open windows. To close the current window, press Ctrl+x 0. To close all other windows and keep only the current one, press Ctrl+x 1.

A new frame is a separate Emacs window at the OS level. Open it with Ctrl+x 5 2.

Open a new frame

Each frame displays different buffers and layouts. To close the current frame, press Ctrl+x 5 0.

Windows and frames make it easier to edit multiple files or different parts of the same file side by side, which improves multitasking and visibility during complex editing sessions.

How to Search Text

Emacs offers powerful search capabilities that function seamlessly in both terminal and graphical environments.

To start a search forward, press Ctrl+s, then begin typing the search term. Emacs highlights matches as you type. Press Ctrl+s again to move to the next occurrence.

To search backward, press Ctrl+r and type the search term. Press Ctrl+r again to move to the previous match.

For example, type Ctrl+s, then enter Emacs.

Search a term

Each occurrence of the word Emacs is highlighted, and pressing Ctrl+s again jumps to the next match in the file.

Press Ctrl+s (for forward) or Ctrl+r (for backward) without retyping the term to repeat the last search.

To end the search and keep the cursor at the last match, hit Enter. Cancel the search and return to the starting point with Ctrl+g.

These search shortcuts make it easy to navigate large files or locate specific terms quickly while editing.

How to Perform Find and Replace

Emacs lets you find and replace text throughout a file, either interactively or automatically.

To start the find and replace feature, press Alt+%. Emacs prompts you at the bottom of the screen to enter the text you want to replace. Type the search term and press Enter, then type the replacement text and press Enter again.

For example, to find all occurrences of Emacs and replace it with GNU Emacs, do the following:

1. Press Alt+%.

2. Enter Emacs as the search term and press Enter.

Search term

3. Type in GNU Emacs as the replacement text.

Replacement term

Emacs highlights all matches.

4. Press y to replace the current match, n to skip it, ! to replace all remaining occurrences, or q to quit. In this example, only the first match is replaced:

Replacing Emacs with GNU Emacs

To perform a replacement automatically without confirmation, Press Alt +x to open the minibuffer and type replace-string. Next, press Enter. Then enter the search and replacement terms when prompted.

To perform a replacement only in part of the file, first mark the desired region (with Ctrl+Space), then run Alt+% or Alt+x and type replace-string.

By default, Emacs searches are case-insensitive unless the search term includes uppercase letters. To force a case-sensitive search, enable the option with Alt+x and type toggle-case-fold-search and then Enter. This setting applies to all subsequent searches.

How to Use Major and Minor Modes

Emacs uses modes to tailor its behavior to various file types and editing tasks. Modes determine how text is displayed, what commands are available, and which shortcuts are active. There are two types of modes: major and minor.

A major mode defines the editing environment for a specific type of file. Only one major mode is active per buffer.

Common major modes include:

  • Text mode. Provides a basic environment for editing plain text.
  • Fundamental mode. Default mode with minimal functionality, used when no specific mode applies.
  • Markdown mode. Enables Markdown syntax highlighting and formatting shortcuts.
  • Org mode. Provides an environment for notes, to-do lists, and project planning.
  • Python mode. Provides syntax highlighting, indentation, and navigation features for Python code.
  • C mode. Enables syntax highlighting and indentation rules for C and C++ code.

To view or change the current major mode, check the status line at the bottom of the window. It shows the active mode in parentheses, such as (Text) or (Fundamental). For instance, when you open sample-emacs-tutorial.txt, Emacs automatically activates Text mode.

Text mode

To switch to another major mode manually, use Alt+x and type the mode name. For example, to enable Fundamental mode, press Alt+x and type fundamental-mode.

Fundamental mode enabled in Emacs

A minor mode adds optional features that can be toggled on or off and do not affect the major mode. Multiple minor modes can be active simultaneously.

Common minor modes include:

  • Auto Fill mode. Automatically wraps long lines of text.
  • Line Number mode. Displays line numbers in the status line.
  • Display Line Numbers mode. Shows line numbers beside each line in the buffer.
  • Flyspell mode. Checks spelling as you type.
  • Electric Pair mode. Automatically inserts matching parentheses and quotes.
  • Auto Save mode. Periodically saves changes to prevent data loss.

To enable or disable a minor mode, press Alt+x and type the mode name. For example, to enable Auto Fill mode, press Alt+x and type auto-fill-mode.

Auto fill mode enabled

Major and minor modes make Emacs highly flexible and adaptable to different workflows, from text editing to software development.

How to Access the Menu

Emacs offers a menu bar in its graphical version, which provides access to common commands and settings. The menu contains options for files, editing, search, buffers, tools, and help.

In the GUI version, the menu bar is displayed at the top of the Emacs window. Navigate it with the mouse or keyboard:

1. Click a menu name (e.g., File, Edit, Tools) to see available options.

2. Use the arrow keys to move through menu items.

3. Press Enter to execute the selected command.

The menu in GUI Emacs

In the terminal version, the menu bar is not visible, but all commands are accessible via keyboard shortcuts.

How to Access Help and Tutorials

Emacs includes extensive built-in help and tutorials to assist both beginners and advanced users. These resources are accessible via the keyboard or the menu bar.

To start the interactive tutorial, press Ctrl+h t. This opens a step-by-step guide that covers basic navigation, editing, and file operations.

Emacs tutorial

For general help about Emacs commands, press Ctrl+h k followed by a key combination to learn what that key does. For example, press Ctrl+h k Ctrl+x Ctrl+s to display information about the save command.

Tutorial for a command in Emacs

To get a list of all available commands, press Ctrl+h b.

Tutorial for all commands in Emacs

Search for help topics by name with Ctrl+h a, which lists all commands and functions that match a keyword. For example, search for undo.

Search for a command in Emacs

In the GUI version, these help options are also accessible through the Help menu in the menu bar.

Help menu in Emacs

How to Exit Emacs

Exiting Emacs is straightforward but slightly different based on whether you are using the terminal or the GUI version.

To quit Emacs in the terminal version, press Ctrl+x Ctrl+c.

If you have unsaved changes in any buffer, Emacs prompts you to save them before exiting. Respond with:

  • y to save changes and exit.
  • n to exit without saving.
  • C-g to cancel the exit and return to Emacs.

In the GUI version, exit via the menu. Click File and then Quit. If there are unsaved changes, the same prompts appear.

Emacs Shortcuts

Emacs relies on keyboard shortcuts to perform common editing tasks efficiently. These shortcuts allow you to navigate text, manipulate buffers, and execute commands quickly without relying on menus.

The following table highlights the most commonly used shortcuts and their functions:

ShortcutExplanation
Ctrl+x Ctrl+sSaves the current buffer.
Ctrl+x Ctrl+wSaves the current buffer with a new name.
Ctrl+x kCloses the current buffer.
Ctrl+x bSwitches to another buffer.
Ctrl+x oSwitches to another window.
Ctrl+gCancels the current command or prompt.
Ctrl+SpaceSets the mark to begin selecting a region.
Alt+wCopies the selected region to the kill ring.
Ctrl+wCuts the selected region to the kill ring.
Ctrl+yPastes the most recently cut or copied text
Ctrl+/_Ctrl+zUndoes the last action.
Alt+%Query-replaces text interactively.
Ctrl+sSearches forward incrementally.
Ctrl+rSearches backward incrementally.
Ctrl+aMoves the cursor to the beginning of the line.
Ctrl+eMoves the cursor to the end of the line.
Alt+fMoves the cursor forward one word.
Alt+bMoves the cursor backward one word.
Alt+<Moves the cursor to the beginning of the buffer.
Alt+>Moves the cursor to the end of the buffer.
Ctrl+x 1Closes other windows and keeps only the current one.
Ctrl+x 2Splits the current window horizontally.
Ctrl+x 3Splits the current window vertically.
Ctrl+h tOpens the Emacs tutorial.
Ctrl+h kDescribes a key and its function.
Ctrl+h fDescribes a function by name.
Ctrl+h vDescribes a variable by name.
Ctrl+x uUndoes the last change (alternative undo command).
Ctrl+lCenters the current line in the window.

New Emacs 30.1 Overview

Emacs 30.1 introduces several enhancements and new features that improve usability, performance, and overall editing experience. These updates make it easier to navigate, edit, and customize your workflow compared to previous versions.

Key highlights of Emacs 30.1 include:

  • Native compilation. Emacs Lisp code is compiled to native machine code by default, which improves startup and execution speed.
  • User interface updates. Modern fonts, icons, and themes are better supported in the GUI version.
  • Multi-monitor support. Multi-display setups are now handled more effectively.
  • Tree-Sitter major modes. New modes improve syntax highlighting and parsing for languages such as Elixir, HEEx, HTML, Lua, and PHP.
  • EditorConfig support. Maintains consistent coding styles across editors.
  • Expanded Language Server Protocol (LSP) support. Integration with programming tools is improved for a smoother development experience.
  • New commands and enhancements. Editing and navigation commands have been updated and improved.
  • Package management updates. Installation and management of Emacs packages is streamlined.
  • Performance improvements. Large files and buffers load faster, and editing is more responsive.
  • Multi-editor support. Multiple tabs and flexible window splitting improve workflow management.
  • Soft word wrap. Line wrapping can be applied automatically to all documents.
  • Help system enhancements. Documentation is easier to access, and search functions are improved.
  • Accessibility improvements. Screen reader support and keyboard navigation are enhanced.

These changes make Emacs 30.1 a more responsive, modern, and feature-rich editor that still retains the flexibility and extensibility.

Conclusion

This tutorial explained how to install and use the Emacs text editor. The text presents practical use-case examples and an elaborate table with commonly used keyboard shortcuts for editing. It also highlights the best features of the new Emacs 30.01.

Next, learn about the best text editors for different operating systems.

Was this article helpful?
YesNo