第 175 / 187 篇

17.4. Unix 编辑器

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/editors.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.4. Unix Editors

Unix 系统上通常装有好几种不同的编辑程序,用来创建和编辑文本文件。文本文件常用于程序源代码、程序数据和文档。Unix 不会根据文件后缀来解释文件内容的含义。不过,用户通常会为许多文件采用后缀命名惯例。例如,.c 后缀是 C 源代码文件的命名惯例(例如 myprog.c),.h 后缀是 C 头文件的命名惯例(例如 stdio.h)。

Typically, there are often several different editing programs installed on Unix systems to create and edit text files. Text files are commonly used for program source code, program data, and documentation. Unix does not interpret file suffixes to mean anything about a file’s content. However, users typically use suffix naming conventions for many of their files. For example, a .c suffix is the naming convention for C code source files (e.g., myprog.c), and a .h suffix is the naming convention for C header files (e.g., stdio.h).

虽然不必学会每一个编辑程序,但用户至少应该学会一个,以便在 Unix 系统上创建和编辑文件。

While it is not important to learn every editor program, users should learn at least one to create and edit files on Unix systems.

Vim(vi improved,改进版 vi)和 Emacs 是大多数系统上都有的两个编辑器,而 vim(或 vi)在每个 Unix 系统上都一定有。这些编辑器采用基于文本的界面,而不是基于图形界面(不过两者也都有图形界面版本)。一般来说,与使用菜单和指点式界面的图形界面编辑器相比,基于文本的编辑器需要多花一点功夫来学习。然而,基于文本的编辑器运行时通常占用更少的计算机资源,在远程连接到你想编辑文件的系统时尤其有用。

Vim (vi improved) and Emacs are two examples of editors that are available on most systems, and vim (or vi) is always available on every Unix system. These editors have a text-based interface rather than a GUI-based interface (although both have GUI-based versions as well). In general, text-based editors require a bit more effort to learn to use than GUI-based editors that use menus and point-and-click interfaces. However, text-based editors typically use fewer computer resources to run, and they are particularly useful when remotely connected to systems on which you want to edit files.

