在Linux上如何使用Emacs编辑器

简介

Emacs 是最古老且功能最多样化的文本编辑器之一。GNU Emacs 版本最初于1984年编写,并以其强大且丰富的编辑功能而闻名。它可以通过不同的模式进行定制和扩展,使其可以像集成开发环境(IDE)一样用于编程语言,如Java、C和Python。

对于那些既使用过Vi又使用过用户友好型的nano文本编辑器的人来说,Emacs可以看作是两者之间的一种选择。它的优势和功能类似于Vi,而其菜单、帮助文件和命令键则类似于nano。

在本文中,你将学习如何在Ubuntu 22.04服务器上安装Emacs,并将其用于基本文本编辑。

先决条件

为了跟随本教程,您需要一个设置了sudo特权和启用了防火墙的非root用户的Ubuntu 22.04服务器。您可以按照我们的《使用Ubuntu 22.04初期服务器设置指南》进行设置。

第一步 – 安装Emacs

首先,检查您的系统上是否已经安装了Emacs。

  1. emacs

如果程序已安装,编辑器将会启动默认的欢迎信息。如果没有安装,则会收到以下输出:

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

安装Emacs,请使用以下命令:

  1. sudo apt install emacs

安装了Emacs后,你就可以继续进行下一步。

步骤2 – 使用界面

在终端中输入命令“emacs”来启动Emacs。

  1. emacs

Emacs 以一个空白的编辑缓冲区开始,并等待您开始输入。当您启动 Emacs 时,如果没有指定文件,程序将显示一个欢迎消息。

Emacs welcome message

要开始一个新文件,将光标移到“访问新文件”链接上,按下TAB键然后按下ENTER。你也可以按下CTRL+X,然后按下CTRL+F来新建一个文件。在你的终端末尾会出现一个提示,要求输入文件名。

A prompt appears at the bottom of the Emacs window asking for a file name.

输入一个文件名开始进行文本编辑。在以下示例中,使用了myfile.txt。您可以随意命名这个文件。输入文件名后,按下回车键继续。

一个空文件将准备好进行文本输入。

Empty file appears after entering a file name.

屏幕顶端有一个菜单。菜单下方是一个大的编辑空间。这被称为主缓冲区,您可以在此输入文本或查看文件内容。

当Emacs编辑磁盘上的现有文件时,该文档的副本首先被加载到内存中,然后在主编辑窗口中显示出来。这个内存区域被称为缓冲区。当您在文档中工作时,您所做的所有更改都将应用到缓冲区,而磁盘上的原始文件保持不变。偶尔,Emacs会在后台自动保存,但只有当您手动保存文档时,更改才会写入磁盘。对于新文件也是如此。在保存之前,所有更改都是在缓冲区上进行的。Emacs中的主编辑空间是您对缓冲区的视图。

在主缓冲区之后,屏幕底部会显示一栏突出的文字。这被称为状态栏或模式栏。这里显示的文字取决于Emacs当前所处的模式。除其他内容外,状态栏还包括:

  • Name of the current file
  • Current cursor location
  • Current editing mode
  • The status of the file (– for an unmodified file, ** for a file with un-saved changes and %% for read-only files)

最后,在状态栏结束的地方之后存在一行空间。在这个例子中,它显示了文本”(新文件)”。这个区域被称为迷你缓冲区。Emacs是一个命令驱动的工具,迷你缓冲区是你的主要交互点。在这里,Emacs会提示你输入命令,并显示输出结果。

Emacs的文本版本和其图形用户界面(GUI)版本对待窗口的方式不同。与GUI应用程序不同,文本版本的Emacs窗口不会弹出,因为它们在终端或控制台会话中无法实现物理弹出。当Emacs需要启动一个新窗口时,它的主缓冲区会被分成两部分,就像在浏览器中有两个框架一样。上半部分显示主缓冲区的内容,下半部分显示新的内容。例如,当你访问Emacs的帮助文件或教程时,Emacs会生成一个新窗口。

访问菜单

Emacs启动时通常会占用整个屏幕。位于屏幕顶部的菜单栏提供大部分功能的访问方式。

与基于图形用户界面的程序不同,文本菜单不能通过鼠标点击来下拉。事实上,您也不能使用快捷键来高亮和滚动浏览菜单。

要访问菜单,请按下F10键。这将在主缓冲区下方打开一个新窗口,并显示一系列用于访问菜单项的键。迷你缓冲区会提示你输入所需的键。一旦你按下该键,新窗口的内容将改变,显示下一层的选项。

