Linux emacs 命令及实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何在 Ubuntu 22.04 Docker 容器中使用强大的 emacs 文本编辑器。实验涵盖了 emacs 入门的基础知识,包括安装必要的软件包、启动编辑器以及探索一些基本命令和快捷键。此外,你还将学习如何通过修改配置文件来自定义 emacs。本实验旨在为你提供使用 emacs 进行文本处理和编辑任务的基本技能。

实验分为三个主要步骤:「emacs 入门」、「emacs 基本命令和快捷键」以及「使用配置文件自定义 emacs」。在第一步中,你将安装 emacs 软件包并启动编辑器。第二步介绍了 emacs 中的基本导航、编辑和文件管理命令。最后,第三步将指导你通过修改配置文件来自定义 emacs,从而个性化你的编辑体验。

Linux 命令速查表


Skills Graph

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

emacs 入门

在这一步中,我们将学习如何在 Ubuntu 22.04 Docker 容器中安装并启动 emacs 文本编辑器。

首先,使用以下命令安装 emacs 软件包:

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

示例输出:

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.

现在 emacs 已经安装完成,我们可以使用以下命令启动它:

emacs

这将在终端中打开 emacs 编辑器。你应该会看到 emacs 的启动屏幕,其中包含一些基本信息和命令。

示例输出:

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'.

在下一步中,我们将学习一些基本的 emacs 命令和快捷键,帮助你开始使用这个强大的文本编辑器。

emacs 基本命令和快捷键

在这一步中,我们将探索 emacs 文本编辑器中的一些基本命令和快捷键。

首先,学习如何在 emacs 编辑器中导航:

## 上下左右移动光标
Ctrl+p, Ctrl+n, Ctrl+b, Ctrl+f

## 将光标移动到行首/行尾
Ctrl+a, Ctrl+e

## 上下滚动页面
Ctrl+v, Alt+v

示例输出:

光标按预期移动。

接下来,学习如何在 emacs 中编辑文本:

## 删除光标下的字符
Ctrl+d

## 删除光标前的单词
Alt+Backspace

## 剪切当前行
Ctrl+k

## 复制当前行
Ctrl+space Ctrl+w

## 粘贴复制/剪切的文本
Ctrl+y

示例输出:

文本按预期被删除、复制和粘贴。

最后,学习如何保存并退出 emacs:

## 保存当前文件
Ctrl+x Ctrl+s

## 退出 emacs
Ctrl+x Ctrl+c

示例输出:

文件已保存,emacs 已关闭。

这些只是 emacs 中的一些基本命令和快捷键。在下一步中,我们将学习如何通过配置文件自定义 emacs。

使用配置文件自定义 emacs

在这一步中,我们将学习如何通过修改配置文件来自定义 emacs 编辑器。

Emacs 将其配置存储在用户主目录下的 .emacs 文件中。让我们创建这个文件并添加一些基本的自定义配置:

touch ~/.emacs

现在,在 emacs 中打开 .emacs 文件:

emacs ~/.emacs

.emacs 文件中,添加以下行以更改字体大小并启用行号显示:

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

保存文件并退出 emacs。

现在,让我们测试这些更改:

emacs

你应该会看到字体大小增加了,并且编辑器中显示了行号。

示例输出:

字体大小变大,行号按预期显示。

Emacs 支持广泛的自定义选项,从更改主题和外观到添加自定义功能。你可以查阅 emacs 文档和在线资源,了解更多关于如何配置 emacs 以满足你的需求的信息。

总结

在本实验中,你首先学习了如何在 Ubuntu 22.04 Docker 容器中安装并启动 emacs 文本编辑器。接着,你探索了一些基本的 emacs 命令和快捷键,用于在编辑器中进行导航,例如移动光标、滚动文本以及执行常见的编辑任务,如复制、粘贴和搜索。最后,你了解了如何通过修改配置文件来自定义 emacs,从而个性化你的编辑体验。

Linux 命令速查表