特殊:Badtitle/NS100:HowToReadline
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/HowToReadline }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/af | • {{#if: UbuntuHelp:HowToReadline|Afrikaans| [[::HowToReadline/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ar | • {{#if: UbuntuHelp:HowToReadline|العربية| [[::HowToReadline/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/az | • {{#if: UbuntuHelp:HowToReadline|azərbaycanca| [[::HowToReadline/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/bcc | • {{#if: UbuntuHelp:HowToReadline|جهلسری بلوچی| [[::HowToReadline/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/bg | • {{#if: UbuntuHelp:HowToReadline|български| [[::HowToReadline/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/br | • {{#if: UbuntuHelp:HowToReadline|brezhoneg| [[::HowToReadline/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ca | • {{#if: UbuntuHelp:HowToReadline|català| [[::HowToReadline/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/cs | • {{#if: UbuntuHelp:HowToReadline|čeština| [[::HowToReadline/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/de | • {{#if: UbuntuHelp:HowToReadline|Deutsch| [[::HowToReadline/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/el | • {{#if: UbuntuHelp:HowToReadline|Ελληνικά| [[::HowToReadline/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/es | • {{#if: UbuntuHelp:HowToReadline|español| [[::HowToReadline/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/fa | • {{#if: UbuntuHelp:HowToReadline|فارسی| [[::HowToReadline/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/fi | • {{#if: UbuntuHelp:HowToReadline|suomi| [[::HowToReadline/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/fr | • {{#if: UbuntuHelp:HowToReadline|français| [[::HowToReadline/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/gu | • {{#if: UbuntuHelp:HowToReadline|ગુજરાતી| [[::HowToReadline/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/he | • {{#if: UbuntuHelp:HowToReadline|עברית| [[::HowToReadline/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/hu | • {{#if: UbuntuHelp:HowToReadline|magyar| [[::HowToReadline/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/id | • {{#if: UbuntuHelp:HowToReadline|Bahasa Indonesia| [[::HowToReadline/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/it | • {{#if: UbuntuHelp:HowToReadline|italiano| [[::HowToReadline/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ja | • {{#if: UbuntuHelp:HowToReadline|日本語| [[::HowToReadline/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ko | • {{#if: UbuntuHelp:HowToReadline|한국어| [[::HowToReadline/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ksh | • {{#if: UbuntuHelp:HowToReadline|Ripoarisch| [[::HowToReadline/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/mr | • {{#if: UbuntuHelp:HowToReadline|मराठी| [[::HowToReadline/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ms | • {{#if: UbuntuHelp:HowToReadline|Bahasa Melayu| [[::HowToReadline/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/nl | • {{#if: UbuntuHelp:HowToReadline|Nederlands| [[::HowToReadline/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/no | • {{#if: UbuntuHelp:HowToReadline|norsk| [[::HowToReadline/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/oc | • {{#if: UbuntuHelp:HowToReadline|occitan| [[::HowToReadline/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/pl | • {{#if: UbuntuHelp:HowToReadline|polski| [[::HowToReadline/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/pt | • {{#if: UbuntuHelp:HowToReadline|português| [[::HowToReadline/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ro | • {{#if: UbuntuHelp:HowToReadline|română| [[::HowToReadline/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/ru | • {{#if: UbuntuHelp:HowToReadline|русский| [[::HowToReadline/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/si | • {{#if: UbuntuHelp:HowToReadline|සිංහල| [[::HowToReadline/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/sq | • {{#if: UbuntuHelp:HowToReadline|shqip| [[::HowToReadline/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/sr | • {{#if: UbuntuHelp:HowToReadline|српски / srpski| [[::HowToReadline/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/sv | • {{#if: UbuntuHelp:HowToReadline|svenska| [[::HowToReadline/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/th | • {{#if: UbuntuHelp:HowToReadline|ไทย| [[::HowToReadline/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/tr | • {{#if: UbuntuHelp:HowToReadline|Türkçe| [[::HowToReadline/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/vi | • {{#if: UbuntuHelp:HowToReadline|Tiếng Việt| [[::HowToReadline/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/yue | • {{#if: UbuntuHelp:HowToReadline|粵語| [[::HowToReadline/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/zh | • {{#if: UbuntuHelp:HowToReadline|中文| [[::HowToReadline/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/zh-hans | • {{#if: UbuntuHelp:HowToReadline|中文(简体)| [[::HowToReadline/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:HowToReadline | UbuntuHelp:HowToReadline | {{#if: | :}}HowToReadline}}/zh-hant | • {{#if: UbuntuHelp:HowToReadline|中文(繁體)| [[::HowToReadline/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:HowToReadline|:HowToReadline|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :HowToReadline/zh | | {{#ifexist: HowToReadline/zh | | {{#ifeq: {{#titleparts:HowToReadline|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:HowToReadline|1|-1|}} | zh | | }}
<<Include(Tag/ContentCleanup)>>
<<Include(Tag/StyleCleanup)>>
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 bind -P
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:
Move forwards and backwards a word at a time1 | |
Delete word to right of cursor | |
Delete word to left of cursor | |
Delete from cursor to end of line | |
Delete from cursor to start of line | |
Toggle overwrite/insert mode2 |
1 This has been part of the default configuration since Kubuntu release 6.06,
but is included here for completeness.
<
>
2 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).
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`. 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:
gzip -cd /usr/share/doc/konsole/README.default.Keytab.gz > myKubuntu.keytab patch myKubuntu.keytab default.keytab.patch
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 konsole --keytab myKubuntu
(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 configuration
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:
patch --backup /etc/inputrc /tmp/inputrc.patch
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.
Other terminal emulators
The following line in /etc/inputrc makes the GNOME terminal emulator use <Ctrl><Delete> to delete the word in front of the cursor:
"\e[3;5~": kill-word
However, I have not yet found a way to use <Ctrl><Backspace>.
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.