无论你在菜单的深度中有多深,只需按下ESC键三次即可退出菜单。这通常会关闭菜单窗口并带你回到主缓冲区。

这里有一些从“工具”菜单中可用的选项。

  • Calendar
  • Simple calculator
  • Programmable calculator
  • Searching a directory
  • Encrypting and decrypting document
  • Send and read e-mails
  • Search files using grep
  • Spell checking
  • Running shell commands and compiling code
  • Version control
  • Compare and merge files
  • Games

访问帮助与教程

Emacs拥有一个包含了很多帮助和教程的系统。要访问它,你可以通过按下F10键,然后使用右箭头或左箭头来选择菜单中的“帮助”,或者按下CTRL+H,然后再按一个相应的键。例如,在按下CTRL+H后,你可以输入以下任意一个键来查看常见问题、教程、新闻以及其他主题。

  • t to enter an Emacs Tutorial
  • CTRL+F for an FAQ
  • CTRL+P to learn about known bugs and problems
  • CTRL+R to read the Emacs Manual
  • CTRL+E to find extra packages

第三步-使用命令键

现在你熟悉了用户界面,你可以开始熟悉Emacs的命令键了。当你打开一个文件时,你可以同时开始输入和发出命令。

命令功能通常涉及两个或三个按键。最常见的是CTRL键,其次是ALT键或ESC键。在Emacs环境中,CTRL以简写形式表示为“C”。类似于Emacs内的C-x C-c的注释意味着您同时按下CTRL + X键,然后再按下CTRL + C键。同样地,C-h t表示同时按下CTRL + H键,然后释放两个键再按t键。

在Emacs中,ALT和ESC键被称为元键。在苹果机器上,使用OPTION键代替ALT键。其他键盘使用EDIT键。与CTRL键类似,Emacs使用元键进行多键功能。例如,M-x这样的符号意味着你同时按下ALT或OPTION和x键。同样,你也可以使用ESC+X来完成相同的命令。

在Emacs中,ENTER键被表示为RET,缩写为回车。ESC键通常被表示为E。

按下ESC键可以退出命令或提示。例如,你可以多次按ESC键来退出特定的菜单。另一种取消操作的方法是按下CTRL+G键。

保存并退出

当您对文档进行了一些更改或编写了一些文字后,您可以按下CTRL + X,然后按下CTRL + S进行保存。小型缓冲区将输出以下消息:

Output
Wrote /home/sammy/myfile.txt

你可以通过按下CTRL+X,然后再按下CTRL+C来退出Emacs。

如果在退出之前没有手动保存文件,你将收到此消息。

Output
Save file /home/sammy/myfile.txt? (y, n, !, ., q, C-r, C-f, d or C-h)

按下Y键保存文件。

如果你按下N表示不同意,你将收到以下消息:

Output
Modified buffers exist; exit anyway? (yes or no)

输入”是”以不保存退出。

导航文本

在Emacs中,浏览长篇文档或帮助主题可能是一项繁琐的任务。幸运的是,Emacs提供了多种浏览文件的方式。

以下是一些常见的导航功能列表:

To perform this function Use these keys
Moving to the next line CTRL+N (N for Next)
Moving to the previous line CTRL+P (P for Previous)
Moving one character forward CTRL+F (F for Forward)
Moving one character backward CTRL+B (B for Backward)
Moving one word forward META+F (F for Forward)
Moving one word backward META+B (B for Backward)
Moving to the start of a line CTRL+A
Moving to the end of a line CTRL+E (E for End)
Moving to the start of a sentence META+A
Moving to the end of a sentence META+E (E for End)
Moving one page down CTRL+V (or PgDn)
Moving one page up META+V (or PgUp)
Moving to the beginning of the file META+< (Alt + Shift + “<”)
Moving to the end of the file META+> (Alt + Shift + “>”)

请记住,META 键可以使用以下任意一个键:ALT、ESC、OPTION 或 EDIT。

步骤4 – 编辑功能

如果您需要执行更多在流行的文字处理软件中常见的专门任务,比如选择或突出显示特定的文本文件部分,您可以在Emacs中完成。

标记文字区域

按照以下步骤标记一个文本区域:

  • Move the cursor to the position where you would like the selection to start. You can use any of the methods described previously to move the cursor.
  • Press CTRL+SPACEBAR or CTRL+@ to set a mark to begin your text highlighting. The mini buffer will show a status message of Mark Activated.
  • Move the cursor to the position where you want the region to end. By using any of the key combinations described before.
  • The text will be highlighted up to the point where your cursor is now located.
  • Press CTRL-SPACEBAR or CTRL+@ twice to unmark the highlighted text. The mini buffer will show a status message of Mark Deactivated.

