Methods to Install and Use Emacs Text Editor in Ubuntu

4 Methods to Install and Use Emacs Text Editor in Ubuntu

Emacs is a versatile, customizable, and extensible text editor that goes beyond the traditional text editor. Emacs created a new attitude of the text editor for Linux users. By providing rich and advanced features, in addition to text editing, Emacs is used as a complete and integrated platform for various tasks such as programming, coding, writing, debugging, project planning, development, email client, etc.

Although most Linux users prefer Vi and Nano editors, Emacs has been able to be more than a simple text editor due to its flexible nature by expanding its features and functionality, which has led to increased popularity among users. If you have compared the famous Vim vs. Nano editors and compared their strengths and weaknesses with features of Emacs, you will notice that Emacs not only offers the advantages of Nano and Vi editors but also creates a new experience of using a text editor with its unique features.

This article aims to teach how to install and use the Emacs text editor in Ubuntu Linux. Whether you are a Linux user or a developer, the Emacs text editor is an ideal choice to meet your needs effectively. By reading this article, it is very easy to install and use the Emacs text editor in Ubuntu because in this article, you will learn how to install the Emacs editor in Ubuntu in different ways, and you will learn the shortcut keys for optimal use of the Emacs editor.

Is it time to use Emacs as a text editor?

Emacs is an open source, cross-platform, user-friendly, and customizable text editor that, in addition to supporting unique features for text editing, such as multiple editing modes, full Unicode support for scripts, text manipulation tools, and integration with numerous external tools (like git and shell), provides the integrated development platform for coding and programming and other functions.

Support for advanced features has led to Emacs becoming a tool beyond an editor and having a wider range of functionality. Text manipulation tools Emacs’ keyboard-centric approach significantly improves and speeds up coding and writing. In addition, to provide specialized functions for different tasks, it allows switching between different modes such as text editing, programming, email, and more.

Emacs open source editor, with its flexible and customizable nature, allows users to customize their platform based on their specific needs and preferences and create a personal editing environment. Emacs is customizable using its built-in programming language (Emacs Lisp) and allows users to create functions, keybindings, and even entirely new modes for specific tasks.

The use of the Emacs editor is not limited to the Linux operating system, it is compatible with all operating systems, such as Windows and macOS, and fortunately, Emacs, with its advanced features, is free. As we mentioned, Emacs is extensive and used for development and programming in addition to text editing. Emacs provides an integrated development platform with features like syntax highlighting, code completion, version control integration, and more to improve development and programming.

Also, by supporting a wide range of packages and programs, it has added support for various functions such as project management, language-specific coding, and file management to its facilities. So, if you are looking for a valuable and versatile editor tool, Emacs text editor is the best choice.

Emacs editor installation prerequisite in Ubuntu Linux

Tutorials to Install Emacs in Ubuntu through different methods

To install Emacs in Ubuntu, you can use different methods that are taught in this article. You can install the Emacs text editor and take advantage of its features through whatever ways you are most comfortable with.

Method 1: Installing Emacs via APT (Advanced Package Tool)

The Linux terminal is a flexible and powerful platform that makes it easier and more efficient for users to apply settings in Linux. For this reason, professional Linux users are not interested in leaving the Linux terminal environment and prefer the Linux terminal platform to apply any settings. In Debian-based distributions like Ubuntu, the APT package manager makes installing the Emacs text editor much easier and faster. One way to install the Emacs editor is to use the APT package manager in the Ubuntu terminal. To install the Emacs text editor in Ubuntu Linux, after you open a terminal window on the Ubuntu system (via Ctrl+Alt+T), follow the steps below:

1. Check if Emacs is already installed on your Ubuntu system

Before installing, it is better to ensure the Emacs text editor is not already installed on the Ubuntu system. So run the following command to Launch Emacs:

emacs

If the text editor starts and you receive a welcome message, you do not need to install the Emacs editor because it is already installed on your Ubuntu system. But if you see the following message in the previous command’s output, you must install the Emacs editor through the methods we teach in this article.

Output:

Command 'emacs' not found, but can be installed with:
sudo apt  install e3           # version 1:2.82+dfsg-2
sudo apt  install emacs-gtk    # version 1:27.1+1-3ubuntu5
sudo apt  install emacs-lucid  # version 1:27.1+1-3ubuntu5
sudo apt  install emacs-nox    # version 1:27.1+1-3ubuntu5
sudo apt  install jove         # version 4.17.3.6-2
See 'snap info emacs' for additional versions.

2. Enable the universe repository

Since sometimes the universe repository is not active on the Ubuntu server, enable this repository on the Ubuntu server through the following command:

sudo add-apt-repository universe

3. Update the Package List

Update the list of packages installed in Ubuntu via the following command:

sudo apt update

4. Install Emacs in ubuntu

