FUNDAMENTALS A Complete Guide for Beginners
Emacs stands for Editor MACroS, is a text-editing application that has been around for 40 years in Linux. It is known for its extensibility and versatility, offering a wide range of features beyond basic text editing. It is not just a text editor, but can also function as an IDE (Integrated Development Environment) and even as a platform for creating and running applications.
So, this article will provide a detailed overview of the Emacs text editor in Linux. It will first discuss the basic ins and outs of “Emacs” mentioning its definition and features. Subsequently, it will mention how to use Emacs to work with files. Moreover, it will discuss different modes of Emacs in Linux and comparison with several popular text editors.
What is Emacs in Linux?
Emacs is a feature-efficient text-editing tool that runs on UNIX, LINUX, BSD, Mac, and even Windows systems. It was first developed in the MIT AI lab in 1976. There are many variants of Emacs in Linux and GNU Emacs is the most widely used Emacs. Now, GNU Emacs is not just an editor but has the functionality of more than an IDE. It has numerous built-in commands and macros and great flexibility overall.
Emacs is one of the oldest text editors released in the world. It is known for its features and functionalities. Some of the key features of “Emacs” that keep it apart from other text editors are as follows:
- Content-aware Editing: Emacs is smart to understand the type of text written inside the program. For instance, it can easily distinguish between programming languages and other text. In addition, it gives syntax coloring for highlighting different file types.
- Complete Unicode Support: Emacs has complete Unicode support (allows for using any known type of symbols or characters). Therefore Emacs is a widely used text editor (from research papers, and app development to basic coding, etc.).
- Customizable Using LISP Code: Emacs has a built-in LISP interpreter allowing for text editor customization. This has resulted in its popularity among professionals even if it’s quite an old text editor.
- More than a Text Editor: Emacs was originally a text editor, but now it has become more than an IDE. Like an IDE, it can compile, run, and test codes written in renowned programming languages. Moreover, it can function as a file explorer, debugger, calendar, and even mail application.
- Lightweight: Emacs is a very lightweight application available in both the GUI and terminal-based versions. It does not contain any bulky form, resulting in minimal startup time with efficient resource usage.
How to install Emacs Text Editor?
To install the Emacs text editor, open the terminal and run the command mentioned below:
sudo apt install emacs
The “Emacs” text editor is going through the installation process as you see in the below image:
Note: You can run the command sudo apt update
to update the package cache before the installation and after the installation of Emacs. This will notify the system of the latest changes. Moreover, you can use the command sudo apt install emacs -y
for installation. This directly completes installation without prompting for pressing “y” later.
Finally, after installation, run the command below to verify whether “Emacs” has been installed successfully:
emacs - -version
The Emacs version 27.1 has successfully been installed in the Ubuntu system.
How to Use Emacs Text Editor in Linux?
To use the Emacs text editor is to open a file in the editor, edit, and save the file. This section will discuss the 3 basic steps of using the “Emacs” text editor in Linux. It will mention how to start with the basic “Emacs” interface, access files, and save them after editing and relevant processing:
1. Getting Started with the Emacs Interface
Opening the Emacs editor will get you started with its interface. So, to open the emacs text editor, simply run the following command in the terminal:
emacs
Emacs opens with its GUI-based interface with a welcome message as shown in the image.
Alternatively, to open the emacs text editor in the terminal without launching its Graphical User Interface (GUI), execute the emacs command with -nw options like below:
emacs -nw
2. Open and Edit Files Using Emacs in Linux
To open a file in Emacs, use the command emacs with the following syntax:
emacs -option <file_name>
This command will open the file file_name (with contents) with its main buffer if it’s already present in the system. Otherwise, a new file is created with a buffer. The buffer is the large editing space for typing text or viewing the file contents.
For instance, running the command emacs newDoc.txt
creates the file newDoc.txt if it doesn’t exist in the system:
Now, in the text buffer area, you can add contents to edit the file “newDoc.txt”:
Note: Apart from opening a file in the way, shown above, you can press CTRL+X and CTR+F to create a new file and enter the file name once prompted at the end of the terminal. Moreover, you can use a mouse or the TAB button to move and select the View New File link and press ENTER (if you use “TAB”) to create a new file or even open an extant file. For this, enter the name of the file when prompted. In addition, you can use the main status bar to do so with a more GUI-based approach.
3. Save the File and Exit in Emacs
To save the file, press CTRL+X and then CTRL+S. Subsequently, pressing CTRL+X and CTRL+C will exit the Emacs text editor: “Emacs” notifies after saving a file using CTRL+X and CTRL+S. You can also explore its GUI-based approaches using the main status bar with the mouse.
Common Keyboard Shortcuts of Emacs in Linux
Emacs has many keyboard shortcuts– also known as Keybindings. These are essential for efficient text editing and performing tasks quickly. The emphasis is to navigate and edit text within Emacs without reaching for the mouse. This results in increased productivity.
Some of the most common Keyboard shortcuts (Keyboard bindings) of Emacs are mentioned below:
Serial No. | Keyboard Shortcuts | Functions |
---|---|---|
1. | CTRL+X CTRL+F | Opens a file or creates a new file. |
2. | CTRL+X CTRL+S | Saves the file. |
3. | CTRL+F | Moves one character forward. |
4. | CTRL+B | Moves one character backward. |
5. | ALT+F | Moves one word forward. |
6. | ALT+B | Moves one word backward. |
7. | CTRL+D | Deletes a character forward. |
8. | CTRL+S | Searches forward. |
9. | ALT+W | Copies a selected text. |
10. | CTRL+W | Cuts the selected text. |
12. | CTRL+Y | Pastes selected text. |
13. | CTRL+X T | Opens the Emacs tutorial. |
14. | ALT+$ | Checks the spelling of the current word. |
15. | CTRL+X CTRL+C | Saves and Exits the Emacs text editor. |
Note: To learn more about the Keyboard shortcuts of Emacs in Linux, read this cheatsheet: Emacs Cheat Sheet [Free PDF Downlaod]
Modes in Emacs Text Editor
Modes in Emacs is a fundamental concept that adds to its functionality while performing specific tasks. For this reason, it is widely adopted by the UNIX community. These are plugins that define the behavior of Emacs based on the different activities executing in it. So, depending on the mode, Emacs can behave as a text processor to write and edit files. Moreover, users can adopt it to work with Java, Python, C code, etc. In addition, different modes of Emacs leverage the working with Bash scripts, spreadsheets, XML editing, or even working with version control systems.
There are 2 kinds of modes in Emacs: 1. Major Mode and 2. Minor Mode.
1. Major Mode in Emacs
The major mode leverages the use of Emacs to edit particular types of text. For instance, it can function as an Integrated Development Environment (IDE) for programming and scripting language. So, this mode offers features like indentation, formatting, compiling, debugging, a language-centric menu for text processing and manipulation, etc.
As a demonstration, run the following command in the home directory first:
emacs hello.cpp
Here, Emacs will automatically understand the file extension (cpp) and open a text buffer to work with C++ mode like the image below:
The above image states the working with C++ language using the “Emacs” editor in the major mode. Here, the command emacs hello.cpp opens a new buffer for C++ while the main file menu has an entry for the language C++ (marked in the image with a box). After writing the code to print “hello world”, pressing CTRL+X and CTRL+S saves the file.
2. Minor Mode in Emacs
Another popular mode in Emacs is the minor mode which provides additional functionality tied up to the major mode. There are multiple minor modes of which some are enabled by default and some are not. Some of the minor modes are mentioned below:
- auto-fill-mode: Automatically wraps lines of text as you type.
- auto-save-mode: A property that periodically saves contents of the main buffer in the background.
- line-number-mode: Displays the current line number in the status bar.
- column-number-mode: Shows the current cursor position in the status bar.
Comparison of Emacs with Other Text Editors in Linux
There are many Text editors available and many of them provide specific advantages over Emacs in Linux. But “Emacs” also has other advantages and flexibility. So, you just need to use them to find your cup of tea. Some of the comparisons of Emacs with 3 other text editors (text processing tools)are as follows:
A. Emacs vs Vim in Linux
Vim is another popular text editor and people love it for its lightweight front. That’s because it was mainly developed from Vi. Vim stands for Vi IMproved. Vi was designed for slower Terminals and focused on the minimum number of keystrokes.
Here are some comparisons between Emacs and Vim:
- Editing Modes: Vim has 2 different editing modes. Command mode and Insert In command mode, you can type and execute commands. In Insert mode, you can add text to your file. But Emacs has content-aware mode. That means it doesn’t need any other modes. It understands the document type on its own.
- Lightweight: Vim was mainly developed to work on slower Terminals. As a result, Vim is quite lightweight and easy to use. It doesn’t need many system resources. This is great for working in older systems. Emacs on the other hand may not be as light as Vim, but still lightweight compared to many other available text editors. Vim requires less RAM to run.
- Learning Curve: Emacs has an easier learning curve for its easier and more natural interface compared to Vim. Vim on the other hand has different editing modes, making it harder for beginners to learn.
- Customization: Emacs has more customization than Vim. You can use different programming languages to customize Emacs according to your needs. Vim also provides good enough customization capabilities. So, it’s not as large of a deal for most programmers or developers.
B. Emacs vs VS Code in Linux
VS Code is a modern text editor and a full-blown IDE. Presently, It is one of the most popular editors nowadays that many developers love to use for its intuitive interface. Some comparisons between Emacs and VS Code are mentioned below:
- Lightweight: Emacs is more lightweight compared to VS Code. VS Code implements many functionalities and a complete IDE that may not run well on older systems. Additionally, It may take a lot of system resources. But Emacs is more optimized and can run on many systems quite easily regardless of the age of that system
- Customization: Emacs is considered to be more customizable than VS Code. VS Code is not bad in any comparison, but in Emacs, you have KeyBindings and Emacs Lisp codes to customize pretty much anything.
- Navigation: Emacs can be used for file navigation and mail checking and many other things we do in our Operating system. But, VS Code has some limitations in that case. Yes, some level of communication in the file manager is possible, but not even close to what is possible with Emacs.
C. Emacs vs Nano in Linux
Nano is a built-in simple text editor in Linux. It was built for simplicity so that pretty much anyone can start using it with little to no knowledge beforehand. But, to do that it also had to sacrifice some functionality. Here are some comparisons between Nano and Emacs:
- Simplicity: Nano is very simple. As a result, there is pretty much no learning curve. As a result, there are simple shortcuts displayed at the bottom and that’s it. So, beginners will need less time to get used to Nano.
- Features: As Nano was built for simplicity, it had to sacrifice many functionalities as well. So, It doesn’t have many features to compete against a well-rounded text editor like Emacs.
- Customization: Nano doesn’t have many customizations. Its main use can be editing some config files or doing some small edits to a text file. But, for any type of coding and development, Nano is simply not as powerful and its customization capabilities are also very limited.
Conclusion
Emacs in Linux is a popular text editor and many professionals use it as a daily driver. Certainly, it has immense functionality over other text editors or IDEs and has more customization than many heavy IDEs as well. Besides, It has a lot of Keybindings to improve functionality as well. But, that doesn’t mean you need to memorize all of them. Just try it out and practice. You will get used to it and love it after the steep learning curve.
People Also Ask
How is Emacs used in Linux?
Emacs is used as a text editor and much more. It is a popular choice for programmers, especially for Lisp languages like Clojure. Along with that, it can also be used for simple writing notes, emails to complex documents with tables, figures, and equations. Moreover, emacs can be used for basic web browsing with packages like EWW or W3M.
Why is Emacs important?
Emacs is considered important for several reasons. Some of these are:
- Highly extensible and customizable.
- Works beyond as a text editor. It can function as a code editor, integrated development environment (IDE), task manager, note-taking tool (Org Mode), email client (Gnus), and more.
- Provides advanced text editing features such as syntax highlighting, auto-indentation, search and replace with regular expressions, etc.
- Relies on keyboard shortcuts for most operations, providing a fast and efficient way to navigate and perform tasks.
- Emacs Lisp, the programming language used for Emacs customization, allows users to treat code as data and data as code.
What is the emacs command?
The emacs command is a Linux command that launches the Emacs text editor from the command line. The command is simply: emacs
. When you run this command in a terminal, it starts Emacs, and you will be presented with the Emacs interface where you can begin editing text. Additionally, you can specify a file as an argument to open that file directly in Emacs by: emacs filename
.
Is Emacs a terminal?
No, Emacs is not a terminal itself, rather, it is a text editor that can run in both terminal (text-based) and graphical (GUI) environments. The mode in which Emacs runs depends on how it is invoked.
Can Emacs run code?
Yes, Emacs can run code, and it can function as a powerful Integrated Development Environment (IDE) for various programming languages.
How to install the Emacs text editor in Linux?
To install the emacs text editor in Linux, for example, in your Ubuntu system, open the terminal and type the command sudo apt install emacs
. Upon running the command, APT will download and install the emacs package and its dependencies making the text editor available to use in your Ubuntu system. In addition, if you are using a Red Hat-based Linux distro such as CentOS or Fedora you can run the command sudo yum install emacs
.
What are keyboard bindings in Emacs?
The keyboard bindings refer to the association between key combinations and specific commands or functions within the emacs text editor. By default, the keybindings revolve around CTRL and ALT keys mostly. In addition, some keyboard bindings are subject to several key presses. It is pivotal in elevating the emacs user experience by allowing users to navigate, edit, and manipulate texts efficiently as per their preference within the emacs environment.
How to check if my system has Emacs installed in Linux?
To check if your Linux system has the emacs text editor installed or not, you can run the command emacs
on the terminal. The editor will start with the default welcome message if the emacs is already installed in the system. Nor it will return an error message.
How to install the emacs text editor in Redhat-based Linux distributions?
To install the Emacs text editor in Redhat-based distributions such as Fedora or CentOS, open the terminal and run sudo yum install emacs
(press “Y” if prompted). This installs the Emacs text editor in the system. Then, to verify the installation, run the command emacs --version
. It prints the installed version name of the text editor in the terminal. Moreover, you can update the system package information using the command sudo yum update
both before and after the main installation.
How do I use the emacs command in Linux?
To use the “emacs” command in Linux, just open the terminal and type emacs
. Upon running this command, it starts the emacs text editor along with its Graphical User Interface (GUI). Moreover, to open a file and the text editor simultaneously, use the command emacs <file_name>
. This opens the “file_name” with its contents, if exists, or creates the file to work within emacs.
How to copy and paste a selected text using emacs in Linux?
To copy a selected text in Emacs text editor, press ALT+W (also called Mega+W) and to paste the selected text press CTRL+Y in Emacs.
What is Lisp code for Emacs in Linux?
LISP is one of the oldest programming languages in the world. Emacs Lisp was developed using the common “LISP” language that is simple and easy to apply even though it is a full-fledged programming language. Subsequently, this language lets edit and customize “Emacs” as you wish. So, this text editor will give you great flexibility and many professionals use “Emacs” just for this feature.
Related Articles
- [4 Methods] How to Install Emacs in Linux?
- How to Cut, Copy, and Paste in Emacs [2 Methods]
- How to Exit Emacs Text Editor in Linux? [4 Methods]
<< Go Back to Linux Text Editors | Learn Linux Basics