Linux emacs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful emacs text editor on a Ubuntu 22.04 Docker container. The lab covers the basics of getting started with emacs, including installing the necessary package, launching the editor, and exploring some fundamental commands and shortcuts. Additionally, you will learn how to customize emacs by modifying its configuration files. This lab aims to provide you with the essential skills to effectively use emacs for text processing and editing tasks.

The lab is divided into three main steps: "Getting Started with emacs", "Basic emacs Commands and Shortcuts", and "Customizing emacs with Configuration Files". In the first step, you will install the emacs package and launch the editor. The second step introduces you to basic navigation, editing, and file management commands within emacs. Finally, the third step guides you through the process of customizing emacs by modifying its configuration files, allowing you to personalize your editing experience.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") subgraph Lab Skills linux/apt -.-> lab-422661{{"`Linux emacs Command with Practical Examples`"}} linux/sudo -.-> lab-422661{{"`Linux emacs Command with Practical Examples`"}} linux/touch -.-> lab-422661{{"`Linux emacs Command with Practical Examples`"}} linux/vim -.-> lab-422661{{"`Linux emacs Command with Practical Examples`"}} end

Getting Started with emacs

In this step, we will learn how to install and launch the emacs text editor on our Ubuntu 22.04 Docker container.

First, let's 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.

Now that emacs is installed, we can launch it using the following command:

emacs

This will open the emacs editor in the terminal. You should see the emacs startup screen with some basic information and 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'.

In the next step, we will learn some basic emacs commands and shortcuts to help you get started with this powerful text editor.

Basic emacs Commands and Shortcuts

In this step, we will explore some of the basic commands and shortcuts in the emacs text editor.

First, let's learn how to navigate 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 learn how to edit text 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 are just a few of the basic commands and shortcuts in emacs. In the next step, we will learn how to customize emacs with configuration files.

Customizing emacs with Configuration Files

In this step, we will learn how to customize the emacs editor by modifying its configuration files.

Emacs stores its configuration in the .emacs file, which is located in the user's home directory. Let's create this file and add some basic customizations:

touch ~/.emacs

Now, open the .emacs file in emacs:

emacs ~/.emacs

In the .emacs file, add the following lines to change 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

You should see that the font size has increased, and line numbers are displayed in the editor.

Example output:

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

Emacs supports a wide range of customization options, from changing the theme and appearance to adding custom functionality. You can explore the emacs documentation and online resources to learn more about configuring emacs to suit your needs.

Summary

In this lab, you first learned how to install and launch the emacs text editor on your Ubuntu 22.04 Docker container. You then explored some basic emacs commands and shortcuts for navigating within the editor, such as moving the cursor, scrolling through the text, and performing common editing tasks like copying, pasting, and searching. Finally, you discovered how to customize emacs by modifying its configuration files to personalize your editing experience.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like