Bash的命令行编辑
8 命令行编辑
这一章描述了gnu命令行编辑接口的一些基本的知识。命令行编辑是由Readline库提供的,某些程序会使用这个库,包括Bash。当使用一个交互式的shell的时候,命令行编辑功能默认是开启的,除非在启动shell时使用–noediting选项。当使用-e选项来读取内置的命令时,行编辑也会被使用 (参见 Bash Builtins)。默认的设置是,这些行编辑命令与emacs相似。当然也有vi风格的行编辑接口。行编辑在任何时候均可以启用,只需要使用-o emacs 或 -o vi选项来设置内置命令 (参见 Bash Builtins),或者通过+o emacs或+o vi选项来禁用此功能。
8.1 介绍
The following paragraphs describe the notation used to represent keystrokes.
The text C-k is read as `Control-K’ and describes the character produced when the <k> key is pressed while the Control key is depressed.
The text M-k is read as `Meta-K’ and describes the character produced when the Meta key (if you have one) is depressed, and the <k> key is pressed. The Meta key is labeled <ALT> on many keyboards. On keyboards with two keys labeled <ALT> (usually to either side of the space bar), the <ALT> on the left side is generally set to work as a Meta key. The <ALT> key on the right may also be configured to work as a Meta key or may be configured as some other modifier, such as a Compose key for typing accented characters.
If you do not have a Meta or <ALT> key, or another key working as a Meta key, the identical keystroke can be generated by typing <ESC> first, and then typing <k>. Either process is known as metafying the <k> key.
The text M-C-k is read as `Meta-Control-k’ and describes the character produced by metafying C-k.
In addition, several keys have their own names. Specifically, <DEL>, <ESC>, <LFD>, <SPC>, <RET>, and <TAB> all stand for themselves when seen in this text, or in an init file (see Readline Init File). If your keyboard lacks a <LFD> key, typing <C-j> will produce the desired character. The <RET> key may be labeled <Return> or <Enter> on some keyboards.
8.2 Readline Interaction
Often during an interactive session you type in a long line of text, only to notice that the first word on the line is misspelled. The Readline library gives you a set of commands for manipulating the text as you type it in, allowing you to just fix your typo, and not forcing you to retype the majority of the line. Using these editing commands, you move the cursor to the place that needs correction, and delete or insert the text of the corrections. Then, when you are satisfied with the line, you simply press <RET>. You do not have to be at the end of the line to press <RET>; the entire line is accepted regardless of the location of the cursor within the line.
8.2.1 Readline Bare Essentials
In order to enter characters into the line, simply type them. The typed character appears where the cursor was, and then the cursor moves one space to the right. If you mistype a character, you can use your erase character to back up and delete the mistyped character.
Sometimes you may mistype a character, and not notice the error until you have typed several other characters. In that case, you can type C-b to move the cursor to the left, and then correct your mistake. Afterwards, you can move the cursor to the right with C-f.
When you add text in the middle of a line, you will notice that characters to the right of the cursor are `pushed over’ to make room for the text that you have inserted. Likewise, when you delete text behind the cursor, characters to the right of the cursor are `pulled back’ to fill in the blank space created by the removal of the text. A list of the bare essentials for editing the text of an input line follows.
- C-b
- Move back one character.
- C-f
- Move forward one character.
- <DEL> or <Backspace>
- Delete the character to the left of the cursor.
- C-d
- Delete the character underneath the cursor.
- Printing characters
- Insert the character into the line at the cursor.
- C-_ or C-x C-u
- Undo the last editing command. You can undo all the way back to an empty line.
(Depending on your configuration, the <Backspace> key be set to delete the character to the left of the cursor and the <DEL> key set to delete the character underneath the cursor, like C-d, rather than the character to the left of the cursor.)
8.2.2 Readline Movement Commands
The above table describes the most basic keystrokes that you need in order to do editing of the input line. For your convenience, many other commands have been added in addition to C-b, C-f, C-d, and <DEL>. Here are some commands for moving more rapidly about the line.
- C-a
- Move to the start of the line.
- C-e
- Move to the end of the line.
- M-f
- Move forward a word, where a word is composed of letters and digits.
- M-b
- Move backward a word.
- C-l
- Clear the screen, reprinting the current line at the top.
Notice how C-f moves forward a character, while M-f moves forward a word. It is a loose convention that control keystrokes operate on characters while meta keystrokes operate on words.
8.2.3 Readline Killing Commands
Killing text means to delete the text from the line, but to save it away for later use, usually by yanking (re-inserting) it back into the line. (`Cut’ and `paste’ are more recent jargon for `kill’ and `yank’.)
If the description for a command says that it `kills’ text, then you can be sure that you can get the text back in a different (or the same) place later.
When you use a kill command, the text is saved in a kill-ring. Any number of consecutive kills save all of the killed text together, so that when you yank it back, you get it all. The kill ring is not line specific; the text that you killed on a previously typed line is available to be yanked back later, when you are typing another line. Here is the list of commands for killing text.
- C-k
- Kill the text from the current cursor position to the end of the line.
- M-d
- Kill from the cursor to the end of the current word, or, if between words, to the end of the next word. Word boundaries are the same as those used by M-f.
- M-<DEL>
- Kill from the cursor the start of the current word, or, if between words, to the start of the previous word. Word boundaries are the same as those used by M-b.
- C-w
- Kill from the cursor to the previous whitespace. This is different than M-<DEL> because the word boundaries differ.
Here is how to yank the text back into the line. Yanking means to copy the most-recently-killed text from the kill buffer.
- C-y
- Yank the most recently killed text back into the buffer at the cursor.
- M-y
- Rotate the kill-ring, and yank the new top. You can only do this if the prior command is C-y or M-y.
8.2.4 Readline Arguments
You can pass numeric arguments to Readline commands. Sometimes the argument acts as a repeat count, other times it is the sign of the argument that is significant. If you pass a negative argument to a command which normally acts in a forward direction, that command will act in a backward direction. For example, to kill text back to the start of the line, you might type ‘M– C-k’.
The general way to pass numeric arguments to a command is to type meta digits before the command. If the first `digit’ typed is a minus sign (‘-’), then the sign of the argument will be negative. Once you have typed one meta digit to get the argument started, you can type the remainder of the digits, and then the command. For example, to give the C-d command an argument of 10, you could type ‘M-1 0 C-d’, which will delete the next ten characters on the input line.
8.2.5 Searching for Commands in the History
Readline provides commands for searching through the command history (see Bash History Facilities) for lines containing a specified string. There are two search modes: incremental and non-incremental.
Incremental searches begin before the user has finished typing the search string. As each character of the search string is typed, Readline displays the next entry from the history matching the string typed so far. An incremental search requires only as many characters as needed to find the desired history entry. To search backward in the history for a particular string, type C-r. Typing C-s searches forward through the history. The characters present in the value of the isearch-terminators variable are used to terminate an incremental search. If that variable has not been assigned a value, the <ESC> and C-J characters will terminate an incremental search. C-g will abort an incremental search and restore the original line. When the search is terminated, the history entry containing the search string becomes the current line.
To find other matching entries in the history list, type C-r or C-s as appropriate. This will search backward or forward in the history for the next entry matching the search string typed so far. Any other key sequence bound to a Readline command will terminate the search and execute that command. For instance, a <RET> will terminate the search and accept the line, thereby executing the command from the history list. A movement command will terminate the search, make the last line found the current line, and begin editing.
Readline remembers the last incremental search string. If two C-rs are typed without any intervening characters defining a new search string, any remembered search string is used.
Non-incremental searches read the entire search string before starting to search for matching history lines. The search string may be typed by the user or be part of the contents of the current line.
注:
本文翻译自《Bash Reference Manual》的第八章《Command Line Editing》
http://www.gnu.org/software/bash/manual/bashref.html
程序共用,奂奡女奨 是其中一个。使用交互式的 女奨奥奬奬 时,默认已经打开了命令行编辑,除非启动 女奨奥奬奬 时指定
了奜–noediting夢选项。当使用内部命令 read 的奜-e夢选项 夨参见 奸 头央夲奛奂奡女奨 的内部命令 read奝夬 奰夳夹 天 时也会
使用行编辑。默认情况下,行编辑命令和 奥奭奡奣女 的很相似;但也可以使用 奶奩 风格的行编辑界面。在任何时
候,都可以使用内部命令 set 夨参见 奸 头央夳央失奛内部命令 女奥奴奝夬 奰头夲 天 的奜-o emacs夢或奜-o vi夢选项来打开行编辑,
或者使用 set 的奜+o emacs夢或奜+o vi夢选项来关闭。
§ 8.1
下面几段介绍键的表示方法。
字符 C-k 读作奜Control-K夢,它表示按下 Control 键时再按 k 键所得到的字符。符号 M-k 读
作奜Meta-K夢,它表示按下 Meta 键 夨如果有这个键天 时再按 k 键所得到的字符。在很多键盘上, Meta
键都标上 ALT 。如果一个键盘有两个标为 ALT 的键 夨通常在空格键的两旁天,则一般左边的那个可以当
Meta 键使用。右边的那个 ALT 键也可以配置成 Meta 键,或者配置成其它的修饰键,例如用来输入字
母音符的 Compose 键。如果没有 Meta 或 ALT 键,也没有其它可以当成 Meta 键的,则可以先按下
ESC 再按下 k 来得到同样的键。这两种键都叫 Meta 化的 k 键。
字符 M-C-k 读作奜Meta-Control-K夢,它表示 奍奥奴奡 化的 C-k 。
此外,还有几个键有独特的名称。特别的,本文或初始化文件 夨参见 奸 夸央夳奛奒奥奡奤奬奩奮奥 的启动脚本奝夬 奰夷头 天 中
出现的 DEL 、 ESC 、 LFD 、 SPC 、 RET 和 TAB 都表示其自身。如果键盘上没有 LFD 键,输入
C-j 也会得到同样的字符。在有些键盘上, RET 键可能被标为 Return 或 Enter 键。
§ 8.2
在交互式的会话中,常常是输入了很长的一行文本,却发现这行的第一个单词拼写错了。奒奥奡奤奬奩奮奥 提
供了一套在输入时控制文本的命令,用来改正输入错误,而不必要重新输入大部分内容。使用这些编辑命
令,可以把光标移动到需要更正的地方,删除或者插入更正的内容;然后,如果文本行还令人满意,就按下
RET 。按下 RET 不一定要在行的结尾;整行都会读入,不管光标在行中的什么地方。
§ 8.2.1
要在行中输入字符,只需要按下对应的键。输入的字符在光标后出现,然后光标就向后移动一格的位
置。如果输错了一个字符,可以用删除字符后退并删除这个输错的字符。有时,输错了不会马上发现,直到
又输入了好个几个字符。这种情况下,可以输入 C-b 向左移动光标然后更正错误。接下来,可以用 C-f
向右移动光标。
uuuuuuu uuuuuuuuuuuuu u u u u u u u u u u u u u u u u u u u u u u u u u u u u u y
~
奰夷夲
草
稿
行编辑介绍
与 Readline 的交互
Readline 的基础
奂奁奓奈 中文文档
删除光标左边的字符。
取消最后一次输入命令。可以一直取消直到行为空。
向后移动一个字符 夨的位置天。
向后移动一个字符 夨的位置天。
Backspace
删除光标下面的字符。
在光标处插入该字符。
C-x
C-u
草
稿
Readline 的移动命令
移到行的开头。
移到行的结尾。
向前移动一个单词 夨的位置天 ;单词是由字母和数字构成的。
向后移动一个单词 夨的位置天。
清除屏幕,然后在顶端打印当前行。
Readline 的删除命令
从当前光标的位置删除到行的结尾。
奸 夸央夲 与 奒奅奁奄奌奉奎奅 的交互
第八章 编辑命令行
yu u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u
如果在行的中间输入,就会发现光标右边的字符被奜推过去夢以腾出空间容纳刚刚输入的文本。同样的,
删除光标下的字符时,光标右边的字符也会被奜推过来夢以填入删除后留出的空白中。下面是编辑输入文本行
用到的最基础的命令:
C-b
C-f
或
DEL
C-d
可打印字符
或
C-
夨取决于配置, Backspace 可以设为删除光标左边的字符,而 DEL 设为和 C-d 一样删除光标下面的字
符,而不是其左边的。天
§ 8.2.2
上面列出了编辑输入行时要用到的最基本的键。为了方便,除了 C-b 、 C-f 、 C-d 、 DEL 以外,
还有其它编辑命令。下面这些命令可以用来在行内部快速移动。
C-a
C-e
M-f
M-b
C-l
注意 C-f 向前移动一个字符,而 M-f 向前移动一个单词。有个很松散的习惯,就是 Control 键操纵字
符而 Meta 键操纵单词。
§ 8.2.3
删除 夨奜奫奩奬奬夢天 是指把文本从行中移除并保存下来以备后用,通常是后来重新插入 夨奜她奡奮奫夢天 到行中 夨现在
常说奜剪切夢[1] 和奜复制夢天。如果介绍命令时说它奜删除夢文本,则可以保证在另外一个地方 夨或者同一地方天 恢
复这些文本。使用删除命令时,文本被放在删除环中。后面连续删除的文本也会被放在一起,所以重新插入
时,会一下子恢复所有文本。删除环不是针对行的;在前面一行删除的文本可以在以后输入另外一行时恢
复。
下面的命令用来删除文本。
C-k
从光标的位置删除到当前单词的结尾;如果是在单词之间,则删除到下一个单词的结尾。单词界
限和 M-f 所使用的一样。
M-d
[1] 剪切的文本一般直接丢弃,而删除的会保存在删除环中。
uuuuuuu uuuuuuuuuuuuu u u u u u u u u u u u u u u u u u u u u u u u u u u u u u y
~
奰夷夳
目录
奂奁奓奈 中文文档
第八章 编辑命令行
奸 夸央夳 奒奅奁奄奌奉奎奅 的启动脚本
yu u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u u
M-DEL
M-b
C-w
下面这些命令用来在行中恢复文本。重新插入是指从删除缓存中复制最后被删除的文本。
C-y
M-y
令。
§ 8.2.4
奒奥奡奤奬奩奮奥 命令可以接受数值型参数。有时这些参数可以表示重复次数,有时数值的正负符号很重要。
如果一个通常向前进行的命令得到了一个负的参数,则它会向后进行。例如,删除到行开头的文本,可以使
用奜 M–
C-k 夢。
给命令指定参数的一般方法是在命令前面输入 Meta 化的数字。如果输入的第一个奜数位夢是负号奜 -
夢,则这个参数将是负数。开始输入了参数的第一个 Meta 化的数字后,就可以输入其余数字,然后再输入
命令。例如,给 C-d 命令以参数 10,则可以输入奜 M-1 0
C-d 夢,这会删除输入行中后面十个字符。
§ 8.2.5
奒奥奡奤奬奩奮奥 提供的可以搜索历史 夨参见 奸 夹央失奛奂奡女奨 的历史功能奝夬 奰夹夰 天 的命令以寻找包含指定字符串的行。
搜索命令有两种,增量的和非增量的。
增量搜索在用户输入搜索字符串结束前就开始搜索。第输入字符串的一个字符时,奒奥奡奤奬奩奮奥 都会显示
历史中与已经输入的字符串匹配的行。增量搜索时,实际需要输入多少字符就输入多少,就能找到想要的历
史。在历史中向后搜索包含特定字符串的行可以用 C-r ;而 C-s 表示向前搜索。isearch-terminators
变量中的字符可以用来结束增量搜索。如果这个变量没有设置值,就用 ESC 或 C-j 来结束增量搜索。
C-g 可以退出增量搜索并恢复之前的行。搜索结束以后,历史中包含搜索字符串的行就成为当前行。
如果要在历史中搜索其它匹配的行,可以根据需要再输入 C-r 或 C-s ;这会在历史中向前或向后搜
索与已经输入的搜索字符串匹配的下一行。与 奒奥奡奤奬奩奮奥 命令绑定的任何其它键序列都会结束搜索并执行搜
索到的命令。例如, RET 会结束搜索并提交一整行,所以会执行历史中的对应命令。移动命令也会结束搜
索,并把最后找到的行作为当前行后开始编辑。
奒奥奡奤奬奩奮奥 会记住最后一次增量搜索的字符串。如果输入的两个 C-r 之间没有其它搜索字符串,就会使
用已经记住的字符串。
非增量搜索在开始搜索匹配的历史行之前要读取整个搜索字符串。搜索字符串可以由用户输入,也可以
的当前行的部分内容。
从光标删除到当前单词的开头;如果是在单词之间,则删除到止上个单词的开头。单词界限和
所使用的一样。
从光标删除到上一个空白符。这和 M-DEL 使用的单词界限是不一样的。
把最近删除的文本插入到光标所在的缓存中。
循环到删除环,并插入新的顶端文本。只有当前一个命令是 C-y 或者 M-y 时才可以用这个命
Readline 的参数
草
稿
在历史中搜索命令
No Comments »
RSS feed for comments on this post. TrackBack URL