After updating the package list in the Ubuntu Linux system, enter the following command to install the Emacs editor through the APT package manager:

sudo apt install emacs

To install the graphical version of the Emacs editor, use the following command:

sudo apt install emacs-gtk

By executing the previous commands, the graphical version of Emacs, along with its dependencies, will be downloaded and installed.

If you get a prompt to confirm the installation during installation, confirm the Emacs editor installation process by typing Y and pressing Enter.

5. Verify the Emacs Installation

After you have installed Emacs, run the following command to ensure a successful installation of the Emacs text editor:

emacs --version

The output of this command will display the version of the Emacs editor that you just installed, and by receiving the version of the Emacs editor, there is no room to worry about Emacs not being installed.

Method 2: Installing Emacs ubuntu using Snap Package

In addition to the APT package manager, Ubuntu also supports the Snap package manager to improve package management, so you can use the features of the Snap package manager to install the Emacs text editor. Snap command line tool provides the latest software while maintaining the integrity and stability of your work environment. Follow the instructions below to install the Emacs editor via Snap Package:

1. Update the Snapd service

Open the terminal of the Ubuntu system and run the following command to update the Snap package:

sudo snap install core

2. Install Emacs via Snap

To install Emacs classic edition using the Snap package, run the following command:

sudo snap install emacs --classic

The Emacs editor is given the necessary permissions for full system access by running the-- classicflag.

To get the Emacs bleeding-edge version using the Snap package, run the following command:

sudo snap install emacs --edge --classic

3. Verify the installation

After the installation is complete, check the version of the Emacs editor to ensure the installation of this editor on the Ubuntu system:

emacs --version

Or

emacs -nw

Method 3: Installing Emacs via third-party PPA repositories in ubuntu

As you know, Linux distributions are compatible with third-party PPA repositories, and you can download and install the most up-to-date and unique software through PPA on the Ubuntu system. If the official Ubuntu repository does not support the software, you can install the specific software using PPA, but if the Ubuntu official repository supports the Emacs editor, installing through the official Ubuntu repository is an ideal way to use the stable version of the Emacs editor. Some users prefer PPA repositories to install software, assuming they use the latest version of the software without encountering bugs. This part of the article teaches how to install Emacs through PPA. Enter the Ubuntu terminal and follow the steps below:

1. Add the Emacs PPA

To install the latest version of the Emacs editor via the PPA repository, you must first add the Emacs PPA repository to your Ubuntu system:

sudo add-apt-repository ppa:kelleyk/emacs

By running the previous command, the stable PPA repository will be added to the list of repositories on your Ubuntu system that supports the latest version of the Emacs editor along with essential Emacs dependencies such as mailutils, systemd, and xwidgets.

You may be prompted to confirm adding the PPA; if prompted for a password, enter your password and then type Enter.

2. Update the packages installed in the Ubuntu system

For other repositories and packages installed in Ubuntu to be aware of the addition of the Emacs PPA repository, update the list of packages to the latest versions by running the following command in Ubuntu:

sudo apt update

3. Install Emacs using PPA

Now that you have equipped your Ubuntu system with the PPA repository, you can install the Emacs editor in Ubuntu via PPA by running a command:

sudo apt install emacs

If you want to install a specific version of the Emacs editor, you can download and install a particular version of Emacs through the PPA repository by running the following command and specifying the version number of Emacs:

sudo apt install emacsVERSION

Finally, after completing the Emacs editor’s installation, ensure the Emacs editor is installed by running theemacs --versioncommand.

Method 4: Installing Emacs by compiling it from source code

Since Emacs is open source, Linux users who want to use the latest version of Emacs and want to customize their editor according to their preferences prefer to install Emacs by compiling it from source code. This method is not for experienced and professional people, you can easily install Emacs from Source by reading this part of the article. For this purpose, Follow the steps below:

1. Install dependencies

The most basic step for Installing Emacs by compiling it from source code is to ensure that the necessary build tools and libraries are installed, so run the following command:

sudo apt update
sudo apt install build-essential automake autoconf libtool texinfo libx11-dev libxpm-dev libjpeg-dev libpng-dev libgif-dev libtiff-dev libgtk2.0-dev libncurses-dev libgnutls28-dev

2. Download the Emacs source code

To install the latest stable version of the Emacs editor, download the relevant source code from the GNU Emacs website. For this purpose, if you are in the Ubuntu terminal environment, enter the following command:

wget https://ftp.gnu.org/gnu/emacs/emacs-<version>.tar.gz

Replace <version> with the desired version of Emacs.

3. Extract the downloaded source code

After downloading the related source code of the Emacs editor, you must extract it from the folder where the source code file is stored through the following command:

tar -xzvf emacs-<version>.tar.gz

Replace <version> with the desired Emacs version number.

4. Install Emacs by compiling it

Go to the directory containing the extracted Emacs source and run the following command:

