Using Emacs on Linux and macOS – emac Commands examples

Emacs stands as one of the most long-standing and adaptable text editors available. The inception of GNU Emacs dates back to 1984, renowned for its robust and extensive editing capabilities. It is highly customizable and can be extended with different modes, making it useful as an Integrated Development Environment (IDE) for programming languages such as Java, C, and Python.

Emacs is primarily designed for text manipulation, although it possesses the ability to format and produce documents similar to a word processor. It achieves this by collaborating with external tools like LaTeX, Ghostscript, and web browsers. In addition, Emacs furnishes commands for altering and exhibiting semantic text components like words, sentences, paragraphs, and even source code elements such as functions. This text editor is categorized as a real-time display editor, wherein modifications are promptly shown on the screen as they happen. Renowned for its significant extensibility, adaptability, and personalization options, Emacs boasts a substantial and dedicated user base.

Here are some features of Emacs that make it unique:

  • Editing Modes: Emacs editing mode are content-aware, the have been integrated with various mode including syntax coloring for different file types.
  • Built-in documentation: Emacs offers a complete documentation which comes in-built. It also includes a tutorial for new users, which guides them during their first time with Emacs.
  • Packaging system: With Emacs, you can download and install extensions since you it is equipped with a packaging system at your disposal.
  • Enhanced functionality: Emacs is equipped with more than just text editing capabilities. It also has a project planner, mail service, a news reader, calendar, an IRC client and much more.
  • Highly customizable: Emacs is a highly customizable text editor. This can be achieved by using the Emacs Lips code, or a graphical interface. Emacs also offers full Unicode support.

Installing Emacs on Linux and macOS

In this article, am going to show you how to install Emacs on Linux and macOS and how to use it. For Linux, I’ll touch on the popular Linux distributions such as Debian based, RHEL, Arch and Alpine. Let’s dive into it.

Install emacs on Debian-based distributions

Open a terminal and run the following command to install Emacs:

sudo apt update && sudo apt install emacs

Install emacs on RHEL-based distributions

For RHEL-based distributions, run the following commands:

# Fedora / RHEL 8+
sudo dnf install emacs

#CentOS / RHEL 7
sudo yum install emacs

Install emacs on Arch-based distributions

Open the terminal and run the following command:

sudo pacman -S emacs

Install emacs on Alpine Linux

Open your terminal and run the following command:

sudo apk add emacs

Install on macOS using Homebrew

Open your terminal and the command below to install Homebrew, if you haven’t already:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing Homebrew successfully, run the following command to install Emacs:

brew install emacs

Using Emacs on Linux

1. Starting Emacs

To start using Emacs, execute the command emacs in your terminal:

$ emacs

The text-based version of Emacs starts with an empty buffer waiting, waiting for input from the user. The program displays a welcome message when Emacs is started without specifying a file to open.

To create a new file, you have to move your cursor all the way to the link labeled “Visit New File” and the press enter. Use the arrow keys to move the cursor. When you press enter, a prompt appears at the end of the terminal, where you can input the name of the file you’d like to create:

Enter a file name, for example newfile.txt and press enter. Emacs will create an empty file named newfile.txt and displays an editing window where you can type or view the content of your file.

When using Emacs to edit an existing file, a copy of that file is loaded into memory and displayed in the buffer area. The buffer is the working space where all the changes made in the editing area are applied, while the original file on disk remains unchanged. Emacs may auto-save in the background, but the changes are only written to the disk when the document is manually saved.

The same applies to new files, where all changes are made in the buffer until it is saved. The main editing space in Emacs provides a view of the buffer. The status bar or mode line, displayed near the bottom of the screen, provides additional information about the editing session.

The status bar shows the name of the current file, position of the cursor, current editing mode, and the status of the file.

2. Emacs Menus

When dealing with text-based programs, the functionality of a mouse click goes out the window. What I mean is, you cannot access a menu by dropping it down as you would in a GUI-based program. Text-based programs usually have some designated keys for accessing such menus.

For Emacs, you can access the menus by pressing the F10 key, which opens another window above the main buffer. The window displays a list of options starting with the File Menu options. Scroll through the options using the UP and DOWN arrow keys.

You can move between the different menu options using the RIGHT and LEFT arrow keys. You can access the HELP menu by scrolling to the Menu labeled ‘Help’. From the HELP menu, you can access all the relevant options such as the Emacs Tutorial:

3. Emacs Command Keys

In Emacs, command functions usually involve a combination of two or three keys. The most common key is the CTRL key, which is usually specified as ‘C’, for instance, when a command is specified as C-x and C-c, it means that you press CTRL+x followed by CTRL+c . Similarly, a command such as C-h t means that you press CTRL+H then followed by t.

