emacs Command in Linux

Introduction

This lab provides a comprehensive guide on leveraging the potent emacs text editor within an Ubuntu 22.04 Docker container environment. It's designed to equip systemadmin professionals and aspiring users with the fundamental knowledge needed to get started with emacs. This includes the installation process, launching the editor, and mastering essential commands and shortcuts. Furthermore, the lab demonstrates how to customize emacs extensively through modification of its configuration files, enabling a personalized editing experience. The ultimate goal is to empower you with the skills to effectively utilize emacs for a wide array of text processing and editing tasks in your systemadmin role.

The lab is structured into three key sections: "Getting Started with emacs", "Basic emacs Commands and Shortcuts", and "Customizing emacs with Configuration Files". The initial section focuses on installing the emacs package and launching the editor. The subsequent section introduces fundamental navigation, editing, and file management commands within the emacs environment. Lastly, the concluding section walks you through customizing emacs by directly modifying its configuration files, allowing for a highly personalized and efficient editing workflow.

Getting Started with emacs

This section details the process of installing and launching the emacs text editor on an Ubuntu 22.04 Docker container.

To begin, install the emacs package using the following command:

sudo apt-get update
sudo apt-get install -y emacs

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  emacs-common emacs-gtk libgnutls30 libotf0 libxaw7 libxft2 libxpm4
Suggested packages:
  emacs-lucid emacs-nox emacs-gtk-el emacs-el emacs-common-non-dfsg
The following NEW packages will be installed:
  emacs emacs-common emacs-gtk libgnutls30 libotf0 libxaw7 libxft2 libxpm4
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.

With emacs successfully installed, launch it using the following command:

emacs

This will initiate the emacs editor within the terminal. The emacs startup screen will appear, presenting basic information and available commands.

Example output:

GNU Emacs
Copyright (C) 2022 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

Welcome to GNU Emacs, one of the best text editors in the world.

To get started, you can read the tutorial by pressing Ctrl+h t.
Or you can try out Emacs by opening a file, for example with 'C-x C-f'.

The subsequent section will introduce essential emacs commands and shortcuts, facilitating efficient usage of this versatile text editor.

Basic emacs Commands and Shortcuts

This section explores fundamental commands and shortcuts within the emacs text editor.

Firstly, let's cover navigation within the emacs editor:

## Move the cursor up, down, left, and right
Ctrl+p, Ctrl+n, Ctrl+b, Ctrl+f

## Move the cursor to the beginning/end of the line
Ctrl+a, Ctrl+e

## Scroll up and down
Ctrl+v, Alt+v

Example output:

The cursor moves as expected.

Next, let's delve into text editing functionalities in emacs:

## Delete the character under the cursor
Ctrl+d

## Delete the word before the cursor
Alt+Backspace

## Cut the current line
Ctrl+k

## Copy the current line
Ctrl+space Ctrl+w

## Paste the copied/cut text
Ctrl+y

Example output:

The text is deleted, copied, and pasted as expected.

Finally, let's learn how to save and exit emacs:

## Save the current file
Ctrl+x Ctrl+s

## Exit emacs
Ctrl+x Ctrl+c

Example output:

The file is saved, and emacs is closed.

These represent just a small fraction of the available commands and shortcuts in emacs. The next section will demonstrate how to customize emacs using configuration files.

Customizing emacs with Configuration Files

This section focuses on customizing the emacs editor by modifying its configuration files, specifically for systemadmin tasks.

Emacs stores its configuration within the .emacs file, located in the user's home directory. Create this file and add some basic customizations:

touch ~/.emacs

Now, open the .emacs file in emacs:

emacs ~/.emacs

Within the .emacs file, add the following lines to adjust the font size and enable line numbers:

(set-face-attribute 'default nil :height 140)
(global-display-line-numbers-mode)

Save the file and exit emacs.

Now, let's test the changes:

emacs

Observe that the font size has increased, and line numbers are now visible within the editor.

Example output:

The font size is larger, and line numbers are displayed as expected.

Emacs provides extensive customization options, ranging from altering the theme and appearance to incorporating custom functionality. Consult the emacs documentation and online resources to explore further customization possibilities, tailoring emacs to meet your specific requirements as a systemadmin or Linux user, even potentially running emacs as root for particular tasks.

Summary

This lab commenced with the installation and launching of the emacs text editor on an Ubuntu 22.04 Docker container. It then explored essential emacs commands and shortcuts for efficient editor navigation, including cursor movement, text scrolling, and common editing operations such as copying, pasting, and searching. Finally, the lab demonstrated how to customize emacs by modifying its configuration files, thus personalizing the editing experience for optimal productivity within a systemadmin context. This includes configuring emacs for tasks performed by the root user.

400+ Linux Commands