$ sudo dnf update
$ sudo dnf install emacs -y
emacs file1.txt
Next, move to the region you want to “yank” or paste the text and hit C-y
to paste it.
Head over to the Emacs GitHub page for more keyboard shortcuts.
How to Install Emacs in Linux
Contents
- 1 How to Install Emacs in Linux
- 2 Getting Started with Emacs Editor
- 3 Opening a New File in Emacs
- 4 Emacs Keyboard Shortcuts
- 5 Open/Save and Close a Buffer in Emacs
- 6 How to Cut / Copy / Paste Text in Emacs
- 7 How to Find and Replace Text in Emacs
- 8 Text Wrapping in Emacs
- 9 Commonly Used Emacsa Keyboard Shortcuts
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.
In case of a backward search, you’ll see a “I-search backward” notification on the mini-buffer.
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.
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.
Getting Started with Emacs Editor
Once you are done working on the file, save the changes using the C-x C-s
keyboard combination.
To launch the Emacs editor, simply run the command:
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.
Now let’s go over some of the commonly used Keyboard shortcuts.
Here is the sample file called “file1.txt” opened in Emacs.
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.
In Emacs, the “Ctrl” and “ESC” keys are critical in keyboard shortcuts.
- 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
From here you can continue editing or working on your file.
From here, you can take the following actions:
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.
Emacs Keyboard Shortcuts
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.
$ sudo apt update
$ sudo apt install emacs -y
Some keyboard shortcuts require you to repeat them twice. For example, “C-h C-h"
means pressing the “Ctrl” and “h” key combinations twice.
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.
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.
For Debian-based systems, you can easily install Emacs from the default repositories using the apt package manager.
- 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
The “Ctrl” Key is abbreviated as just "C"
. For instance, C-g
implies pressing the “Ctrl” and "g"
keys on the Keyboard.
Before we can start using Emacs. Let’s first see how to install it on various Linux distributions.
To copy the text, use the M-w
shortcut (ESC + W) and to cut or “kill” a region, press C-w.
In the example shown, the search query is the word ‘emacs’.
The first instance of the search pattern will be displayed.
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.
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`.
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 Cut / Copy / Paste Text 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
To open an existing file and load its contents on the Emacs buffer, use the C-x C-f
shortcut combination.
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.
For example, here, ‘Emacs’ is the search query. Then press ENTER.
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.
Let’s now look at the remaining GUI sections.
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]
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.
$ emacs
You can hover over the toolbar icons with a mouse to see what task or operations they execute when clicked on.
When you get to the end of the search result, you will see a “Failing I-search” message on the mini-buffer.
The search query will be highlighted as shown.
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 Find and Replace Text in Emacs
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.
In this example, we have saved the same file as ‘jamie.txt’.
To address this issue, simply run M-q
to wrap text into readable paragraphs as shown.
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’
.
To undo changes, use the combination C-x u
.
- 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
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.
Let’s briefly discuss each section:
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.
- 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.
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”.
Conclusion
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.
To close the buffer, use the C-x k
shortcut. This prompts you for the buffer to kill. Once specified, hit ENTER to exit.