The ALT and ESC keys are referred to as meta keys, which are also used in combination with other keys to issue commands in Emacs. In macOS, the OPTION key is used in place of ALT. The ENTER key is shown as RET, short for RETURN, and the ESC key is shown as E.

4. Basic Commands

Now that you are familiar with the key functions of Emacs, here is a cheat sheet for the basic commands:

Key-StrokeFunctionCommand-Line
C-x C-fOpen a fileM-x find-file
C-x iInsert another file at the current cursor positionM-x insert-file
C-x C-sSave current fileM-x save-buffer
C-x C-wSave current file with another nameM-x write-file
C-x C-fOpen an additional fileM-x find file
Emacs basic commands

We had already opened a new file called newfile.txt, now lets insert another file called myfile.py to this file using the command C-x i. When you execute C-x i, you will be prompted to enter the name of the file you would like to insert.

After typing in the name of the file and pressing enter, the contents of that file will be inserted into the current buffer at the current position of the cursor:

To save the changes we just made to the current file, we use the C-x C-s command and we can see the output at the bottom the window, showing that changes have been written to the file:

5. Moving among buffers

As we defined earlier, a buffer is the workspace/editor where we can view and edit the contents of a file. In Emacs, we can move between the open buffers, delete a buffer and list available buffers.

Here is the cheat sheet:

Key-StrokesFunctionCommand-Line
C-x bMove to the previous bufferM-x switch-to-buffer
C-x C-bDisplay buffer listM-x list-buffers
C-x kDelete current bufferM-x kill-buffer
Buffer commands

From the current buffer that we were operating in, we can switch to previous buffer, which is the empty buffer before we inserted myfile.py to the current buffer:

When you execute C-x b command, you are prompted whether you’d like to proceed and move to the previous buffer. You can type either ‘Yes‘ or ‘No‘:

When you type ‘Yes’ and press enter:

6. Using Emacs Windows

In Emacs, windows are the areas where buffers are displayed. Each window can show a different buffer, allowing you to work on multiple files simultaneously. Windows can be split horizontally or vertically, creating multiple side-by-side or stacked windows.

Here is a cheat sheet to guide you through navigating the windows:

Key-StrokesFunctionCommand-Line
C-x 2Create two windowsM-x split-window-vertically
C-x oMove to the other windowM-x other-window
C-x 0Delete the current windowM-x delete-window
C-x 1Delete all the other windows except the current oneM-x delete-other-windows
Emacs window commands

To split the current window, use the C-x 2 command. You can execute it multiple times if you want to create more than two windows:

To switch to the other window from the current one, use C-x o command. The switch is shown by the change in position of the cursor from the first window, to the second:

You can ahead and try out the rest of the window commands and see the outcome. So an going to delete all other windows except for the first one, since am still using it:

7. Moving around the buffer

In Emacs, moving around the buffer is achieved using the following commands:

Key-StrokesFunctionCommand-Line
M-<Move to the beginning of the bufferM-x beginning-of-buffer
M->Move to the end of the bufferM-x end-of-buffer
C-fMove forward one characterM-x forward-char
C-bMove backward one characterM-x backward-char
C-nMove down one lineM-x next-line
C-pMove up one lineM-x previous-line
M-fMove forward one wordM-x forward-word
M-bMove backward one wordM-x backward-word
C-eMove to the end of a lineM-x end-of-line
C-a Move to the beginning of a lineM-x beginning-of-line
Buffer navigation commands

To be able to work fast and efficiently using Emacs, you need to have these commands on your fingertips. Let’s start with moving to the beginning and end of the buffer. We use M-< to move to the beginning and M-> to move to the end:

Keep in mind that ‘M‘ is short for the ALT key:

Instead of moving by characters, you can also move word by word, either forward or backward. The command M-f is used to move forward by one word and M-b is used for moving backward by one line:

All of the buffer operations can be performed using the arrow keys. It’s only a matter of preference.

8. Editing commands

Emacs provides a wide range of functions and features for text editing, allowing users to navigate and edit text efficiently and effectively. 

Cutting(deleting) Text

Here is the cheat sheet for Cutting and deleting texts:

Key-StrokesFunctionCommand-Line
C-dDelete characterM-x delete-char
DEL/BACKSPACEDelete previous characterM-x delete-backward-char
M-dDelete wordM-x kill-word
M-DEL/M-BACKSPACEDelete previous wordM-x backward-kill-word
C-kDelete the entire lineM-x kill-line
Editing Commmands

Most of these operations are very straightforward and don’t need any demonstrations, so lets move on to marking the text you’ll be deleting, moving or copying.

Marking/Highlighting Text

