Emacs is a venerable text editor that has been around since 1984. Known for its versatility and powerful editing features, Emacs has stood the test of time and remains a popular choice for programmers and Linux users. In this guide, we will take you through the process of installing Emacs on your Ubuntu 22.04 server and show you how to use it for basic text editing.
Prerequisites
Before we dive into the installation process, there are a few prerequisites that you need to have in place. Firstly, you will need an Ubuntu 22.04 server with a non-root user that has sudo privileges. Additionally, make sure that your firewall is enabled to ensure the security of your server. If you haven’t set up your server yet, you can follow our Initial Server Setup with Ubuntu 22.04 guide to get everything up and running.
Step 1 – Installing Emacs
The first step is to check if Emacs is already installed on your system. You can do this by running the following command in your terminal:
emacs
If Emacs is already installed, the editor will start with the default welcome message. However, if Emacs is not installed, you will see an output similar to the following:
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.
To install Emacs, simply use the following command:
sudo apt install emacs
Once the installation is complete, you are ready to move on to the next step.
Step 2 – Navigating the Interface
Now that Emacs is installed on your machine, let’s explore the interface and learn how to navigate through the editor.
To start Emacs, open your terminal and type:
emacs
Emacs will open with an empty editing buffer, waiting for you to start typing. If you don’t specify a file, Emacs will display a welcome message.
To create a new file, move the cursor to the link “Visit New File” by pressing the TAB key and then press ENTER. Alternatively, you can press CTRL+X, then CTRL+F to create a new file. Emacs will prompt you to enter a filename.
Enter a filename of your choice, for example, “myfile.txt”. An empty file will be created and ready for text entry.
The Emacs interface consists of several sections. At the top of the screen, you will find a menu bar that provides access to various commands and features. Below the menu bar is the main editing space, known as the main buffer, where you can type and view the contents of your file.
When editing an existing file, Emacs loads a copy of the document into memory and displays it in the main editing window. This area in memory is called a buffer. Any changes you make in the editing space are applied to the buffer, while the original file remains unchanged on disk. Remember to save your changes manually to write them back to the disk.
After the main buffer, you will see a highlighted bar of text called the status bar or mode line. This bar displays information such as the current file name, cursor location, and editing mode. It also indicates the status of the file, such as whether it has been modified or is read-only.
Finally, there is a single line of space at the bottom of the screen called the mini buffer. The mini buffer is where Emacs prompts you for command inputs and displays output.
In the text-based version of Emacs, windows are handled differently compared to the GUI-based version. Instead of popping out as separate windows, text-based Emacs splits the main buffer into two parts when necessary. The top half displays the main buffer, while the bottom half shows the new content. This is similar to having two frames in a browser. You will encounter this when accessing help files or tutorials.
Step 3 – Accessing Menus and Help
Emacs provides a comprehensive set of menus that offer a wide range of functionalities. To access the menus, press the F10 key. This will open a new window below the main buffer, displaying a list of keys to access the menu items. The mini buffer will prompt you to enter the required key. Press the corresponding key to access the next level of options. To exit the menus, press the ESC key three times to return to the main buffer.
The Tools menu is one of the main menus in Emacs and provides access to various utilities and features. Some of the options available in the Tools menu include the calendar, simple calculator, programmable calculator, directory search, encryption and decryption of documents, email management, file searching using grep, spell checking, running shell commands and compiling code, version control, file comparison and merging, and even games.
Emacs also has an extensive help system with tutorials and FAQs. To access the help system, you can use the menu by pressing F10 and navigating to the Help section. Alternatively, you can press CTRL+H followed by a corresponding key. For example, pressing CTRL+H followed by “t” will take you to the Emacs Tutorial. Other useful keys include CTRL+F for FAQs, CTRL+R for the Emacs Manual, and CTRL+E for extra packages.
Step 4 – Mastering Command Keys
Emacs provides a wide range of command keys that allow you to perform various actions and operations. Command functions in Emacs typically involve two or three keys. The most common key is the CTRL key, which is denoted by “C” within the Emacs environment. For example, the key combination “C-x C-c” means pressing CTRL+X together, followed by CTRL+C.
The ALT or ESC key is referred to as the meta key in Emacs. On Apple machines, you can use the OPTION key instead of ALT. Other keyboards may have an EDIT key. Similar to the CTRL key, Emacs uses multi-key functions with the meta key. For example, “M-x” means pressing ALT or OPTION and X together, and ESC+X accomplishes the same command.
The ENTER key is shown as RET in Emacs, which stands for return. The ESC key is often represented as E. You can use the ESC key to back out of a command or prompt. Pressing ESC multiple times will exit out of a specific menu. Another way to cancel an operation is by pressing CTRL+G.
Saving and quitting are essential operations in any text editor. In Emacs, you can save your document by pressing CTRL+X, followed by CTRL+S. This will save the changes and display a message indicating the successful write to the disk. To exit Emacs, press CTRL+X, then CTRL+C. If you haven’t saved your file, Emacs will prompt you to save before exiting.
Navigating through a text file can be a cumbersome task, especially if it is long. Emacs provides several navigation functions to make this process easier. For example, you can move to the next line by pressing CTRL+N or the previous line by pressing CTRL+P. To move one character forward, use CTRL+F, and to move one character backward, use CTRL+B. Emacs also allows you to move word by word using META+F (forward) and META+B (backward). The beginning of a line can be reached by pressing CTRL+A, and the end of a line can be reached by pressing CTRL+E. Additionally, you can move to the start or end of a sentence using META+A and META+E, respectively. For larger jumps, you can move one page down with CTRL+V or one page up with META+V. To move to the beginning or end of a file, use META+< and META+>, respectively.
Step 5 – Mastering Editing Functions
Emacs offers a variety of editing functions that allow you to manipulate text with ease. To select or highlight a specific section of text, you can mark text regions in Emacs. There are several ways to do this. One method is to move the cursor to the starting position of the selection and press CTRL+SPACEBAR or CTRL+@ to set the mark. Then, move the cursor to the end position of the selection. The text between the starting and ending positions will be highlighted. To unmark the highlighted text, press CTRL-SPACEBAR or CTRL+@ twice.
Another method for selecting text is to hold the SHIFT key and use the UP or DOWN arrow keys to make the selection. You can also select the paragraph your cursor is currently on by pressing META+H. Pressing META+H continuously will select subsequent paragraphs in your text file. To select all the contents of the main buffer, press CTRL+X followed by h.
Once you have selected text, you can perform various operations such as copying, cutting, and pasting. To copy the selected text, press META+W. To cut the text, press CTRL+W. To paste the text, press CTRL+Y.
Deleting text in Emacs is straightforward. You can use the Backspace and Delete keys to delete characters. To delete a whole word quickly, move the cursor to the beginning of the word and press META+D. To delete multiple words, press and hold the META key and continuously press D. Emacs will delete words one by one. To delete a whole line, position the cursor where you want and press CTRL+K. This will delete all the text from the cursor position to the end of the line. To delete a sentence, press META+K.
Emacs also provides undo and redo functionality to reverse or redo previous operations. To undo the last operation, press CTRL+X followed by u. You can also use the key combination CTRL+_ (CTRL+SHIFT+MINUS) to perform an underscore. To redo your last undo, press CTRL+G followed by CTRL+_.
Searching and replacing text is a common task in any text editor. Emacs offers powerful search and replace features. To search for a specific word or phrase, press CTRL+S for forward search or CTRL+R for backward search. Emacs will highlight any matches it finds in the main buffer. To replace text, press META+% and Emacs will prompt you to enter the text to be searched and the replacement text. You can then choose to replace each occurrence individually or perform a global replacement.
Emacs also provides options for aligning text. To center a line, move the cursor to the beginning of the line and press META+O followed by META+S. To justify a selected text region, highlight the text and press META+X. In the mini buffer, enter “set-justification-” and press the TAB key to see completion options. Select the desired justification command, such as “set-justification-right”, and the selected text will be justified accordingly.
Converting case is another useful feature in Emacs. You can capitalize a word after the cursor by pressing META+C. To convert a word to lowercase, press META+L, and to convert a word to uppercase, press META+U. If you want to convert a whole paragraph to uppercase or lowercase, you can use the block selection feature by selecting the text and pressing CTRL+X followed by CTRL+U for uppercase or CTRL+X followed by CTRL+L for lowercase.
Step 6 – Managing Windows
Emacs allows you to work with multiple windows, making it easier to navigate through your files and perform different tasks simultaneously. To switch between windows, you can use the key combination CTRL+X followed by the key combination CTRL+B. Emacs will prompt you to enter the buffer name you want to switch to. Simply start typing the buffer name, such as “myfile.txt”, and press ENTER. This will take you from one buffer to another.
Step 7 – Entering Major Modes
One of the key features of Emacs is its ability to assume different modes, known as major modes. Major modes enhance Emacs’ functionality and provide specialized features for specific tasks, such as programming or scripting. For example, you can change Emacs’ mode to work with languages like Python, C, or Java, which enables features like syntax highlighting, indentation, and language-specific menu options.
To enter a major mode, open Emacs and create a new file with the desired file extension. For example, to enter Python mode, create a file with a .py extension. Emacs will automatically recognize the file extension and switch to the corresponding major mode. You can then write code in that particular language and take advantage of the language-specific features provided by Emacs. Save your file with CTRL+X followed by CTRL+S.
To change the major mode within Emacs, press META+X and enter the desired mode. Emacs will prompt you with completion options for available major modes. Select the mode you want and press ENTER.
Step 8 – Entering Minor Modes
In addition to major modes, Emacs also supports minor modes. Minor modes provide specific features that can be tied to a major mode or have a system-wide effect. Unlike major modes, multiple minor modes can be active at the same time. Some minor modes are enabled by default, while others need to be explicitly activated.
For example, the auto-fill-mode is a minor mode that automatically wraps lines of text when they exceed a certain length. To enable this mode in Emacs, press META+X and enter “auto-fill-mode”. Emacs will toggle the mode on or off, depending on its current state.
Other minor modes include auto-save-mode, which periodically saves the contents of the main buffer, line-number-mode, which displays the current line number in the status bar, linum-mode, which displays line numbers along the left edge of the window, column-number-mode, which shows the cursor position in the status bar, and overwrite-mode, which allows text to be overwritten instead of inserted. You can toggle these modes on or off as needed.
Conclusion
In this comprehensive guide, we have covered the installation process of Emacs on Ubuntu 22.04, explored its user interface, and learned how to navigate, edit, and manage files effectively. We have also delved into major and minor modes, which provide specialized features for programming and other tasks. With this knowledge, you are now equipped to master the Emacs editor and leverage its powerful capabilities.
To further enhance your experience with Emacs and explore more advanced features, we recommend visiting the GNU Emacs website, which offers a wealth of information, including links to the Emacs Wiki and the GNU Emacs manual.
Shape.host offers reliable and scalable cloud hosting solutions, including Cloud VPS services. With Shape.host, you can enjoy the benefits of high-performance cloud hosting tailored to your specific needs. Visit Shape.host to learn more about our services and how we can help your business thrive in the digital landscape.