或者,就像一个文字处理软件那样,你可以按住SHIFT键,并使用键盘上的上下箭头键来移动光标,以进行选择。

如果你想选择光标当前所在的段落,请按下META+H键。之后连续按下META+h键将选择文本文件中的下一段落。

如果您想选择主缓冲区的所有内容(即“全选”),请按Ctrl+X然后按h。

剪切、复制和粘贴文本

类似于文字处理器,你可以复制、剪切和粘贴文本。

  • To copy the text you’ve selected, press META+W.
  • To cut the text selection, press CTRL-W.
  • To paste a text selection, press CTRL-Y.

删除文本

使用退格键和删除键删除文本的方式与您预期的一样。

想要快速删除整个词汇,只需要将光标移至词汇开头,然后按下 META+D。若想删除多个词汇,按住 META 键并连续按下 D。词汇将会逐个被删除。

要删除整行文本,将光标定位到您要删除的位置,然后按下Ctrl+K。这样会删除整行文本,直到行尾为止。

按下META+K可以删除一句话。请注意,如果句号后没有两个空格,Emacs将删除整行或更多内容。句号后的两个空格是Emacs确定句子是否跨越多行的标志。

撤销和重做

按下CTRL+X然后按u可以撤销上一次操作。另一种替代的按键组合是CTRL+_(这里的按键是CTRL, SHIFT和-来进行下划线操作)。

要重新做你上次的撤销操作,请按下CTRL+G,接着按下CTRL+下划线键。

搜索和替换文本

在Emacs中有两个搜索方向:向前和向后。在向前搜索中,将从当前光标位置向前搜索您指定的单词。而对于向后搜索,则相反。

  • Press CTRL+S for forward search. Then input the text you’re searching for in the mini-buffer prompt.
  • Press CTRL+R for backward search.

当你输入搜索词后,Emacs会立即搜索并在主缓冲区中突显所有匹配项。

例如,在文本文件中搜索“猫”这个词将会将主缓冲区中的每个出现都显示为突出显示的文字。

The word 'cat' is highlighted after inputting the search term 'cat' in a forward search in the mini-buffer.

更换文本时,请按照以下步骤进行:

  • Press META+%. The mini buffer will prompt for the text to be searched with Query replace:.
  • Input the text that you’re replacing and press ENTER.
  • The mini buffer will display Query replace your_search_term with:.
  • Enter the word or phrase you want to replace the your_search_term with and press ENTER.
  • Each match will be highlighted, and you will be given a prompt to make a replacement. The mini buffer will ask Query replacing your_search_word with your_replacement_word: (C-h for help).
  • Press y to replace the current match found.
  • Press n to skip to the next match.
  • Press q to exit without any replacements.
  • Press ! to do a global replacement without any prompts. The mini buffer will output this message: replaced number occurrences.

加入左对齐、右对齐和居中对齐。

要使一行居中,将光标移动到该行开头处,然后按下META+O,再按下META+S。

为了证明所选择的文本区域,按照以下步骤进行:

  • Highlight the text you wish to justify.
  • Press META+X. The mini buffer will await a response.
  • Input set-justifiction- and press the TAB key.
  • You will be given the following completion options: set-justification-center, set-justification-left, set-justification-right, set-justification-none and set-justification-full.
  • Complete the justification command, by selecting set-justification-right or one of your choice, then press ENTER.
  • The selected text will be justified to the direction of your choosing.

以下是分别针对不同对齐设置指定的文本示例:

An example of selecting the text in Emacs. The first section reveals that the text is selected. The second section reveals text justified to the right. The next section is justified to the left. The last section reveals a centered justification of the text.

转换大小写

您可以使用几个不同的命令来转换大小写。以下是一些命令按键的列表:

To perform this function Use these keys
Capitalizing a word after the cursor META+C (C for capitalize)
Converting a word to lowercase META+L (L for lowercase)
Converting a word to uppercase META+U (U for uppercase)
Converting a paragraph to uppercase Block select, then CTRL+X CTRL+U
Converting a paragraph to lowercase Block select, then CTRL+X CTRL+L

如果你要将一个完整的段落或更多内容转换为大写或小写,会出现一个新窗口和提示信息。

