分类: LINUX
2010-10-02 12:22:45
Chapter 2. Editing
第2章.编辑
Now that you know how to enter and exit Emacs as well as the basics of working with files, it's time to learn how to move around in and edit files. Emacs offers lots of ways to move around in files. At first, you might find it confusing that there are so many ways to do the same thing. Be patient—as you learn, the confusion will lessen, and you'll begin to appreciate the variety of Emacs commands. The more ways you learn, the fewer keystrokes you'll need to get to the part of the file you want to edit.
现在你已经知道打开关闭Emacs以及简单的文件操作,现在介绍如果在文件中移动和编辑文件.Emacs提供很多种在文件中移动的方式.开始时会为如此多的方式做相同的事而感到困惑.耐心学习,困惑会越来越少,对各式各样的Emacs指令你会觉得很受用.方式学得越多,你移动光标到目标位置的按键操作越少.
If you want to practice commands while you're reading—which will help you learn faster—start by typing a page or two from anything you happen to have handy; the newspaper is fine. That will give you some text to work with as you learn the editing skills described in this chapter. Don't worry if you make mistakes; just keep on typing. You can correct any mistakes after you learn the basic editing skills outlined here. Learning any editor is primarily a matter of forming certain finger habits rather than memorizing what the book says. You will learn the right finger habits only if you start typing.
如果你想边学边练(这样有助于你更好的学习),那就从你手边的文稿上录入一两页文字,报纸就很好.当你学完列举的内容后,就可以修正任何编辑错误了.学用编辑器主要是培养手指习惯而不是记住书上的内容.只有开始输入才能真正养成正确的手指习惯.
When you are typing and you get to the right side of the display, you have two options. You can press Enter to go to the next line, or you can keep typing. If you type a long line and don't press Enter, Emacs waits until you reach the end of the display. Then it puts a curved arrow at the end of the line and one at the beginning of the next line as a visual indication that the next line is a continuation of the previous line (see Figure 2-1). If Emacs is run in a nongraphical environment, a backslash (\) is used instead.
当你输入到显示区的右侧边缘,有两个选择,可以按回车键转到下一行,也可以继续输入.如果输入很长的一行并且一直没有按回车键,Emacs在你输入到显示区右侧边缘,它会在行尾显示一个弯箭头,并在下一行的开始也显示一个弯箭头,来标识下一行是前一行的延续(见图2-1).如果Emacs在一个非图形环境运行,会用一个反斜杠来代替.
Figure 2-1. Graphical versions of Emacs use curved arrows to indicate that a line is continued; terminal versions use backslashes
图2-1. 图形界面的Emacs用弯前头标识连续行;终端界面用反斜杠
Refill mode is a minor mode that keeps paragraphs neat as you edit them. It is not on by default. Look at the mode line. If the word Refill appears, you are in refill mode already. If not, you can turn it on for this buffer only by typing M-x refill-mode Enter. If you decide that you don't like refill mode, type M-x refill-mode Enter again. This command is like a light switch: it toggles refill mode on and off.
重换行是一个副模式,可以在你编辑时保持文件整齐.默认情况是不启动的.查看模式行,如果能看到'ReFill',表明你已经在重换行模式下.如果没有,你可以输入'M-x refill-mode Enter'为当前缓冲区打开重换行模式.如果不习惯重换行模式,再次输入'M-x refill-mode Enter'将其关闭.这个指令就像个电灯开关一样:打开或关闭重换行模式.
You may decide that you want to enter refill mode automatically whenever you edit. We'll describe how to do so at the end of this chapter.
你或许想一进入编辑状态就打开重换行模式.我们会在本章结束的时候介绍如何实现该功能.
In some contexts, refill mode can be annoying, and it is still a work in progress according to the Emacs manual. You may prefer auto-fill mode. You enter it in the same way; type M-x auto-fill-mode Enter. The word Fill appears on the mode line.
有些时候,重换行模式令人厌烦,根据Emacs的帮助还有一种模式.你或许更喜欢auto-fill模式.可以以相同的方式进入:输入M-x auto-fill-mode Enter.可以在模式行上看到'Fill'.
When you type paragraphs, auto-fill mode formats them. When you edit them, however, auto-fill mode does not automatically reformat them. You do that yourself using the fill-paragraph command, M-q.
当你输入完一段后, auto-fill格式化该段.然而当你在编辑本段时,auto-fill模式不会自动给本段换行.你可以用段落换行指令M-q.
If you turn on refill mode and then decide to use auto-fill mode, you still have to turn refill mode off explicitly by typing M-x refill-mode Enter. Otherwise, both modes appear on the mode line, and refill mode continues its merry automatic reformatting of paragraphs, ignoring the fact that auto-fill mode has been enabled.
如果你打开了重换行模式,又想用auto-fill模式,依然要输入'M-x refill-mode Enter'来关闭重换行模式.否则两个模式都会出现在模式行,并且重换行模式会无视auto-fill模式的存在,仍旧自动格式化段落.
Watch out for one important pitfall when reformatting paragraphs. In text mode, a paragraph is any text that is indented or has a blank line before and after it. If you have a file with no blank lines, Emacs thinks it is all one long paragraph. Typing M-q takes all the text, ignoring line breaks, and makes it one long paragraph. This command is a particular problem if you have a data file, a program, or if you just prefer to write files with no blank lines. Luckily, pressing C-_ or C-x u (both for undo) magically puts things back the way they were. If you regularly create files with no blank lines, here are some suggestions:
在格式化段落时需要特殊注意一点.在文本模式,一个段落是指缩进的或者在前后各有一行空行.如果你的文件中一个空行都没有,那么Emacs会认为这个一个段落.输入M-q会忽略换行,并把它格式化成一个很大的段落.这个指令对数据文件,程序,或者一个没有空行的文件是个问题.幸运的是,按C-_或者C-x u(都表示撤消),可以将段落复原.如果经常创建没有空白行的文件,这里有一些建议:
Instead of writing in text mode, use paragraph indent text mode. In this mode, a line that starts with any blank space is a new paragraph. Type M-x paragraph-indent-text-mode to start this mode; you'll see Parindent on the mode line. See Chapter 6 for more details.
不使用文本模式,而是使用段缩进文本(paragraph indent text mode).在这个模式下,以空白字符开头的行都被认为是一个新的段落.输入'M-x paragraph-indent-text-mode'来启动该模式.在模式行会显示Parindent.第六章会有详细介绍.
Use a specific mode rather than text mode for writing. For example, use HTML mode or LaTeX mode, described in Chapter 8, for editing files of these types. These special modes redefine what a paragraph means so that the fill-paragraph command works correctly. Otherwise, these modes are very similar to text mode.
使用其他模式而不是文本模式进行编写.例如:使用HTML模式或者LaTeX模式,在第8章会介绍如何编辑这些类型的文件.这些特殊的模式重新定义了段落的含义,让段落换行指令(M-q)正确的工作,否则这些模式和文本模式没什么区别.
Instead of filling a paragraph, fill a marked section of text called a region (we'll discuss regions later in this chapter). Define the region you want to fill and press M-x fill-region Enter. This command takes a region and formats each individual paragraph within it.
不是给一段换行,而是给文本中标记的一个区域(区域,会在本章介绍什么是区域)换行.确定你要格式化的区域,然后安排按 'M-x fill-region Enter'.这个指令会将区域内的各个段格式化.
lists commands for filling text automatically and reformatting paragraphs
with auto-fill mode.
表2-1列出自动给文本换行以及自动换行模式格式化段落的指令。
|
Table 2-1. Text filling and
reformatting commands |
||
|
Keystrokes |
Command name |
Action |
|
(none) |
refill-mode重换行模式 |
Toggle refill
mode, in which Emacs automatically reformats text.开关重换行模式,可以自动格式化文本。 |
|
(none)Options |
auto-fill-mode自动换行模式 |
Toggle
auto-fill mode, in which Emacs formats paragraphs as you type them.开关自动换行模式,自动对编辑完成的段落进行格式化。 |
|
M-q |
fill-paragraph |
Reformat
paragraph.重新格式化段落。 |
|
(none)Edit |
fill-region |
Reformat
individual paragraphs within a region.重格式化区域内的各段落。 |
[1] Remember that (none) in the first column means that you type M-x followed by the command name in the second column, then press Enter to run the command. There are no default keystrokes. To use the refill-mode command, type M-x refill-mode Enter.
【1】记住第一列的(none)表示输入M-x后面输入第二列的指令,然后按回车鍵来执行指令。这里没有默认的快捷键。要使用重换行模式指令,输入“M-x refill-mode Enter”。