个人工具

“UbuntuHelp:HowToReadline/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示2个用户的2个中间版本)
第1行: 第1行:
----
+
{{Translation}} {{From|https://help.ubuntu.com/community/HowToReadline}} {{Languages|UbuntuHelp:HowToReadline}}
原文出处:https://help.ubuntu.com/community/HowToReadline
+
  
原文作者:UbuntuWiki
+
<br> 如果你使用Bash Shell,不难发现它的命令行编辑都是通过Readline库来实现的,也有其他很多程序使用这个库。Ubuntu和Kubuntu提供的Readline配置允许你使用一些常用键来移动光标或编辑命令行,例如,左右箭头控制键。除此之外,你可能还想要一些其他特性。实际上,Readline自身有一套丰富的键盘绑定,但是它们可能很难记住,或者有些用起来不方便。例如,Alt+d键用来删除当前光标右边的单词。键入bash内置命令bind -P可以查看所有键的绑定情况。
  
授权许可:[http://creativecommons.org/licenses/by-sa/2.0/ 创作共用协议] [http://www.gnu.org/copyleft/fdl.html GNU自由文档许可证]
+
本文描述了如何在键盘指令表中新增键和设置组合键,同时附上了下表中相关键设置的Readline配置文件。
 
+
翻译人员:denven
+
 
+
校对人员:
+
 
+
贡献者:
+
 
+
适用版本:
+
 
+
文章状态:[[等待翻译]]
+
 
+
----
+
If you use a Bash shell, you might know it uses the Readline library for editing command lines. Many other programs use it too. The Readline configuration supplied with Ubuntu and Kubuntu allows you to use some obvious keys, such as the left/right arrow keys, for moving around and editing the command line, but you might want a few features beyond those provided. In fact, Readline has a rich set of default key bindings, but they can be difficult to remember or awkward to use. For instance, <Alt>d deletes the word to the right of the cursor. Enter the Bash builtin command <code><nowiki>bind -P</nowiki></code> for a complete list of the bindings.
+
  
This article describes how to add some keys and key combinations to the repertoire. A new Readline configuration file is attached which defines the following keys:
+
{| cellspacing="0" border="1"
{|border="1" cellspacing="0"
+
|Ctrl + Arrow ||Move forwards and backwards a word at a time<sup>1</sup>
+
 
|-
 
|-
|Ctrl + Delete ||Delete word to right of cursor
+
|    组合键
 +
|     功能描述
 
|-
 
|-
|Ctrl + Backspace ||Delete word to left of cursor
+
| Ctrl + Arrow
 +
| 一次向前或向后移动一个单词<sup>1</sup>
 
|-
 
|-
|Shift + Delete ||Delete from cursor to end of line
+
| Ctrl + Delete
 +
| 删除光标右边的一个单词
 
|-
 
|-
|Shift + Backspace ||Delete from cursor to start of line
+
| Ctrl + Backspace
 +
| 删除光标左边的一个单词
 
|-
 
|-
|   Insert ||Toggle overwrite/insert mode<sup>2</sup>
+
| Shift + Delete
|}
+
| 删除光标处到行尾间的内容
 
+
 
+
<sup>1^ This has been part of the default configuration since Kubuntu release 6.06, but is included here for completeness. <br> ^2</sup> Each call to Readline starts in insert mode; the Insert key doesn't "stick" between calls.
+
 
+
A ''word'' is defined by Readline to be a sequence of letters or numbers; it is not possible to change this definition. Other parts of Ubuntu might define a word differently. For instance, Konsole by default defines the characters in ''':@-./_~''' to be part of a word when double clicking (see '''Settings/Configure Konsole''').
+
 
+
如果你使用Bash Shell,不难发现它的命令行编辑都是通过Readline库来实现的,也有其他很多程序使用这个库。Ubuntu和Kubuntu提供的Readline配置允许我们自定义一些通用键(例如,编辑命令行时使用左右箭头来控制光标移动),光有这个特性还不够,我们可能需要更多特性来满足自己的要求。尽管Readline自身有一套很完整的键盘绑定,但是它们很难记住,或者有些用起来不太方便。例如,组合键<Alt>-d用来删除当前光标右边的单词就很少有人记得。键入内置命令bind -P可以查看所有键的绑定情况。
+
 
+
本文描述了如何在键盘指令表中新增键和设置组合键,同时附上了下表中相关键设置的Readline配置文件。
+
{|border="1" cellspacing="0"
+
|   组合键 ||    功能描述
+
|-
+
|Ctrl + Arrow ||一次向前或向后移动一个单词<sup>1</sup>
+
|-
+
|Ctrl + Delete ||删除光标右边的一个单词
+
|-
+
|Ctrl + Backspace ||删除光标左边的一个单词
+
|-
+
|Shift + Delete ||删除光标处到行尾间的内容  
+
 
|-
 
|-
|Shift + Backspace ||删除光标处到行首间的内容  
+
| Shift + Backspace
 +
| 删除光标处到行首间的内容
 
|-
 
|-
|   Insert ||替换模式/插入模式切换<sup>2</sup>  
+
|    Insert
 +
| 替换模式/插入模式切换<sup>2</sup>
 
|}
 
|}
  
 
+
<br> 注1:Kubuntu自6.06版已默认使用该组合键,在这为使得例子更加完整,我们也把它列举了进来。<br>注2:每次在插入模式中才开始调用Readline,"Insert"键在各次调用间并不相互联系。
<sup>1^:Kubuntu自6.06版已默认使用该组合键,在这为使得例子更加完整,我们也把它列举了进来。<br>注^2</sup>:每次在插入模式中才开始调用Readline,"Insert"键在各次调用间并不相互联系。
+
  
 
由Readline库定义的一系列字符或者数字的组合,称为一个"单词"。"单词"一旦定义就不太可能重新定义,但在Ubuntu中的其他地方可能对它们又有不同的定义。例如,Konsole默认在双击选定单词时将''':@-./_~'''当作单词的一部分(参考 控制台中的'''Settings(设置)/Configure Konsole(配置Konsole)''')。
 
由Readline库定义的一系列字符或者数字的组合,称为一个"单词"。"单词"一旦定义就不太可能重新定义,但在Ubuntu中的其他地方可能对它们又有不同的定义。例如,Konsole默认在双击选定单词时将''':@-./_~'''当作单词的一部分(参考 控制台中的'''Settings(设置)/Configure Konsole(配置Konsole)''')。
  
----
+
==== 键盘映射 ====
==== Keyboard map(键盘映射) ====
+
Before installing the new Readline configuration file, it is necessary to configure the terminal emulator to emit a unique byte sequence for each required key combination. Konsole needs only a small change: with the default '''XTerm (XFree 4.x.x)''' keyboard table, it emits the same byte sequence for <Ctrl><Backspace> as for <Shift><Backspace>, which is just the same as for <Backspace> on its own.
+
  
Attached to this article is attachment:myKubuntu.keytab , which configures Konsole to emit the required byte sequences. The simplest way to install it is to put it in `~/.kde/share/apps/konsole` ('''~''' is the conventional notation for "my home directory"). Alternatively, you could make a system-wide change by putting the keytab in `/usr/share/apps/konsole`.
+
在安装新的Readline配置文件之前,需要对终端模拟器进行设置,使其能对设置的各组合键分别产生相应的字节序。设置时,对Konsole只需要稍做改动,因为默认的'''Xterm(XFree 4.x.x)'''键盘表,&lt;Ctrl&gt;&lt;Backspace&gt;&lt;Shift&gt;&lt;Backspace&gt;产生同样的字节序,也就相当于&lt;Backspace&gt;键。
 
+
Rather than downloading the entire table, you could download the patch in attachment:default.keytab.patch and apply it to the default keyboard table. The default keyboard table doesn't exist as a .keytab file since it's built into Konsole; instead, it's available in `/usr/share/doc/konsole/README.default.Keytab.gz`. To create the new keytab, issue these commands:
+
 
+
<pre><nowiki>
+
gzip -cd /usr/share/doc/konsole/README.default.Keytab.gz > myKubuntu.keytab
+
patch myKubuntu.keytab default.keytab.patch
+
</nowiki></pre>
+
After installing myKubuntu.keytab as outlined above, start a new Konsole session and select '''Settings/Keyboard/XTerm (myKubuntu)'''. If you're happy with this after testing, select '''Settings/Save as Default'''.
+
 
+
If you don't use the default Konsole keyboard table, and need to modify a different keytab file, a useful technique during development is to start Konsole with the command <code><nowiki>konsole --keytab myKubuntu</nowiki></code> (or whatever name you choose). This allows you to see any error messages from Konsole, for instance those that say a key combination has been defined earlier in the keytab file. To verify a key combination, a useful technique is to start vim, enter Insert mode with i, and enter <Ctrl>v followed by the key combination. See `/usr/share/doc/konsole/README.KeyTab` for details of the format of the keytab file.
+
 
+
[FIXME: not described are the GNOME terminal emulator, the Linux tty console, or classic xterm.]
+
 
+
 
+
在安装新的Readline配置文件之前,需要对终端模拟器进行设置,使其能对设置的各组合键分别产生相应的字节序。设置时,对Konsole只需要稍做改动,因为默认的'''Xterm(XFree 4.x.x)'''键盘表,<Ctrl><Backspace><Shift><Backspace>产生同样的字节序,也就相当于<Backspace>键。
+
  
 
使用 attachment:myKubuntu.keytab 文件能使Konsole产生我们需要的字节序。最简单的安装方法是把这个文件放到`~/.kde/share/apps/konsole`目录中(按照惯例,符号~表示当前用户主目录),相应地,如果将它放在`/usr/share/apps/konsole`目录中,那么这个配置文件将对系统中的所有用户起作用。
 
使用 attachment:myKubuntu.keytab 文件能使Konsole产生我们需要的字节序。最简单的安装方法是把这个文件放到`~/.kde/share/apps/konsole`目录中(按照惯例,符号~表示当前用户主目录),相应地,如果将它放在`/usr/share/apps/konsole`目录中,那么这个配置文件将对系统中的所有用户起作用。
  
 
使用前面说到的.keytab文件会修改整个键盘设置,而使用 attachment:default.keytab.patch 文件,将其放到相应目录中则可将键盘还原到默设置。这种方式会更合适,因为一旦使用.keytab文件,就会将键盘表映射Konsole中去,导致系统默认的键盘表将不复存在(而使用该patch文件我们可以进行还原)。值得庆幸的是,在`/usr/share/doc/konsole/README.default.Keytab.gz`中还有默认设置的备份。利用它可以创建新的键盘表进行还原,运行以下命令即可:
 
使用前面说到的.keytab文件会修改整个键盘设置,而使用 attachment:default.keytab.patch 文件,将其放到相应目录中则可将键盘还原到默设置。这种方式会更合适,因为一旦使用.keytab文件,就会将键盘表映射Konsole中去,导致系统默认的键盘表将不复存在(而使用该patch文件我们可以进行还原)。值得庆幸的是,在`/usr/share/doc/konsole/README.default.Keytab.gz`中还有默认设置的备份。利用它可以创建新的键盘表进行还原,运行以下命令即可:
 
+
<pre>gzip -cd /usr/share/doc/konsole/README.default.Keytab.gz &gt; myKubuntu.keytab  
<pre><nowiki>
+
gzip -cd /usr/share/doc/konsole/README.default.Keytab.gz > myKubuntu.keytab  
+
 
patch myKubuntu.keytab default.keytab.patch  
 
patch myKubuntu.keytab default.keytab.patch  
</nowiki></pre>
+
</pre>  
 
在按照上面所说的安装了myKubuntu.keytab文件后,启动一个新的Konsole会话,依次选择'''Settings(设置)/Keyboard(键盘)/XTerm (myKubuntu)''',如果你觉得新的键盘设置很适合自己,那么点击'''Settings(设置)/Save as Default(设为默认)'''将其设成默认。
 
在按照上面所说的安装了myKubuntu.keytab文件后,启动一个新的Konsole会话,依次选择'''Settings(设置)/Keyboard(键盘)/XTerm (myKubuntu)''',如果你觉得新的键盘设置很适合自己,那么点击'''Settings(设置)/Save as Default(设为默认)'''将其设成默认。
  
如果你没有使用Konsole的默认键盘设置,同时又需要修改另一个keytab文件。那么在操作过程中建议你使用这个技巧,通过命令<code><nowiki>konsole --keytab myKubuntu</nowiki></code>来启动Konsole,因为以这种方式启动可以看到Konsole键盘表的出错信息,例如它可能会提示你某个键已经被绑定。要检查键盘绑定情况,启动Vim,按i进入插入模式,在按下你要检验的组合键后接着按<Ctrl>-v即可。关于键盘表文件的具体细节请参见`/usr/share/doc/konsole/README.KeyTab`。
+
如果你没有使用Konsole的默认键盘设置,同时又需要修改另一个keytab文件。那么在操作过程中建议你使用这个技巧,通过命令<code><nowiki>konsole --keytab myKubuntu</nowiki></code>来启动Konsole,因为以这种方式启动可以看到Konsole键盘表的出错信息,例如它可能会提示你某个键已经被绑定。要检查键盘绑定情况,启动Vim,按i进入插入模式,在按下你要检验的组合键后接着按&lt;Ctrl&gt;-v即可。关于键盘表文件的具体细节请参见`/usr/share/doc/konsole/README.KeyTab`。
  
 
[特别注意:如无特别描述,文中的终端均指GNOME终端模拟器,Linux控制台或者xterm。]
 
[特别注意:如无特别描述,文中的终端均指GNOME终端模拟器,Linux控制台或者xterm。]
  
----
+
==== Readline配置 ====
==== Readline configuration(Readline配置) ====
+
After your terminal emulator is configured to emit the required byte sequences, it's time to configure Readline. Download the file attachment:inputrc and install it either in your home directory as `.inputrc` or as a replacement for the existing file `/etc/inputrc`.
+
 
+
Again, you might prefer to apply a patch instead of installing a complete replacement. Download attachment:inputrc.patch (say, to `/tmp`) and with root privilege issue the command:
+
 
+
<pre><nowiki>
+
patch --backup /etc/inputrc /tmp/inputrc.patch
+
</nowiki></pre>
+
Note this creates a backup file, most likely called `/etc/inputrc.orig` (see `patch(1)` for details).
+
 
+
Remember to start a new session before testing and, if necessary, change the keyboard table.
+
  
 
在完成终端模拟器配置成可执行自定义的字节序后,接下来配置Readline。下载 attachment:inputrc 文件将其命名成`.inputrc`放到自己主目录,或者替换`/etc/inputrc`文件即可。
 
在完成终端模拟器配置成可执行自定义的字节序后,接下来配置Readline。下载 attachment:inputrc 文件将其命名成`.inputrc`放到自己主目录,或者替换`/etc/inputrc`文件即可。
  
 
与前面类似,使用patch文件方式安装可能比采用完全替换的安装方式更适合你。下载 attachment:inputrc.patch 文件到/tmp目录,切换到root权限然后执行下面的命令:
 
与前面类似,使用patch文件方式安装可能比采用完全替换的安装方式更适合你。下载 attachment:inputrc.patch 文件到/tmp目录,切换到root权限然后执行下面的命令:
 
+
<pre>patch --backup /etc/inputrc /tmp/inputrc.patch
<pre><nowiki>
+
</pre>  
patch --backup /etc/inputrc /tmp/inputrc.patch
+
</nowiki></pre>
+
 
这样将生成一个备份文件,很可能是调用了/etc/inputrc.orig(具体请参考`path(1)`)。
 
这样将生成一个备份文件,很可能是调用了/etc/inputrc.orig(具体请参考`path(1)`)。
  
 
请记得在测试前重新启动一个会话,如果有必要的话,更改键盘表。
 
请记得在测试前重新启动一个会话,如果有必要的话,更改键盘表。
  
----
+
=== 总结 ===
=== Summary(总结) ===
+
You have modified your terminal emulator to emit the byte sequences required by your custom Readline configuration file. Now Bash and other command-line programs can use the keys defined in the table above, as well as Home, End, etc.
+
  
 
通过定制Readline配置文件,你已经修改了终端模拟器,可以让Konsole执行自定义的字节序。到此为止,键盘表中自定义的按键会跟系统中的Home,End等键一样在Bash和其他命令行程序中生效。
 
通过定制Readline配置文件,你已经修改了终端模拟器,可以让Konsole执行自定义的字节序。到此为止,键盘表中自定义的按键会跟系统中的Home,End等键一样在Bash和其他命令行程序中生效。
  
----
+
=== 相关页面 ===
=== Related Pages(相关页面) ===
+
 
 
[http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html GNU Readline Library]
 
[http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html GNU Readline Library]
  
第137行: 第73行:
  
 
----
 
----
[[等待翻译]]
 
  
----
+
[[Category:CategoryDocumentation]] [[Category:CategoryCleanup]] [[Category:UbuntuHelp]]

2008年4月23日 (三) 00:51的最新版本



如果你使用Bash Shell,不难发现它的命令行编辑都是通过Readline库来实现的,也有其他很多程序使用这个库。Ubuntu和Kubuntu提供的Readline配置允许你使用一些常用键来移动光标或编辑命令行,例如,左右箭头控制键。除此之外,你可能还想要一些其他特性。实际上,Readline自身有一套丰富的键盘绑定,但是它们可能很难记住,或者有些用起来不方便。例如,Alt+d键用来删除当前光标右边的单词。键入bash内置命令bind -P可以查看所有键的绑定情况。

本文描述了如何在键盘指令表中新增键和设置组合键,同时附上了下表中相关键设置的Readline配置文件。

   组合键     功能描述
Ctrl + Arrow 一次向前或向后移动一个单词1
Ctrl + Delete 删除光标右边的一个单词
Ctrl + Backspace 删除光标左边的一个单词
Shift + Delete 删除光标处到行尾间的内容
Shift + Backspace 删除光标处到行首间的内容
   Insert 替换模式/插入模式切换2


注1:Kubuntu自6.06版已默认使用该组合键,在这为使得例子更加完整,我们也把它列举了进来。
注2:每次在插入模式中才开始调用Readline,"Insert"键在各次调用间并不相互联系。

由Readline库定义的一系列字符或者数字的组合,称为一个"单词"。"单词"一旦定义就不太可能重新定义,但在Ubuntu中的其他地方可能对它们又有不同的定义。例如,Konsole默认在双击选定单词时将:@-./_~当作单词的一部分(参考 控制台中的Settings(设置)/Configure Konsole(配置Konsole))。

键盘映射

在安装新的Readline配置文件之前,需要对终端模拟器进行设置,使其能对设置的各组合键分别产生相应的字节序。设置时,对Konsole只需要稍做改动,因为默认的Xterm(XFree 4.x.x)键盘表,<Ctrl><Backspace>和<Shift><Backspace>产生同样的字节序,也就相当于<Backspace>键。

使用 attachment:myKubuntu.keytab 文件能使Konsole产生我们需要的字节序。最简单的安装方法是把这个文件放到`~/.kde/share/apps/konsole`目录中(按照惯例,符号~表示当前用户主目录),相应地,如果将它放在`/usr/share/apps/konsole`目录中,那么这个配置文件将对系统中的所有用户起作用。

使用前面说到的.keytab文件会修改整个键盘设置,而使用 attachment:default.keytab.patch 文件,将其放到相应目录中则可将键盘还原到默设置。这种方式会更合适,因为一旦使用.keytab文件,就会将键盘表映射Konsole中去,导致系统默认的键盘表将不复存在(而使用该patch文件我们可以进行还原)。值得庆幸的是,在`/usr/share/doc/konsole/README.default.Keytab.gz`中还有默认设置的备份。利用它可以创建新的键盘表进行还原,运行以下命令即可:

gzip -cd /usr/share/doc/konsole/README.default.Keytab.gz > myKubuntu.keytab 
patch myKubuntu.keytab default.keytab.patch 

在按照上面所说的安装了myKubuntu.keytab文件后,启动一个新的Konsole会话,依次选择Settings(设置)/Keyboard(键盘)/XTerm (myKubuntu),如果你觉得新的键盘设置很适合自己,那么点击Settings(设置)/Save as Default(设为默认)将其设成默认。

如果你没有使用Konsole的默认键盘设置,同时又需要修改另一个keytab文件。那么在操作过程中建议你使用这个技巧,通过命令konsole --keytab myKubuntu来启动Konsole,因为以这种方式启动可以看到Konsole键盘表的出错信息,例如它可能会提示你某个键已经被绑定。要检查键盘绑定情况,启动Vim,按i进入插入模式,在按下你要检验的组合键后接着按<Ctrl>-v即可。关于键盘表文件的具体细节请参见`/usr/share/doc/konsole/README.KeyTab`。

[特别注意:如无特别描述,文中的终端均指GNOME终端模拟器,Linux控制台或者xterm。]

Readline配置

在完成终端模拟器配置成可执行自定义的字节序后,接下来配置Readline。下载 attachment:inputrc 文件将其命名成`.inputrc`放到自己主目录,或者替换`/etc/inputrc`文件即可。

与前面类似,使用patch文件方式安装可能比采用完全替换的安装方式更适合你。下载 attachment:inputrc.patch 文件到/tmp目录,切换到root权限然后执行下面的命令:

patch --backup /etc/inputrc /tmp/inputrc.patch

这样将生成一个备份文件,很可能是调用了/etc/inputrc.orig(具体请参考`path(1)`)。

请记得在测试前重新启动一个会话,如果有必要的话,更改键盘表。

总结

通过定制Readline配置文件,你已经修改了终端模拟器,可以让Konsole执行自定义的字节序。到此为止,键盘表中自定义的按键会跟系统中的Home,End等键一样在Bash和其他命令行程序中生效。

相关页面

GNU Readline Library

Bash Reference Manual

Andrew's (barebones) guide to terminals and keymappings