Highlighting is a common operation which is useful in specifying the part of the text you’ll be performing changes to. When you want to delete, move, or copy a large chunk of the text, you will highlight it first. So here is the cheat sheet for marking text in Emacs:

Key-StrokesFunctionCommand-Line
C-@ or C-SPACEBARMark the beginning of selectionM-x set-mark-command
C-wDelete the marked regionM-x kill-region
M-w or C-INSERTCopy a marked regionM-x kill-ring-save
C-yPaste a copied regionM-x yank
Editing Commands
  • 1. To mark the beginning of a selection, use CTRL+@ or CTRL+SPACEBAR then start selecting using the RIGHT and LEFT arrow keys:
  • 2. Once you have selected a region, you can delete it using the BACKSPACE key or the CTRL+w command.
  • 3. You can also copy the selection using ALT+w and paste it using CTRL+y.

Searching Text

When searching for text in Emacs, there are several commands and techniques you can use. Here is a summary of the search operations and the commands involved:

Key-StrokesFunctionCommand-Line
C-sIncremental Search forwardM-x isearch-forward
C-r Incremental search backwardM-x isearch-backward
C-s ENTERNon-incremental search*None
C-g Cancel incremental searchM-x keyboard-quit
M-s wWord search*None
Search Commands

To initiate an incremental search in Emacs, press CTRL+s and input the text to search in the prompt at the bottom of the buffer. The text to search is highlighted starting from the current cursor position going forward:

You can also perform an incremental search backward. A backward incremental searches the whole buffer backwards starting from the cursor position going backwards. Here the cursor was positioned at the end of the buffer:

For a non-incremental search, use CTRL+s followed by ENTER. A non-incremental search performs the search and stops after the first occurrence of the text. Also, the search only occurs after you press the ENTER key.

To perform a word search, press ALT+s followed by w :

9. Entering Modes

In Emacs, modes are sets of definitions that customize Emacs behavior in useful ways. There are two types of modes: major modes and minor modes. Major modes are used for editing or interacting with a particular kind of text, while minor modes provide features that users can turn on and off while editing. Each buffer has exactly one major mode at a time. Major modes are language or task-specific, while minor modes are smaller and less specific utilities that cut across many tasks.

Entering Major Mode

Major modes provide specialized facilities for working on a particular file type, such as a C source file, or a particular type of non-file buffer, such as a shell buffer.  Emacs has specialized major modes for many programming languages, and these modes typically specify the syntax of the language.

Here is a list of all the available Major modes in Emacs:

  1. python-mode
  2. c-mode
  3. text-mode
  4. java-mode
  5. shell-script-mode
  6. html-mode
  7. perl-mode

To enter any of the major modes, execute ALT+x and then input the mode for example , python-mode to enter and press ENTER. To demonstrate, we will use the file we already have and since it is a python file, let’s try to enter python mode. Once we enter the mode, the syntax highlighting for python will be applied when the buffer recognizes the text.

You can see that from the status bar, the buffer mode changes from text-mode to python-mode.

Entering Minor Mode

Minor modes in Emacs offer more specific features than major modes. Unlike major modes, there can be multiple minor modes in effect at any one time. Some minor modes are enabled by default, while others are not. If a minor mode is already on, calling it will switch it off, and if it is off, it will be switched back on.

Minor modes are like switches that can be turned on or off independently of the major mode. Most minor modes are disabled by default, but some are enabled by default. Most buffer-local minor modes indicate in the mode line when they are enabled, while global minor modes affect everything you do in the Emacs session, in all buffers.

These are some examples of the minor modes in Emacs:

  1. auto-save-mode: This turns on the feature that periodically auto-saves content to the main buffer.
  2. column-number-mode: Shows the current position if the cursor in the status bar.
  3. line-number-mode: Turns on the display of the current line number in the status bar.
  4. overwrite-mode: Turns on the feature that overwrites text on the right side of the cursor as you type.
  5. menu-bar-mode: Switches the main menu on and off.
  6. linum-mode: Turns on the display of line numbers along the left edge of the window.

Recommended Linux Books  to read:

Conclusion

Emacs is a versatile and powerful text editor that has been in development for over 40 years. It is cross-platform, open-source, and can be used for a wide range of tasks, from editing configuration files to programming and document processing. Understanding how to use this powerful editor can greatly improve your productivity on Linux. 

More guides to check out:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Let’s clarify the differences between merge requests (commonly called pull requests in GitHub), releases, release candidates (RCs), tags, and branches […]

Kind (which in full means “Kubernetes IN Docker”), is a command line tool that enables you to run Kubernetes clusters […]

Are you looking for an easy way to migrate packages from one cPanel server to a new cPanel server? In […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.