A Beginner’s Guide to Using Emacs Text Editor in Linux
GNU Emacs is one of the oldest text editors. It was created by Richard Stallman, the founder of the Free Software movement and GNU project. He is also the creator of the GNU C compiler and Debugger.
Emacs is a versatile and highly configurable text editor with hundreds of keybindings and commands. With numerous configuration settings, it’s arguably one of the text editors with a steep learning curve, so most developers, system administrators, and regular Linux users prefer Nano or Vim editors.
Despite the complexity of learning the ins and outs of Emacs, it’s not entirely hostile to beginners, and with the right learning strategy and guidance, you’ll be comfortable in no time.
In this guide, we will hold your hand and walk you through the Emacs editor. By the end of this guide, you will have a decent amount of knowledge to get started with using the Emacs editor.
How to Install Emacs in Linux
Before we can start using Emacs. Let’s first see how to install it on various Linux distributions.
For Debian-based systems, you can easily install Emacs from the default repositories using the apt package manager.
$ sudo apt update $ sudo apt install emacs -y
For RedHat-based systems such as Rocky Linux, Alma Linux, and Fedora, use the dnf package manager to install Emacs from the default repositories.
$ sudo dnf update $ sudo dnf install emacs -y
To install Emacs on other Linux distributions, use the following appropriate command.
sudo emerge -a sys-apps/emacs [On Gentoo Linux] sudo apk add emacs [On Alpine Linux] sudo pacman -S emacs [On Arch Linux] sudo zypper install emacs [On OpenSUSE] sudo pkg install emacs [On FreeBSD]
That aside, let’s now get started with the Emacs editor.
Getting Started with Emacs Editor
Emacs editor offers both GUI and CLI versions as separate entities. However, when invoked from the terminal, the modern Emacs version provides a GUI editor complete with menus, buttons, windows, and even a separate editor.
To launch the Emacs editor, simply run the command:
$ emacs
The Emacs GUI window will pop up as shown. Below the Emacs logo are some helpful links that you can click to get more information about how to use Emacs.
From the top, we have a menu bar that consists of standard menu options such as “File“, “Edit” and “Options“. Below it is the Toolbar with menu options for creating a new file, reading an existing file from a buffer, copying, pasting, and cutting text to mention a few.
You can hover over the toolbar icons with a mouse to see what task or operations they execute when clicked on.
Let’s now look at the remaining GUI sections.
Let’s briefly discuss each section:
- Frame – This would otherwise be called a window in normal GUI applications. In Emacs, however, a window is referred to as a Frame. The default window in Emacs is the “Welcome” window in which we can see the Emacs logo and an overview of useful links and other sections.
- Buffer – A buffer is a canvas or area that holds the text you are typing or editing. It simply holds the text contained in a file. You can have multiple buffers within t. Additional buffers can be created and “killed” at will.
- Blinking Cursor – Also known as the ‘Point‘, the blinking black cursor acts like a cursor where you can insert text.
- Status Bar – This displays information about the active buffer and the blinking point. Each bar has one status bar.
- Mini Buffer – This is the white space below the status bar. It occasionally displays status messages (for example if you need to save the file with new changes). In addition, this is the section where you enter and execute Emacs commands.
Opening a New File in Emacs
To open a new file in Emacs on the command line, type `emacs` followed by the file name and hit ENTER. For example, to open a file called file1.txt, run the command
emacs file1.txt
This opens a new window, with a buffer containing the file’s contents.
From here you can continue editing or working on your file.
Emacs Keyboard Shortcuts
In Emacs, most tasks are executed with the help of keyboard shortcuts. Thus, to have an easier time, you need to have a solid command of commonly used keyboard shortcuts.
In Emacs, the “Ctrl” and “ESC” keys are critical in keyboard shortcuts.
The “ESC” key (sometimes the “ALT” key) is the Key is commonly called the “Meta” key, which is abbreviated as just "M"
. For example, M-q
implies pressing the “Meta” and "q"
keys on the keyboard.
The “Ctrl” Key is abbreviated as just "C"
. For instance, C-g
implies pressing the “Ctrl” and "g"
keys on the Keyboard.
Some keyboard shortcuts require you to repeat them twice. For example, “C-h C-h"
means pressing the “Ctrl” and “h” key combinations twice.
Now let’s go over some of the commonly used Keyboard shortcuts.
- C-h C-h – This populates the help manual.
- C-g – Quits the Emacs Window.
- C-x right arrow key – Right-cycles through buffers.
- C-x left arrow key – Left-cycles through buffers.
- C-x C-f – Opens an existing file in Emacs. You will be prompted for the path of the file in the mini-buffer.
- C-x C-s – Saves the changes made to a file.
- C-x s – The command saves all the files.
Open/Save and Close a Buffer in Emacs
To open an existing file and load its contents on the Emacs buffer, use the C-x C-f
shortcut combination.
This action opens a prompt at the mini buffer which prompts you for the name of the file. So specify the file path and hit ENTER.
Here is the sample file called “file1.txt” opened in Emacs.
Once you are done working on the file, save the changes using the C-x C-s
keyboard combination.
To save the file under a different name, use the C-x C-w
keyboard shortcut. This is the equivalent of the “Save as” functionality. The operation prompts you to specify the file name.
In this example, we have saved the same file as ‘jamie.txt’.
To close the buffer, use the C-x k
shortcut. This prompts you for the buffer to kill. Once specified, hit ENTER to exit.
If you make changes before exiting the buffer, you will be prompted to save them. To save the changes, type ‘yes’ in the mini-buffer and hit ENTER. Otherwise, type ‘NO’.
How to Cut / Copy / Paste Text in Emacs
Just like command-line editors like Nano or Vim, Emacs has its version of ‘cut‘, ‘copy‘, and ‘paste‘ operations. These operations are referred to by different names.
The kill operation is analogous to “cut”, and “yank” is analogous to “paste”. But before you start the copy / cut / paste operations, you need to, first, select a region of text. This section of text is known as the `region`.
To select the region or area of text to be copied or cut, place the pointer at the beginning of your desired region. Next, press the “C-spacebar” shortcut combination on the keyboard. At the mini-buffer, you’ll see a message written “Mark set”.
Next, use the arrow key to select the text. In this case, I’m using the arrow forward key to select the first line. The selected text will be highlighted as shown.
To copy the text, use the M-w
shortcut (ESC + W) and to cut or “kill” a region, press C-w.
Next, move to the region you want to “yank” or paste the text and hit C-y
to paste it.
To undo changes, use the combination C-x u
.
How to Search Text in Emacs
To search for a string or word invoke the “C-s”
shortcut for a forward search. To search backward, use the “C-r”
shortcut. Next, type the search word or query and keep pressing ‘C-s’
or ‘C-r’
.
In the example shown, the search query is the word ‘emacs’.
The search query will be highlighted as shown.
When you get to the end of the search result, you will see a “Failing I-search” message on the mini-buffer.
In case of a backward search, you’ll see a “I-search backward” notification on the mini-buffer.
If you reach the end of the search pattern, the cursor heads to the start of the file and wraps over to the beginning of the search results. You’ll get the “Overwrapped I-search” notification on the mini-buffer.
How to Find and Replace Text in Emacs
Sometimes, you might want to replace a specific word(s) in your text file. With Emacs, use the ALT + SHIFT + 5 combination. Enter the search item in the mini-buffer.
For example, here, ‘Emacs’ is the search query. Then press ENTER.
Next, provide the string or word you want to replace it with. In this instance, ‘Nano’ is the word to replace ‘Emacs’. Then hit ENTER.
The first instance of the search pattern will be displayed.
From here, you can take the following actions:
- type space or
'y'
to replace one match. Press ‘Delete‘ or'n'
to skip to the next query. - type
'!'
to replace all remaining matches in the buffer.
Text Wrapping in Emacs
By default, Emacs does not automatically wrap text. This might present a challenge when reading large documents since Emacs treats a paragraph as a single line.
To address this issue, simply run M-q
to wrap text into readable paragraphs as shown.
Commonly Used Emacsa Keyboard Shortcuts
Here is a summary of the commonly used keyboard shortcuts:
Windows
- C-x 0: Close or exit from an active window.
- C-x 1: Close or exit from all windows except the active window.
- C-x 2: Split the currently active window horizontally.
- C-x 3: Split the currently active window vertically.
- C-x o: Change active window to next window.
Buffers
- C-h C-h: Open the help menu.
- C-x b: Switch buffers.
- C-x right: Move to the right buffer in a cyclical motion.
- C-x left: Move to the left buffer in a cyclical motion.
- C-x k: Kill buffer.
- C-x C-c: Exit Emacs.
Navigation
- C-up: Moves the one paragraph up.
- C-down: Moves the one paragraph down.
- C-right: Moves cursor one word to the right.
- C-left: Moves cursor one word to the left.
- home: Moves the pointer to the beginning of the line.
- end: Moves the pointer to the end of the line.
Searching Text
- C-s: Perform a forward search.
- C-r: Perform a backward search.
- M-%: query replace (‘space’ to replace, ‘n’ to skip,
‘!’
to replace all). - M-q: Carry out text wrapping.
Opening / Saving Files
- C-x C-f: Open a file.
- C-x C-s: Save a file.
- C-x C-w: This is equivalent to the ‘Save File as’ function.
- C-_: Undo operation.
Copy and Paste
- C-w: Cut text in the region.
- M-w: Copy text in the region.
- C-y: Paste text that has been cut or copied.
Head over to the Emacs GitHub page for more keyboard shortcuts.
Conclusion
Emacs is a lightweight and highly customizable text editor that offers numerous customization capabilities. However, due to its long learning curve, developers, system administrators, and operation teams nowadays prefer the Nano or Vim editors which are far easier to learn and use.
This guide has covered the basics of Emacs and touched base on how to get started using the editor along with commonly used keyboard shortcuts.