预计阅读本页时间:-
The readline Startup File
The default startup file is called .inputrc and must exist in your home directory if you wish to customize readline. You can change the default filename by setting the environment variable INPUTRC (see Chapter 3 for further information on environment variables).
When bash starts up, it reads the startup file (if there is one) and any settings there come into effect. The startup file is just a sequence of lines that bind a keyname to a macro or readline function name. You can also place comments in the file by preceding any line with a #.
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
You can use either an English name or a key escape sequence for the keyname. For example, to bind CTRL-T to the movement command for moving to the end of the current line, you could place Control-t: end-of-line in your .inputrc. If you wanted to use a key escape sequence you could have put "\C-t<">: end-of-line. The \C- is the escape sequence prefix for Control. The advantage of the key sequence is that you can specify a sequence of keys for an action. In our example, once readline has read this line, typing a CTRL-T will cause the cursor to move to the end of the line.
The end-of-line in the previous example is a readline function. There are over 60 functions that allow you to control everything from cursor motions to changing text and command completion (for a complete list, see the bash manual page). All of the emacs and vi editing mode commands that we looked at in this chapter have associated functions. This allows you to customize the default modes or make up completely new ones using your own key sequences.
Besides the readline functions, you can also bind a macro to a key sequence. A macro is simply a sequence of keystrokes inside single or double quotes. Typing the key sequence causes the keys in the macro to be entered as though you had typed them. For example, we could bind some text to CTRL-T; "\C-t<">: <">Curiouser and curiouser!<">. Hitting CTRL-T would cause the phrase Curiouser and curiouser! to appear on the command line.
If you want to use single or double quotes in your macros or key sequence, you can escape them by using a backslash (\). Table 2-18 lists the common escape sequences.
Table 2-18. Escape sequences
Sequence
Description
\C-
Control key prefix
\M-
Meta (Escape) key prefix
\e
The escape character
\\
The backslash character (\)
\<">
The double quote character (<">)
\'
The single quote character (')
readline also allows simple conditionals in the .inputrc. There are three directives: $if, $else, and $endif. The conditional of the $if can be an editing mode, a terminal type, or an application-specific condition.
To test for an editing mode, you can use the form mode= and test for either vi or emacs. For instance, to set up readline so that setting CTRL-T will take place only in emacs mode, you could put the following in your .inputrc:
$if mode=emacs
"\C-t": "Curiouser and curiouser!"
$endif
Likewise, to test for a terminal type, you can use the form term=. You must provide the full terminal name on the right-hand side of the test. This is useful when you need a terminal-specific key binding. You may, for instance, want to bind the function keys of a particular terminal type to key sequences.
If you have other applications that use readline, you might like to keep your bash-specific bindings separate. You can do this with the last of the conditionals. Each application that uses readline sets its own variable, which you can test for. To test for bash specifics, you could put $if bash into your .inputrc.
readline variables
readline has its own set of variables that you can set from within your .inputrc. Table 2-19 lists them.[10]
Table 2-19. readline variables
Variable
Description
bell-style
If set to none, readline never rings the bell (beeps). If set to visible, readline will attempt to use a visible bell. If set to audible, it will attempt to ring the bell. The default is audible.
comment-begin
The string to insert when the readline insert-comment command is executed. The default is a #.
completion-query-items
Determines when the user is asked to see further completions if the number of completions is greater than that given. The default is 100.
convert-meta
If set to On, converts characters with the eighth bit set to an ASCII key sequence by stripping the eighth bit and prepending an escape character. The default is On.
disable-completion
If set to On, inhibits word completion. Completion characters will be inserted into the line as if they had been mapped to self-insert. The default is Off.
editing-mode
Sets the editing mode to vi or emacs.
enable-keypad
If set to On, readline tries to enable the keyboard's application keypad when it is called. Some systems need this to enable the arrow keys. The default is Off.
expand-tilde
If set to On, tilde expansion is attempted when readline attempts word completion. The default is Off.
horizontal-scroll-mode
Set to On means that lines will scroll horizontally if you type beyond the right-hand side of the screen. The default is Off, which wraps the line onto a new screen line.
input-meta
If set to On, eight-bit input will be accepted. The default is Off. This is synonymous with meta-flag.
keymap
Sets readline's current keymap for bindings. Acceptable names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, vi-command and vi-insert. The default is emacs. Note that the value of editing-mode also affects the keymap.
mark-directories
If set to On, completed directory names have a slash appended.
mark-modified-lines
If set to On, displays an asterisk at the start of history lines that have been modified. The default is Off.
meta-flag
If set to On, eight-bit input will be accepted. The default is Off.
output-meta
If set to On, displays characters with the eighth bit set directly. The default is Off.
show-all-if-ambiguous
If set to On, words with more than one possible completion are listed instead of ringing the bell. The default is Off.
visible-stats
If set to On, a character denoting a file's type as reported by the stat system call is appended to the filename when listing possible completions. The default is Off.
To set any of the variables, you can use the set command in your .inputrc. For example, to set vi-mode when you start up, you could place the line set editing-mode vi in your .inputrc. Every time bash starts it would change to vi-mode.