Window
You have typed C-x C-l, invoking disabled command downcase-region. It is disabled because new users often find it confusing. Here’s the first part of its description: Convert the region to lowercaselower case. In programs, it wants two arguments.These arguments specify the starting and ending character numbers of the region to operate on. When used as a command, the text between point and the mark is operated on. Do you want to use this command anyway? You can now type 'y' to try it and enable it (no questions if you use it again). 'n' to cancel--don’t try the command, and it remains disabled. 'SPC' to try the command just this once, but leave it disabled. '!' to try it, and enable all disabled commands for this session only.
A message inside your Emacs instance after pressing the  and  keys to convert a paragraph to lowercase.

按下所提到的按键继续操作。

管理Windows

在Emacs中管理窗口可以帮助您更高效地处理文件。

比如,从你的主缓冲区中,通过按下CTRL+h然后t,可以切换到Emacs教程。你的主缓冲区窗口现在是Emacs教程。如果你想切换回myfile.txt缓冲区,按下CTRL+X,然后b。这是切换缓冲区的命令。Emacs会要求你输入要切换的缓冲区名称。开始输入缓冲区名称myfile.txt,然后按ENTER。这将使你从Emacs教程转到你指定的文件。

第五步 – 进入模式

进入一个主要模式

由于Emacs具有能够假设不同模式的能力,这也是它在UNIX社区被广泛采用的原因之一。一个模式可以增强Emacs的功能。

根据所选择的模式,Emacs可以用作编写文本文件的文字处理器,也可以适用于高级任务,例如编写Python、C或Java代码。例如,您可以更改Emacs的模式,使其能够与版本控制系统配合使用、运行Shell命令或阅读man页面。

Emacs有两种不同类型的模式。一种被称为主模式(major mode)。在主模式中,Emacs可以作为编程或脚本语言的集成开发环境(IDE)使用。在这种模式下,程序提供了特殊的功能,如语法高亮、缩进和格式化、语言特定的菜单选项,或与调试器和编译器的自动接口。

为了演示,您可以使用Emacs在Python中编写一个“Hello World”应用程序。

在您的终端和根目录中,输入以下命令:

  1. cd ~
  2. emacs hello.py

Emacs能够识别文件扩展名并自动以Python模式启动。在主缓冲区中,输入以下Python代码:

print "hello world!\n"

现在用彩色语法高亮显示关键词。还要注意迷你缓冲区上方的状态行显示您当前所处的模式。主菜单也有一个专门针对Python的独立条目。

Inside the Emacs main buffer, the edited Python code has color syntax-highlighting and reveals that it is in Python mode in the status line. The main menu includes a 'Python' selection.

使用CTRL+X CTRL+S保存缓冲区。

要在Emacs中更改主模式,请按下 META+X。迷你缓冲区将等待您的回应。然后,您可以输入不同的模式。以下是一些主模式的例子:

  • c-mode
  • python-mode
  • java-mode
  • html-mode
  • perl-mode
  • shell-script-mode
  • text-mode

进入一个小调

与主要模式相比,小模式提供更具体的功能。这些功能可以与特定的主要模式相关联,或者与主要模式无关地在整个系统中产生影响。此外,不同于主要模式,可以同时启用多个小模式。小模式就像开关:有些默认开启,有些则未开启。如果一个小模式已经开启,再次调用它将关闭它。如果它未开启,调用后将重新开启。

一个次要模式的例子是在前面的例子中使用的设置对齐的选项。

另一个次要模式的例子是自动填充模式。要在您的Emacs编辑器中进入此模式,按下META+X键,然后输入auto-fill-mode。

当文本长度超过70个字符时,这种模式会使得文本换行并换到下一行。请记住,当你启用一个次要模式时,它就像一个切换开关。再次调用相同的命令将会禁用换行。

以下是一些副调的例子:

  • auto-save-mode: This toggles the property of auto saving that periodically saves the contents of the main buffer behind the scene.
  • line-number-mode: This toggles the display of the current line number in the status bar.
  • linum-mode: Toggles the display of line numbers along the left edge of the window.
  • column-number-mode: Shows the current position of the cursor in status bar.
  • overwrite-mode: This is like pressing the INS key on your keyboard. When switched on, it will overwrite text on the right side of the cursor as you type.
  • menu-bar-mode: This can switch the main menu on or off.

结论

在本教程中,你学习了Emacs中的各种命令、编辑功能和模式。

为了进一步了解Emacs编辑器,GNU Emacs网页上有丰富的信息,包括指向Emacs Wiki等其他资源的链接。您还可以阅读GNU Emacs手册。

发表回复 0

Your email address will not be published. Required fields are marked *