cd emacs-<version>

Then, use the following command to configure build:

./configure

Compile the emacs text editor by running the following command:

make

Finally, it’s time to install the Emacs editor:

sudo make install

Congratulations, you have successfully installed Emacs from source on your Ubuntu system.

To ensure the successful installation of Emacs, check the Emacs version as in the previous methods.

Getting started with Emacs in ubuntu

Welcome to the exciting world of Emacs. We have decided to introduce you to basic Emacs commands, including opening, editing, saving, and closing files, and how to customize Emacs and its advanced features such as modes, keyboard shortcuts, etc.

Launching Emacs interface

To start the Emacs text editor, just run the following command in the Ubuntu terminal:

emacs

starting Emacs editor

Displaying the Emacs welcome message informs you that you have entered the wonderful world of Emacs. When Emacs is launched, an empty buffer (a channel for interaction with the editor) is provided to the user for typing.

Opening the file in Emacs

When Emacs is launched, use the “Tab” key to place the cursor on the “Visit New File” link and press Enter. You can also open a new file in the Emacs editor by pressing “Ctrl+x“and “Ctrl+f“.

Then, Emacs will prompt you to specify the path to the desired file and the name of the file. After typing the file’s path and the file’s name, press Enter to open the desired file.

Press “Ctrl+x” and “Ctrl+r” to open files you recently opened and edited.

To open, navigate, and manage files and directories through Emacs, you can activate Dired mode, the built-in file manager in Emacs; for this purpose, you must press “Ctrl+x d” keys simultaneously.

As a result, the textual content of the file is displayed in a large editing space called the main buffer, and you can edit files in this space.

When you edit a file using Emacs, this editor loads a copy of the text documents in the main buffer, and with the changes you make in the editing space, the content in the main buffer is also changed, but these changes are not applied to the main file unless manually save your changes in the Emacs editor. Therefore, the changes you make in the file’s content will be applied to the main buffer, but to apply changes to the main file, you must save the file manually after making changes.

Access to Emacs features

After Emacs is started, the Emacs menu at the top of the screen provides different functions, including Calendar, calculator, Programmable calculator, Spell checking, Compare and merge files, Version control, Search files using grep, Encrypting and decrypting document, Searching a directory, Send and read e-mails, etc.

Since you are in the terminal environment and the mouse cannot open the Emacs editor’s menu options, you must use keyboard shortcuts to navigate through the menu options. So, press the “F10” key to access the Emacs menu options.

A window containing a list of keys to access the menu items will open. Using the shortcut keys, you can navigate among the items and features of the Emacs editor. To exit the menu and its items, press the “ESC” key three times to return to the main buffer.

In addition to the Emacs menu, the highlighted text bar at the bottom of the screen, which is known as the “status bar” or the “mode line,” depending on the active mode of Emacs, displays texts such as the current file name, Current editing mode, and Current cursor location.

Navigation in the contents of the file

The Emacs text editor allows the user to navigate through the long texts of the file through keyboard shortcuts. In the following, we provide a list of the most common ways to scroll through the contents of a file:

  • Press the “Ctrl+n” keys to move to the next line.
  • Use the “Ctrl+p” keys to move to the previous line.
  • Through “Ctrl+a,” move to the beginning of the line.
  • By using “Ctrl+e,” Move to the end of the line.
  • Press the “Ctrl+f” to move forward one character.
  • Press “Ctrl+b” to Move backward one character.
  • Use the “Alt+e” to Move to the end of the sentence.
  • Use the “Alt+a” to move to the beginning of the sentence.
  • Through “Alt+f,” move forward one word.
  • Through “Alt+b,” move backward one word.
  • By using the “Ctrl+v,” Scroll one screen forward.
  • By using the “Alt+v,” Scroll one screen backward.
  • Use the “Ctrl+l” to center the cursor line in the window.
  • Use the “Ctrl+x” b to Switch buffer.
  • Press “Alt+shift+<” to move to the first line of the file.
  • Press “Alt+Shift+>” to Move to the last line of the file.

As you can see, to interact with the Emacs editor, the Ctrl and Alt keys, which are known as meta keys, are mostly used. In the workspace of the Emacs editor, you exit from a command or a specific menu through the “ESC” key, and the Enter key is used for the Return function. Also, the Ctrl+g keys cancel the operation, which is an alternative ESC key method.

Editing text using Emacs

The main goal of the Emacs editor design is to provide the most advanced features for editing the text and content of a file in Linux. In this section, you will learn basic editing using keyboard shortcuts in the Emacs text editor.

Text highlighting: activate the Mark setting by pressing the “Ctrl+Spacebar” or “Ctrl+@” keys and specify the desired text in the mini buffer. You can also highlight text by pressing the Shift key and moving the cursor through the arrow keys on the keyboard.