网上有许多学习 Vim 和 Emacs 的资源。没有必要两个都学,而且关于 Emacs 和 Vim 哪个最好,有一些非常强烈的观点(https://en.wikipedia.org/wiki/Editor_war)。不过,由于 Vim(或 vi)在每个 Unix 系统上都有,我们会更详细地介绍一些 vim 编辑器的基础知识,以及如何学习使用 Vim(和 vi)。我们还会介绍一些 Emacs 和 Nano 的基础知识,并给出这三者的进一步参考资料。

There are many online resources for learning both Vim and Emacs. It is not important to learn both, and there are some strong opinions about which of Emacs and Vim is best (https://en.wikipedia.org/wiki/Editor_war). However, because Vim (or vi) is available on every Unix system, we present a bit more details on the basics of the vim editor and how to learn using Vim (and vi). We also include some information about the basics of Emacs and Nano, including further references for all of three.

17.4.1. Vim 编辑器

17.4.1. The Vim Editor

vim(和 vi)编辑器是 Unix 系统上最普遍可用的编辑器。它是一个高效、轻量而强大的文本编辑器,只要学会几条基本命令就很容易使用,而这些命令可以通过学完 vimtutor 教程来掌握。vim 在通过 ssh 连接远程工作时尤其有用。它还有许多高级功能,并且可以通过 .vimrc 配置文件中的设置进行高度定制。

The vim (and vi) editor is the most widely available editor on Unix systems. It is an efficient, lightweight, and powerful text editor that is easy to use after learning a few basic commands, which you can learn by running through the vimtutor tutorial. vim is particularly useful when working remotely over an ssh connection. It also has many advanced features and is very configurable through settings in a .vimrc config file.

没有必要学会 vim 的所有功能;几条基本命令就能让你开始使用 vim,并完成大多数编辑任务。

It is not necessary to learn all of vim’s features; a few basic commands will get you started with vim and allow you to perform most editing tasks.

Vim 有两种工作模式:

Vim operates in two modes:

  1. 插入模式(insert mode):按键被解释为在光标处插入到文件内容中的字符。
  1. insert mode: keystrokes are interpreted as insertions into the file contents at the point of the cursor.
  1. 命令模式(command mode,或称转义模式,escape mode):按键被解释为 vim 命令,让用户能做诸如保存、退出、搜索或在文件中移动光标位置之类的事情。
  1. command or escape mode: keystrokes are interpreted as vim commands that allow a user to do such things as saving, exiting, searching, or move the cursor’s location in the file.

要从插入模式切换到命令模式,按 ESC 键。

To switch from insert mode to command mode, press the ESC key.

从命令模式切换到插入模式有很多种方法。一种方法是按 i 键。学习 vim 的最佳方式之一,是使用 Vim 教程 vimtutor。

There are many ways to switch from command mode to insert mode. One way is to press the i key. One of the best ways to learn i`vim` is with vimtutor, the Vim tutorial.

vimtutor

vimtutor

学习 vim 编辑器的最好方法是运行 vimtutor:

The best way to learn the vim editor is to run vimtutor:

1
$  vimtutor

要入门 vim,我们建议你集中学习下面列出的 vimtutor 小节(其他小节介绍的是一些不太常用、也不是必需的功能)。学完所有这些课大约需要 20 分钟。

To get started in vim, we recommend that you focus on the vimtutor sections listed below (the other sections cover more obscure features that are not necessary). It takes about 20 minutes to run through all of these lessons.

  • 第 1 课的全部内容(移动光标、x、i、A、:wq)
  • All of Lesson 1 (moving around, x, i, A, :wq)
  • 第 2.6 课(dd)
  • Lesson 2.6 (dd)
  • 第 2.7 课(撤销)
  • Lesson 2.7 (undo)
  • 第 3.1 课(p)和第 3.2 课(r)
  • Lesson 3.1 (p) and 3.2 (r)
  • 第 4.1 课(G)和第 4.2 课(搜索)
  • Lesson 4.1 (G) and 4.2 (searching)
  • 第 6.2 课(a)、第 6.3 课(R)和第 6.4 课(y 和 p)
  • Lesson 6.2 (a), 6.3 ( R ), and 6.4 (y and p)

Vim 命令参考

Vim Command Reference

下面列出了常用的 vim 命令。在学习 vim 的过程中,你可能会发现回头查阅这份列表很有帮助。

Here is a list of common vim command. You may find it helpful to refer back to this as you are learning vim.

  • 切换模式
    i 切换到插入模式:你输入的内容出现在文件中的光标处
    ESC 切换到命令模式:你输入的内容被解释为 vim 命令
  • Switching between modes

    i switch to insert mode: what you type shows up in file at the cursor
    ESC switch command mode, what you type is interpreted as vim commands
  • 移动光标的命令:
    k 光标上移一行(也可以用方向键)
    j 光标下移一行
    l 光标右移一个字符
    h 光标左移一个字符
    CTRL-u 光标上移一页
    CTRL-f 光标下移一页
    SHFT-g 光标移到文件末尾
    :<num> 光标移到第 <num> 行的开头(例如 :1 把光标移到文件开头)
  • Commands for moving around:

    k move cursor up one line (can also use arrow keys)
    j move cursor down one line
    l move cursor right one char
    h move cursor left one char
    CTRL-u move cursor up one page
    CTRL-f move cursor down one page
    SHFT-g move cursor to end of file
    :<num> move cursor to start of line number <num> (e.g., :1 moves cursor to start of file

译注

按 Vim 自带的帮助文档(:help CTRL-U),CTRL-u 默认向上滚动半屏;向上翻一整页是 CTRL-b,与 CTRL-f(向下翻一整页)相对应。

  • 保存与退出的命令:
    :w 把改动保存(写入)到文件
    :q 退出 vim
    :q! 不保存改动直接退出
  • Commands for saving and quitting:

    :w save changes (write) to file
    :q quit vim
    :q! quit without saving changes
  • 切换到插入模式的命令:
    i 插入(insert):你输入的内容出现在光标处
    a 追加(append):你输入的内容出现在光标之后
    o 新开一行(open):在文件中光标所在行的下方新起一行
    O 新开一行(open):在文件中光标所在行的上方新起一行
  • Commands for switching to insert mode:

    i insert: what you type shows at the cursor
    a append: what you type shows up after the cursor
    o open: starts a new line in file below the cursor
    O open: starts a new line in file above the cursor
  • 复制、粘贴、删除的命令:
    yy 复制当前行(光标所在的行)
    <num>yy 从光标所在行开始复制 <num> 行(例如 8yy 复制 8 行)
    p 把复制的内容粘贴到光标所在行之后
    P 把复制的内容粘贴到光标所在行之前
    x 删除光标处的字符
    <num>x 从光标处开始删除 <num> 个字符(例如 4x 删除 4 个字符)
    dd 删除当前行(p 会把它粘贴回来)
    <num>dd 从光标所在行开始删除 <num> 行
    :u 撤销操作,可以撤销多次
    :redo 重做被撤销的操作,撤销了几次就能重做几次
  • Commands to copy, paste, delete:

    yy copy current line (line cursor is on)
    <num>yy copy <num> lines starting with the one the cursor is on (e.g., 8yy copies 8 lines)
    p paste what was copied after line cursor is on
    P paste what was copied before line cursor is on
    x remove the char at the cursor
    <num>x remove <num> chars starting with one at cursor (e.g., 4x remove 4 chars)
    dd delete the current line (p will paste it back)
    <num>dd delete <num> lines starting with one at cursor
    :u undo action, can undo multiple times
    :redo redo undone action, can redo as many times as undo’ed
  • 搜索与替换的命令
    /<pattern> 在文件中搜索 <pattern>,从光标处起找到第一个出现的位置(例如 /hello 在文件中搜索 hello)
    n 向前查找搜索模式的下一个出现位置
    N 向后查找搜索模式的下一个出现位置
    :s/<pattern>/<replacement> 把 <pattern> 的下一个出现位置替换为 <replacement>
    :s/<pattern>/<replacement>/g /g:把 <pattern> 的所有出现位置替换为 <replacement>
  • Commands to Search, Replace

    /<pattern> search for <pattern> in file, finds first occurrence from cursor (e.g., /hello search for hello in file)
    n find next occurrence of search pattern moving forward
    N find next occurrence of search pattern moving backward
    :s/<pattern>/<replacement> replace NEXT occurrence of <pattern> with <replacement>
    :s/<pattern>/<replacement>/g /g: replace ALL occurrences of <pattern> with <replacement>

译注 · :s 的作用范围

按 Vim 帮助文档(:help :s),:[range]s/{pattern}/{string}/ 对 [range] 中的每一行替换匹配项,不写范围时只作用于当前行;不加 g 只替换每行中的第一个匹配,加 g 替换该行中的所有匹配(:help s_g)。所以表中两条命令都只作用于光标所在的那一行;要在整个文件中替换所有匹配,应写作 :%s/<pattern>/<replacement>/g。

  • 可视选择命令:
    SHIFT-v 可视地选中一行(用方向键选择更多行)
    = 重新格式化可视选中的文本(自动缩进 C 代码!)
    gq 把可视选中的文本重新格式化为每行最多 80 个字符
  • Visual Selection Commands:

    SHIFT-v visually select line (use arrow keys to select more lines)
    = reformat visually selected text (auto-indent C code!)
    gq reformat visually selected text to 80 char max line length
  • 其他命令:
    :set number 显示行号
    :set nonumber 不显示行号
    J 把光标下面的一行与光标所在行合并
    :help <cmd> 某条 vim 命令的帮助文档,用 :q 退出帮助文档(例如 help v_ 查看可视选择的帮助信息)。用 <tab>(: help v_<tab>)在不同的可视命令的帮助之间循环切换
  • Misc. Commands:

    :set number show line numbers
    :set nonumber don’t show line numbers
    J join line under cursor with line on cursor
    :help <cmd> help documentation about a specific vim command, :q to quit help documentation (e.g., help v_) help info visual selection. Use <tab> (: help v_<tab>) to cycle through different visual commands’ help

配置 vim

Configuring vim

你可以选择用各种方式来配置 vim。尽管使用 vim 并不要求你配置它,但你可能会发现定制配色方案或调整一些其他设置很有帮助。

Optionally, you can configure vim in all kinds of ways. While you’re not required to configure vim to use it, you may find it helpful to customize the color scheme or adjust some other settings.

在你的主目录中添加一个名为 .vimrc 的 vim 配置文件。启动时,vim 会检查这个文件,为这次 vim 会话设置各种配置选项,包括配色方案和默认窗口大小。

In your home directory add a configuration file for vim, named .vimrc. On start-up, vim examines this file to set different configuration options for the vim session, including setting a color scheme and default window size.

在你的主目录中运行 ls -a,列出你所有的点文件(dot file),确定是否已经存在 .vimrc 文件:

Run ls -a in your home directory to list all your dot files and to determine if a .vimrc file already exists:

1
2
$ cd
$ ls -a ~/ # ~/ is shorthand for /home/you/

如果 .vimrc 文件已存在,你可以在 vim 中打开它,像编辑其他文件一样编辑它:

If the .vimrc file exits, you can open it in vim and edit it like any other file:

1
$ vim  .vimrc

如果不存在,你可以在主目录中创建一个 .vimrc,把朋友的或在网上找到的现成的 .vimrc 示例文件复制进去。例如:

If not, you can create a .vimrc in your home directory and copy an existing example .vimrc file of a friends or that you find online into it. For example:

1
2
3
$ pwd
/home/sarita/
$ vim .vimrc

网上有许多关于配置 vim 设置的资源。

There are many on-line resources information about configuring vim settings.

17.4.2. Emacs 编辑器

17.4.2. The Emacs Editor

Emacs 是 Unix 系统上几乎总能找到的另一个编辑器。没有必要同时学习和使用 Vim 和 Emacs,但学会其中一个是有用的,对另一个也许只需了解几个基本操作(至少知道如何退出每一个)。

Emacs is another editor that is almost always available on Unix systems. It is not necessary to learn and use both Vim and Emacs, but it is useful to know one, and perhaps to know just a few basics about the other (at least how to exit each one).

与 Vim 不同,Emacs 是单模式编辑器,即它运行在单一的插入模式下,命令(例如保存文件)通过 CTRL、ALT 和 SHIFT 键的特殊组合来发出。Vim 和 Emacs 之间的主要权衡在于:Emacs 的命令按键序列比 Vim 的复杂,但它没有 Vim 那种需要用户来回切换的两种模式。Emacs 就像一把瑞士军刀,它有丰富的宏和命令,可以用于各种各样的任务(包括一些不常用的功能)。因此,Emacs 通常不会安装在内存受限的系统上,因为它通常比 Vim 这样的应用程序需要更多的内存。

Unlike Vim, Emacs is a single-mode editor, meaning that it runs in a single insertion mode where commands, like saving the file, are issued through special key combinations with the CTRL, ALT, and SHIFT keys. The main trade-off between Vim and Emacs is that Emacs has more complicated key sequences than Vim for commands, but it doesn’t have the two modes that the user needs to switch between like Vim does. Emacs is like a swiss army knife in that it has a rich assortment of macros and commands that can be used for a wide assortment of tasks (including features that are not commonly used). For this reason, Emacs is typically not installed on memory-constrained systems as it usually requires more memory than an application like Vim.

要启动 emacs,只需在命令行上对要编辑的文件运行它。例如,要在 emacs 中编辑 myprog.c:

To start emacs, just run from the command line on the file you want to edit. For example, to edit myprog.c in emacs:

1
$ emacs myprog.c

用户输入普通的按键时,输入的字符会被加到文件中光标之后的位置。

When the user types in regular keystrokes, the characters entered are added to the file after the cursor.

Emacs 命令参考

Emacs Command Reference

下面列出了常用的 Emacs 命令。在学习 Emacs 的过程中,你可能会发现回头查阅这份列表很有帮助。

Here is a list of common Emacs commands. You may find it helpful to refer back to this as you are learning Emacs.

  • 文件控制(这些命令涉及多键序列):CTRL-x CTRL-s 表示按住 CTRL 键,先按 x,再按 s
    CTRL-x CTRL-s 保存对文件的编辑
    CTRL-x CTRL-c 退出 Emacs
  • File Control (these involve multi-sequences): CTRL-x CTRL-s is hold down the CTRL key and type x followed by s

    CTRL-x CTRL-s save the file edits
    CTRL-x CTRL-c exit Emacs
  • 移动光标
    CTRL-n 光标移到下一行
    CTRL-p 光标移到上一行
    CTRL-b 光标后退一个字符
    CTRL-f 光标前进一个字符
  • Moving the cursor

    CTRL-n move the cursor forward one line
    CTRL-p move the cursor backward one line
    CTRL-b move the cursor back one character
    CTRL-f move the cursor forward one character
  • 删除内容
    CTRL-d 删除一个字符
    CTRL-k 删除一行
  • Deleting Content

    CTRL-d deletes a character
    CTRL-k deletes a line.
  • 复制内容
    CTRL-k 复制一行
    CTRL-y 粘贴
  • Copying Content

    CTRL-k copy a line.
    CTRL-y past
  • 搜索
    CTRL-s 搜索某个模式
  • Search

    CTRL-s search for a pattern
  • 其他命令:
    CTRL-g 退出 emacs 的命令序列(在你输错了什么的时候很有用)
    CTRL-x-0 关闭当前的缓冲区窗口
    CTRL-x-1 关闭除当前缓冲区之外的所有其他缓冲区
    CTRL-x-2 在下方打开缓冲区窗口
    CTRL-x-3 在右侧打开缓冲区窗口
    CTRL-x-o 切换到另一个缓冲区
    ESC-x 进入 meta 模式(在 Emacs 文档以及本表接下来的部分中,记作 M-x)
    M-x goto-line n 跳到文件的第 n 行
    M-x linum-mode 打开行号显示
    M-x ansi-term 在当前缓冲区中打开一个 bash shell
  • Misc. Commands:

    CTRL-g quit out of emacs command sequence (useful if you type in something wrong)
    CTRL-x-0 close the current buffer window
    CTRL-x-1 close all other buffers except the current one
    CTRL-x-2 open buffer window below
    CTRL-x-3 open buffer window to the right
    CTRL-x-o switch to another buffer
    ESC-x enters meta mode (in Emacs documentation and in this table forward, it is listed as M-x)
    M-x goto-line n go to line n in the file
    M-x linum-mode turn on line numbering
    M-x ansi-term open a bash shell in the current buffer

与 Vim 一样,Emacs 也有一套丰富的命令,支持复杂的编辑任务。它也可以通过配置文件 ~/.emacsrc 进行配置,你可以在其中定制 emacs 的功能和外观。

Like Vim, Emacs has a rich set of commands that support complicated editing tasks. It is also configurable via a configuration file, ~/.emacsrc, in which you can customize functionality and the look of emacs.

17.4.3. Nano 编辑器

17.4.3. The Nano Editor

Nano 是另一个文本编辑器,适合对文件做较小的修改。它很容易使用,但缺少 Emacs 和 Vim 中那些使它们适用于更大、更复杂编辑任务的功能。Nano 也是像 Emacs 那样的单模式编辑器,用特殊的按键组合来执行移动光标或保存文件之类的命令。

Nano is another text editor that is useful for making smaller edits on files—it is easy to use, but lacks some of the features of Emacs and Vim that make them useful for larger, more complicated editing tasks. Nano is also a single-mode editor like Emacs that uses special key combinations for commands like moving the cursor or saving the file.

Nano 的运行方式与 Vim 和 Emacs 类似;要打开一个文件进行编辑,在命令行上输入 nano 和要编辑的文件:

Nano is run similar to Vim and Emacs; to open a file for editing type nano and the file to edit at the command line:

1
$ nano myprog.c

运行 nano 时,它会在底部列出一些最常用的命令。在这个列表中,M- 通常表示 ALT(meta)键加另一个键,C- 表示 CTRL 键。一般来说,nano 命令的控制序列比 emacs 的简单。例如,用 CTRL-x 退出 nano。

When you run nano it lists some of its most common commands at the bottom. In its listing, M- usually means the ALT (meta) key plus another key, and C- is the CTRL key. In general, nano’s control sequences for commands are simpler than in emacs. For example, CTRL-x is used to exit nano.

17.4.4. 设置默认编辑器

17.4.4. Setting your default editor

默认编辑器是其他程序在其部分功能需要启动编辑会话时自动打开的那个编辑程序。例如,Git 这样的版本控制软件会启动一个编辑器,让你为一次提交添加注释。在 Linux 系统上,可以用 echo 打印 EDITOR 和 VISUAL 环境变量,查看你的默认文本编辑器:

Your default editor program is the one that is automatically opened by other programs that start an editing session for some of their functionality. For example, revision control software such as Git starts an editor for adding comments to a commit. You can see your default text editor on Linux systems by echo’ing your EDITOR and VISUAL environment variables:

1
2
3
4
$ echo $EDITOR
nano
$ echo $VISUAL
nano

要设置默认编辑器,在你的 .bashrc 文件中添加或编辑这两个环境变量的定义,然后运行 source ~/.bashrc 使改动生效。下面是把两者都设为 vim 的例子:

To set your default editor, add or edit the definitions for these two environment variables to your .bashrc file, and then source ~/.bashrc to see the changes. Here is an example to set both to vim:

1
2
3
4
5
6
7
8
9
10
$ vim ~/.bashrc

export EDITOR=vim
export VISUAL=vim

$ source ~/.bashrc
$ echo $EDITOR
vim
$ echo $VISUAL
vim

在点文件一节中,我们会更详细地讨论环境变量和 .bashrc。

In the dot files section we talk about environment variables and .bashrc in more detail.

17.4.5. 参考资料

17.4.5. References

网上有许多学习 Vim、Emacs 或 Nano 的资源,包括一些教程视频。下面只是几个参考资料和教程的链接:

There are many resources for learning Vim, Emacs, or Nano on the web, including several tutorial videos. Here are just a few links to references and tutorials: