说明
本文是个人学习用的非官方中英对照译文,原文: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
.csuffix is the naming convention for C code source files (e.g.,myprog.c), and a.hsuffix 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(andvi) 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 thevimtutortutorial.vimis particularly useful when working remotely over an ssh connection. It also has many advanced features and is very configurable through settings in a.vimrcconfig file.
没有必要学会 vim 的所有功能;几条基本命令就能让你开始使用 vim,并完成大多数编辑任务。
It is not necessary to learn all of vim’s features; a few basic commands will get you started with
vimand allow you to perform most editing tasks.
Vim 有两种工作模式:
Vim operates in two modes:
- 插入模式(insert mode):按键被解释为在光标处插入到文件内容中的字符。
- insert mode: keystrokes are interpreted as insertions into the file contents at the point of the cursor.
- 命令模式(command mode,或称转义模式,escape mode):按键被解释为 vim 命令,让用户能做诸如保存、退出、搜索或在文件中移动光标位置之类的事情。
- 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
ESCkey.
从命令模式切换到插入模式有很多种方法。一种方法是按 i 键。学习 vim 的最佳方式之一,是使用 Vim 教程 vimtutor。
There are many ways to switch from command mode to insert mode. One way is to press the
ikey. One of the best ways to learn i`vim` is withvimtutor, 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
vimtutorsections 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
iswitch to insert mode: what you type shows up in file at the cursor ESCswitch 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:
kmove cursor up one line (can also use arrow keys) jmove cursor down one line lmove cursor right one char hmove cursor left one char CTRL-umove cursor up one page CTRL-fmove cursor down one page SHFT-gmove cursor to end of file :<num>move cursor to start of line number <num> (e.g., :1moves cursor to start of file
译注
按 Vim 自带的帮助文档(:help CTRL-U),CTRL-u 默认向上滚动半屏;向上翻一整页是 CTRL-b,与 CTRL-f(向下翻一整页)相对应。
- 保存与退出的命令:
:w把改动保存(写入)到文件 :q退出 vim :q!不保存改动直接退出
Commands for saving and quitting:
:wsave changes (write) to file :qquit vim :q!quit without saving changes
- 切换到插入模式的命令:
i插入(insert):你输入的内容出现在光标处 a追加(append):你输入的内容出现在光标之后 o新开一行(open):在文件中光标所在行的下方新起一行 O新开一行(open):在文件中光标所在行的上方新起一行
Commands for switching to insert mode:
iinsert: what you type shows at the cursor aappend: what you type shows up after the cursor oopen: starts a new line in file below the cursor Oopen: 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:
yycopy current line (line cursor is on) <num>yycopy <num> lines starting with the one the cursor is on (e.g., 8yycopies 8 lines)ppaste what was copied after line cursor is on Ppaste what was copied before line cursor is on xremove the char at the cursor <num>xremove <num> chars starting with one at cursor (e.g., 4xremove 4 chars)dddelete the current line (p will paste it back) <num>dddelete <num> lines starting with one at cursor :uundo action, can undo multiple times :redoredo 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., /hellosearch for hello in file)nfind next occurrence of search pattern moving forward Nfind 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-vvisually select line (use arrow keys to select more lines) =reformat visually selected text (auto-indent C code!) gqreformat 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 numbershow line numbers :set nonumberdon’t show line numbers Jjoin line under cursor with line on cursor :help <cmd>help documentation about a specific vim command, :qto 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,vimexamines 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 -ain your home directory to list all your dot files and to determine if a.vimrcfile already exists:
1 | $ cd |
如果 .vimrc 文件已存在,你可以在 vim 中打开它,像编辑其他文件一样编辑它:
If the
.vimrcfile 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
.vimrcin your home directory and copy an existing example.vimrcfile of a friends or that you find online into it. For example:
1 | $ pwd |
网上有许多关于配置 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, andSHIFTkeys. 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.cin 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,再按sCTRL-x CTRL-s保存对文件的编辑 CTRL-x CTRL-c退出 Emacs
File Control (these involve multi-sequences):
CTRL-x CTRL-sis hold down theCTRLkey and typexfollowed bys
CTRL-x CTRL-ssave the file edits CTRL-x CTRL-cexit Emacs
- 移动光标
CTRL-n光标移到下一行 CTRL-p光标移到上一行 CTRL-b光标后退一个字符 CTRL-f光标前进一个字符
Moving the cursor
CTRL-nmove the cursor forward one line CTRL-pmove the cursor backward one line CTRL-bmove the cursor back one character CTRL-fmove the cursor forward one character
- 删除内容
CTRL-d删除一个字符 CTRL-k删除一行
Deleting Content
CTRL-ddeletes a character CTRL-kdeletes a line.
- 复制内容
CTRL-k复制一行 CTRL-y粘贴
Copying Content
CTRL-kcopy a line. CTRL-ypast
- 搜索
CTRL-s搜索某个模式
Search
CTRL-ssearch 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-gquit out of emacs command sequence (useful if you type in something wrong) CTRL-x-0close the current buffer window CTRL-x-1close all other buffers except the current one CTRL-x-2open buffer window below CTRL-x-3open buffer window to the right CTRL-x-oswitch to another buffer ESC-xenters meta mode (in Emacs documentation and in this table forward, it is listed as M-x)M-x goto-line ngo to line n in the file M-x linum-modeturn on line numbering M-x ansi-termopen 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
nanoand 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
nanoit lists some of its most common commands at the bottom. In its listing,M-usually means theALT(meta) key plus another key, andC-is theCTRLkey. In general, nano’s control sequences for commands are simpler than in emacs. For example,CTRL-xis 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
EDITORandVISUALenvironment variables:
1 | $ echo $EDITOR |
要设置默认编辑器,在你的 .bashrc 文件中添加或编辑这两个环境变量的定义,然后运行 source ~/.bashrc 使改动生效。下面是把两者都设为 vim 的例子:
To set your default editor, add or edit the definitions for these two environment variables to your
.bashrcfile, and thensource ~/.bashrcto see the changes. Here is an example to set both tovim:
1 | $ vim ~/.bashrc |
在点文件一节中,我们会更详细地讨论环境变量和 .bashrc。
In the dot files section we talk about environment variables and
.bashrcin 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:
- CMU 的 Vim 速查表
- Vim cheat sheet from CMU
- vim.org 上的 Vim 文档
- Vim documentation from vim.org
- gnu.org 上的 GNU Emacs 速查表
- GNU Emacs cheat sheet from gnu.org
- nano-editor.org 上的 Nano 编辑器文档
- Nano editor documentation from nano-editor.org