Copy text: Press the “Meta+w” keys to copy specific text. (Meta can include Alt, ESC, Option, or Edit keys.)

Cut the text: Use the “Ctrl+k” keys to cut the text. This command cuts the text from the current cursor position to the end of the line. You can also cut the desired text by pressing “Ctrl+w” after specifying the beginning of the text.

Paste the text: Press the “Ctrl+y” keys to paste the copied text or the last cut text at the cursor position.

Undo:Ctrl+/” or “Ctrl+x u” keys are used to undo.

Redo: To redo the last undo, press “Ctrl+g” followed by “Ctrl+_.”

Find text: In the Emacs editor, there are two types of search, forward search and backward search; search forward for text by pressing the “Ctrl+s” buttons, and for backward search, use the “Ctrl+r” keys.

Replace phrase or word: Use “Alt+% (M+%)” to replace text and follow the mini buffer prompts. (After placing the word or phrase you want in query replace, use the “y” key to replace the current match found, the “n” key to skip to the next match, and the “q” key to exit without replacements.)

How to save changes in the file

After editing the file’s content, you must save your changes manually. To save your edits and changes in the file’s content, press “Ctrl+x“, followed by “Ctrl+s.” If this file has not been saved before, you must specify the file name. If you want to save the file with a different name and in another location, “Ctrl+x” followed by “Ctrl+w” will save the file with the changes applied.

How to exit out of Emacs

To close the current buffer (file) without exiting the emacs editor, just press “Ctrl+x k” keys. But if you want to quit the emacs editor, press “Ctrl+x” then “Ctrl+c.”

Experience of different Emacs modes

One of the reasons for the popularity of Emacs is the extensive functionality of this editor, with support for different modes such as c-mode, java-mode, python-mode, perl-mode, text-mode, and shell-script-mode, etc. Emacs text editor offers two major and minor modes for different purposes. The features of Emacs make it more than just a text editor. To perform advanced tasks such as writing Java or Python code, you can change the modes supported in Emacs and benefit from the features of the integrated development environment. Major mode offers features such as language-specific menu options, automatic interfacing with debuggers and compilers, formatting, etc. To use the major mode of the Emacs editor and write Python code in the Emacs development environment, enter the following command in the terminal:

cd ~

emacs hello.py

As a result, Emacs starts in Python mode by recognizing the file extension. In the main buffer of the Emacs editor, type the following command:

print "hello world!\n"

Changing to the Emacs Python mode

As you can see, you entered Python mode; the mode change in the Emacs editor is evident from the status line above the mini buffer. Also, A new tab under the title Python is displayed in the main menu.

To change the major mode to other modes, save the buffer, press “Meta+x” to bring up the mini buffer, and enter different modes for the switch.

You can experience the Minor mode of the Emacs editor, which offers more special and specialized features. Several minor modes can be active simultaneously, and sometimes some minor modes are active by default. By calling minor modes, you can activate inactive modes and vice versa.

For example, to enter auto-fill-mode, which is a mode to customize the length of a line of text based on the number of characters to break the line and wrap to the next line, just press the “Meta+x” keys.

Auto-save-mode, linum-mode, line-number-mode, column-number-mode, menu-bar-mode, and overwrite-mode are examples of minor modes that you can use their features to achieve your goals.

FAQ

The customization of Emacs is one of the outstanding advantages of this editor. To customize Emacs, create a configuration file named ".emacs" or "init.el" in the home directory and add custom functions.

Various factors cause this problem, such as a misbehaving package or a resource-intensive operation. To solve this problem, stop the current operation with the "Ctrl + g" keys or exit and restart the Emacs editor.

As the error indicates, this problem is caused by running out of memory, usually displayed when editing large files or resource-intensive operations. To solve this problem, close unused buffers and Adjust Emacs' memory settings. Increasing the heap size by running the " --heap" command-line option is another helpful solution to this error.

Conclusion

Emacs editor is an advanced technology that changes users’ attitudes to text editors and introduces them to a new and different world. Emacs is not just a simple text editor but covers various functions such as project planning, programming and coding, debugging, etc. The world of Emacs editor is vast and exciting, you need time and experience to learn and understand it deeply; but after you understand it completely, we are sure that you will not replace any editor with Emacs because there is currently no competitor for the Emacs text editor in terms of productivity and efficiency.

Emacs supports a tutorial ecosystem that is very useful for learning; enter the Emacs menu by pressing the F10 button and select the help option through the keyboard arrow keys to get more information about Emacs. You can also benefit from the Emacs tutorial by pressing “Ctrl+h” then “t.” Pressing “Ctrl+p” helps you to detect bugs. In addition, pressing the “Ctrl+r” buttons provide the Emacs Manual for reading.

The Emacs Wiki is a valuable resource for learning more about the Emacs editor. Thank you for reading this article to the end. We hope that the presented content was helpful to you.

Leave a Reply

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