本文为 Bash Reference Manual第8章:Command Line Editing ,第3节:Readline Init File 的读书笔记。
完整的笔记目录参见Bash学习笔记总目录。
虽然 Readline 库默认自带一组类似 Emacs 的键绑定,但也可以使用不同的一组键绑定。任何用户都可以通过在 inputrc 文件中放置命令来自定义使用 Readline 的程序,通常该文件位于用户的主目录中。该文件的名称取自 shell 变量 INPUTRC 的值。如果该变量未设置,则默认值为 ~/.inputrc。如果该文件不存在或无法读取,Readline 会查找 /etc/inputrc。bind 内置命令也可以用于设置 Readline 的键绑定和变量。请参见 Bash 内置命令。
bash
$ echo $INPUTRC
$ file ~/.inputrc
/home/vagrant/.inputrc: cannot open `/home/vagrant/.inputrc' (No such file or directory)
$ file /etc/inputrc
/etc/inputrc: ASCII text
$ wc -l /etc/inputrc
42 /etc/inputrc
$ cat /etc/inputrc
# do not bell on tab-completion
#set bell-style none
set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
# Completed names which are symbolic links to
# directories have a slash appended.
set mark-symlinked-directories on
$if mode=emacs
# for linux console and RH/Debian xterm
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# commented out keymappings for pgup/pgdown to reach begin/end of history
#"\e[5~": beginning-of-history
#"\e[6~": end-of-history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": overwrite-mode
"\e[5C": forward-word
"\e[5D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word
# for rxvt
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word
# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
"\eOH": beginning-of-line
"\eOF": end-of-line
# for freebsd console
"\e[H": beginning-of-line
"\e[F": end-of-line
$endif
当使用 Readline 库的程序启动时,Readline 会读取初始化文件,并设置其中包含的所有变量和键绑定。
此外,C-x C-r 命令会重新读取该初始化文件,从而使您对其所做的任何更改生效。
8.3.1 Readline Init File Syntax
Readline 初始化文件只允许少量基本构造。空行会被忽略。以"#"开头的行是注释。以"$"开头的行表示条件构造(参见条件初始化构造)。其他行表示变量设置和键绑定。
变量设置
你可以通过在初始化文件中使用 set 命令更改 Readline 中变量的值来修改 Readline 的运行时行为:
bash
set variable value
例如将默认的 Emacs 的按键绑定更改为使用 vi 行编辑命令的方法:
bash
set editing-mode vi
变量名和值(在适用的情况下)不区分大小写。不被识别的变量名将被忽略。
如果布尔变量(可以设置为开或关)的值为null、空、on(不区分大小写)或1,则设置为开。任何其他值都会使变量设置为关。
bind -V 命令列出当前的 Readline 变量名和值。请参见 Bash 内置命令。
bash
$ bind -V
bind-tty-special-chars is set to `on'
blink-matching-paren is set to `off'
byte-oriented is set to `off'
colored-completion-prefix is set to `off'
colored-stats is set to `off'
completion-ignore-case is set to `off'
completion-map-case is set to `off'
convert-meta is set to `off'
disable-completion is set to `off'
echo-control-characters is set to `on'
enable-bracketed-paste is set to `on'
enable-keypad is set to `off'
enable-meta-key is set to `on'
expand-tilde is set to `off'
history-preserve-point is set to `off'
horizontal-scroll-mode is set to `off'
input-meta is set to `on'
mark-directories is set to `on'
mark-modified-lines is set to `off'
mark-symlinked-directories is set to `on'
match-hidden-files is set to `on'
menu-complete-display-prefix is set to `off'
meta-flag is set to `on'
output-meta is set to `on'
page-completions is set to `on'
prefer-visible-bell is set to `on'
print-completions-horizontally is set to `off'
revert-all-at-newline is set to `off'
show-all-if-ambiguous is set to `off'
show-all-if-unmodified is set to `off'
show-mode-in-prompt is set to `off'
skip-completed-text is set to `off'
visible-stats is set to `off'
bell-style is set to `audible'
comment-begin is set to `#'
completion-display-width is set to `-1'
completion-prefix-display-length is set to `0'
completion-query-items is set to `100'
editing-mode is set to `emacs'
emacs-mode-string is set to `@'
history-size is set to `1000'
keymap is set to `emacs'
keyseq-timeout is set to `500'
vi-cmd-mode-string is set to `(cmd)'
vi-ins-mode-string is set to `(ins)'
许多运行时行为可以通过以下变量进行更改。
active-region-start-color
一个字符串变量,用于控制在活动区域显示文本时的文本颜色和背景(请参见下文对 enable-active-region 的描述)。这个字符串在显示时不能占用显示屏上的任何物理字符位置,因此它应该仅由终端转义序列组成。在显示活动区域的文本之前,该字符串会输出到终端。每当终端类型发生变化时,该变量会重置为默认值。默认值是将终端设置为高亮模式的字符串,从终端的 terminfo 描述中获取。一个示例值可能是 'e[01;33m'。
bash
$ bind -v |grep active-region-start-color
$
## 前景色为黄色
$ echo -e '\e[01;33m'
$ reset
## maximum number of colors on screen
$ tput colors
8
active-region-end-color
一个字符串变量,用于"撤销"active-region-start-color 的效果,并在在活动区域显示文本后恢复"正常"的终端显示外观。该字符串不能占据显示屏上的任何实际字符位置,因此它应该仅由终端转义序列组成。在显示活动区域的文本后,它会输出到终端。每当终端类型改变时,此变量会重置为默认值。默认值是从终端的 terminfo 描述中获得的用于将终端从高亮模式恢复的字符串。一个示例值可能是 'e[0m'。
bash
$ echo -e '\e[01;33m'"My color is yellow!"'\e[0m'
My color is yellow!
$ bind -v |grep active-region-end-color
$
💡 关于终端颜色请参见博客文章终端的颜色控制。
bell-style
控制当 Readline 想要响终端铃时的行为。如果设置为"none",Readline 永远不会响铃。如果设置为"visible",Readline 在可用时使用可见铃。如果设置为"audible"(默认值),Readline 会尝试响终端的铃声。
bash
$ bind -v|grep bell-style
set bell-style audible
bind-tty-special-chars
如果设置为"on"(默认值),Readline 会尝试将内核终端驱动程序特别处理的控制字符绑定到它们在 Readline 中的等效键。这些会覆盖此处描述的默认 Readline 绑定。在 Bash 提示符下输入"stty -a"即可查看当前终端设置,包括特殊控制字符(通常是 cchars)。
bash
$ bind -v |grep bind-tty-special-chars
set bind-tty-special-chars on
$ stty -a
speed 38400 baud; rows 53; columns 172; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
$ stty -a|grep cc|grep cchars
$
blink-matching-paren
如果设置为"开",Readline 会在插入闭括号时尝试短暂地将光标移动到对应的开括号。默认值为"关"。
bash
$ bind -v |grep blink-matching-paren
set blink-matching-paren off
colored-completion-prefix
如果设置为"on",在列出补全项时,Readline 会使用不同的颜色显示可能补全集的公共前缀。颜色定义取自 LS_COLORS 环境变量的值。如果 LS_COLORS 中为自定义后缀"readline-colored-completion-prefix"定义了颜色,Readline 会使用该颜色显示公共前缀,而不是默认颜色。默认值为"off"。
bash
$ bind -v |grep colored-completion-prefix
set colored-completion-prefix off
$ echo $LS_COLORS | sed 's/:/\n/g' | sort
*.7z=01;31
*.aac=01;36
*.ace=01;31
*.alz=01;31
*.arc=01;31
*.arj=01;31
*.asf=01;35
*.au=01;36
*.avi=01;35
bd=40;33;01
*.bmp=01;35
*.bz=01;31
*.bz2=01;31
ca=30;41
*.cab=01;31
cd=40;33;01
*.cgm=01;35
*.cpio=01;31
*.deb=01;31
di=01;34
*.dl=01;35
do=01;35
*.dwm=01;31
*.dz=01;31
*.ear=01;31
*.emf=01;35
*.esd=01;31
ex=01;32
*.flac=01;36
*.flc=01;35
*.fli=01;35
*.flv=01;35
*.gif=01;35
*.gl=01;35
*.gz=01;31
*.jar=01;31
*.jpeg=01;35
*.jpg=01;35
*.lha=01;31
ln=01;36
*.lrz=01;31
*.lz=01;31
*.lz4=01;31
*.lzh=01;31
*.lzma=01;31
*.lzo=01;31
*.m2v=01;35
*.m4a=01;36
*.m4v=01;35
mh=00
mi=01;37;41
*.mid=01;36
*.midi=01;36
*.mjpeg=01;35
*.mjpg=01;35
*.mka=01;36
*.mkv=01;35
*.mng=01;35
*.mov=01;35
*.mp3=01;36
*.mp4=01;35
*.mp4v=01;35
*.mpc=01;36
*.mpeg=01;35
*.mpg=01;35
*.nuv=01;35
*.oga=01;36
*.ogg=01;36
*.ogm=01;35
*.ogv=01;35
*.ogx=01;35
*.opus=01;36
or=40;31;01
ow=34;42
*.pbm=01;35
*.pcx=01;35
*.pgm=01;35
pi=40;33
*.png=01;35
*.ppm=01;35
*.qt=01;35
*.ra=01;36
*.rar=01;31
*.rm=01;35
*.rmvb=01;35
*.rpm=01;31
rs=0
*.rz=01;31
*.sar=01;31
sg=30;43
so=01;35
*.spx=01;36
st=37;44
su=37;41
*.svg=01;35
*.svgz=01;35
*.swm=01;31
*.t7z=01;31
*.tar=01;31
*.taz=01;31
*.tbz=01;31
*.tbz2=01;31
*.tga=01;35
*.tgz=01;31
*.tif=01;35
*.tiff=01;35
*.tlz=01;31
tw=30;42
*.txz=01;31
*.tz=01;31
*.tzo=01;31
*.tzst=01;31
*.vob=01;35
*.war=01;31
*.wav=01;36
*.webm=01;35
*.webp=01;35
*.wim=01;31
*.wmv=01;35
*.xbm=01;35
*.xcf=01;35
*.xpm=01;35
*.xspf=01;36
*.xwd=01;35
*.xz=01;31
*.yuv=01;35
*.z=01;31
*.zip=01;31
*.zoo=01;31
*.zst=01;31
colored-stats
如果设置为"开",Readline 会使用不同的颜色显示可能的补全选项,以指示其文件类型。颜色定义取自 LS_COLORS 环境变量的值。默认是"关"。
bash
$ bind -v |grep colored-stats
set colored-stats off
comment-begin
insert-comment 命令在行首插入的字符串。默认值是"#"。
bash
$ bind -v |grep comment-begin
set comment-begin #
completion-display-width
在执行补全时用于显示可能匹配项的屏幕列数。如果值小于0或大于终端屏幕宽度,则会被忽略。值为0会导致每行显示一个匹配项。默认值为-1。
bash
$ bind -v |grep completion-display-width
set completion-display-width -1
completion-ignore-case
如果设置为"开启",Readline 将以不区分大小写的方式进行文件名匹配和补全。默认值为"关闭"。
bash
$ bind -v |grep completion-ignore-case
set completion-ignore-case off
completion-map-case
如果设置为"开启",并且启用了 completion-ignore-case,Readline 在执行不区分大小写的文件名匹配和补全时,会将连字符('-')和下划线('_')视为等同。默认值为"关闭"。
bash
$ bind -v |grep completion-map-case
set completion-map-case off
completion-prefix-display-length
显示的可能补全列表中共同前缀的最大字符长度。当设置为大于零的值时,Readline 在显示可能的补全项时,会用省略号替换超过该长度的共同前缀。如果补全项以句点开头,并且 Readline 正在补全文件名,它将使用三个下划线而不是省略号。
bash
$ bind -v |grep completion-prefix-display-length
set completion-prefix-display-length 0
completion-query-items
可能完成的数量决定了何时提示用户是否应该显示可能的列表。如果可能的完成数量大于或等于该值,Readline 会询问用户是否希望查看这些完成;否则,Readline 会直接列出完成项。该变量必须设置为大于或等于零的整数值。值为零表示 Readline 永远不应询问;负值会被视为零。默认限制为 100。
bash
$ bind -v |grep completion-query-items
set completion-query-items 100
$ cd /usr/include
$ ls *.h<Tab>
Display all 122 possibilities? (y or n)
convert-meta
如果设置为"开启",Readline 会将读取到的第八位被设置的字符转换为 ASCII 键序列,方法是清除第八位并在前面加一个 ESC 字符,将其转换为带有 Meta 前缀的键序列。默认值为"开启",但如果区域设置包含可能包含第八位设置字节的字符,Readline 会将其设置为"关闭"。该变量依赖于 LC_CTYPE 区域设置类别,如果区域设置更改,该变量可能会随之变化。该变量还会影响键绑定;详见下面的 force-meta-prefix 描述。
bash
$ bind -v |grep convert-meta
set convert-meta off
disable-completion
如果设置为"开",Readline 会禁止单词自动补全。补全字符会像已映射到自插入一样被插入到行中。默认值是"关"。
bash
$ bind -v |grep disable-completion
set disable-completion off
echo-control-characters
当设置为"开启"时,在那些表明支持该功能的操作系统上,Readline 会回显一个与从键盘生成的信号相对应的字符。默认设置为"开启"。
bash
$ bind -v |grep echo-control-characters
set echo-control-characters on
editing-mode
editing-mode 变量控制默认的按键绑定集。默认情况下,Readline 启动时使用 emacs 编辑模式,在该模式下按键操作最类似于 Emacs。此变量可以设置为 'emacs' 或 'vi'。
bash
$ bind -v |grep editing-mode
set editing-mode emacs
emacs-mode-string
如果启用了 show-mode-in-prompt 变量,当 Emacs 编辑模式处于活动状态时,这个字符串会在主提示符的最后一行之前立即显示。其值会像键绑定一样被展开,因此可以使用标准的元键和控制键前缀以及反斜杠转义序列。'1' 和 '2' 转义用于标记非打印字符序列的开始和结束,可以用于将终端控制序列嵌入到模式字符串中。默认值是 '@'。
bash
$ bind -v |grep emacs-mode-string
set emacs-mode-string @
enable-active-region
point 是当前光标位置,而 mark 指的是已保存的光标位置(参见移动命令)。point 和 mark 之间的文本称为区域。当此变量设置为"开启"时,Readline 允许某些命令将区域指定为活动状态。当区域处于活动状态时,Readline 会使用 active-region-start-color 的值突出显示区域中的文本,默认为启用终端高亮模式的字符串。活动区域会显示由括号粘贴插入的文本以及通过增量和非增量历史搜索找到的任何匹配文本。默认值为"开启"。
bash
$ bind -v |grep enable-active-region
$
enable-bracketed-paste
当设置为"开启"时,Readline 会将终端配置为将每次粘贴的内容作为单个字符字符串插入编辑缓冲区,而不是将每个字符都视为从键盘读取的。这被称为将终端置于括号粘贴模式;它可以防止 Readline 执行与粘贴文本中出现的键序列绑定的任何编辑命令。默认设置为"开启"。
bash
$ bind -v |grep enable-bracketed-paste
set enable-bracketed-paste on
enable-keypad
当设置为"开启"时,Readline 会在调用时尝试启用应用程序小键盘。一些系统需要这样才能启用箭头键。默认设置是"关闭".
bash
$ bind -v |grep enable-keypad
set enable-keypad off
enable-meta-key
当设置为"开启"时,Readline 会尝试在被调用时启用终端声称支持的任何元修饰键。在许多终端上,Meta 键用于发送八位字符;该变量检查终端是否具有能力,表明如果在输入字符时按住 Meta 键,可以启用或禁用一个模式,该模式会将字符的第八位 (0200) 设置为 1(即元字符)。默认值为"开启"。
bash
$ bind -v |grep enable-meta-key
set enable-meta-key on
expand-tilde
如果设置为"开启",Readline 在尝试单词补全时会尝试波浪号展开。默认设置是"关闭"。
bash
$ bind -v |grep expand-tilde
set expand-tilde off
force-meta-prefix
如果设置为"开启",Readline 会在绑定包含 M- 或 Meta- 的按键序列时修改其行为(参见 Readline 初始化文件语法中的按键绑定),将形如 M-C 或 Meta-C 的按键序列转换为两字符序列 ESC C(添加 meta 前缀)。如果 force-meta-prefix 设置为"关闭"(默认值),Readline 会使用 convert-meta 变量的值来决定是否执行此转换:如果 convert-meta 为"开启",Readline 会执行上述转换;如果为"关闭",Readline 会通过设置第八位(0200)将 C 转换为 meta 字符。默认值为"关闭"。
bash
$ bind -v |grep force-meta-prefix
$
history-preserve-point
如果设置为"开",历史代码会尝试将点(当前光标位置)放置在每条使用 previous-history 或 next-history 检索的历史记录行的相同位置。默认值为"关"。
bash
$ bind -v |grep history-preserve-point
set history-preserve-point off
history-size
设置保存在历史列表中的最大历史记录条目数。如果设置为零,则会删除任何现有的历史记录条目,并且不会保存新的条目。如果设置为小于零的值,则历史记录条目数不受限制。默认情况下,Bash 将历史记录条目的最大数量设置为 HISTSIZE shell 变量的值。如果尝试将历史记录大小设置为非数字值,最大历史记录条目数将被设置为 500。
bash
$ bind -v |grep history-size
set history-size 1000
$ echo $HISTSIZE
1000
horizontal-scroll-mode
将此变量设置为"开启"意味着,当被编辑的行长度超过屏幕宽度时,行文本将在单行屏幕上水平滚动,而不会换到新的一行。对于高度为1的终端,此变量会自动设置为"开启"。默认情况下,该变量设置为"关闭"。
bash
$ bind -v |grep horizontal-scroll-mode
set horizontal-scroll-mode off
input-meta
如果设置为"开",Readline 将启用八位输入(也就是说,它不会清除读取字符中的第八位),无论终端声称其支持什么。默认值为"关",但如果本地环境包含可能包含第八位字节的字符编码,Readline 会将其设置为"开"。此变量依赖于 LC_CTYPE 本地环境类别,其值可能随本地环境的变化而变化。meta-flag 这个名称是 input-meta 的同义词。
bash
$ bind -v |grep input-meta
set input-meta on
isearch-terminators
一串字符,用于终止增量搜索,而不会随后将该字符作为命令执行(参见在历史中搜索命令)。如果该变量没有被赋值,字符 ESC 和 C-j 将终止增量搜索。
bash
$ bind -v |grep isearch-terminators
$
keymap
设置 Readline 对当前键绑定命令的键盘映射的认知。内置的键盘映射名称有 emacs、emacs-standard、emacs-meta、emacs-ctlx、vi、vi-move、vi-command 和 vi-insert。vi 等同于 vi-command(vi-move 也是其同义词);emacs 等同于 emacs-standard。应用程序可以添加其他名称。默认值是 emacs;editing-mode 变量的值也会影响默认键盘映射。
bash
$ bind -v |grep keymap
set keymap emacs
keyseq-timeout
指定 Readline 在读取不明确的按键序列时等待字符的时间(这种序列可能通过已读取的输入形成完整按键序列,也可能需要额外输入才能完成更长的按键序列)。如果在超时时间内 Readline 没有收到任何输入,它将使用较短但完整的按键序列。Readline 使用此值来确定当前输入源(默认是 rl_instream)上是否有可用输入。该值以毫秒为单位,因此值为 1000 表示 Readline 会等待一秒钟以获取额外输入。如果将此变量设置为小于或等于零的值,或者设置为非数字值,Readline 会一直等待,直到按下另一个按键才决定完成哪个按键序列。默认值为 500。
bash
$ bind -v |grep keyseq-timeout
set keyseq-timeout 500
mark-directories
如果设置为"开启",已完成的目录名称将添加斜杠。默认设置为"开启"。
bash
$ bind -v |grep mark-directories
set mark-directories on
mark-modified-lines
当此变量被设置为"开启"时,Readline 会在已修改的历史行开头显示一个星号('*')。该变量默认是"关闭"的。
bash
$ bind -v |grep mark-modified-lines
set mark-modified-lines off
mark-symlinked-directories
如果设置为"开启",完成的名称如果是指向目录的符号链接,将会在末尾加上斜杠,这取决于 mark-directories 的值。默认是"关闭"。
bash
$ bind -v |grep mark-symlinked-directories
set mark-symlinked-directories on
match-hidden-files
当此变量设置为"on"时,Readline 在执行文件名补全时会匹配以"."(隐藏文件)开头的文件名。如果设置为"off",用户必须在要补全的文件名中包含前导的"."。该变量默认设置为"on"。
bash
$ bind -v |grep match-hidden-files
set match-hidden-files on
menu-complete-display-prefix
如果设置为"开",菜单补全会在循环浏览可能的补全列表之前显示该列表的公共前缀(可能为空)。默认设置为"关"。
bash
$ bind -v |grep menu-complete-display-prefix
set menu-complete-display-prefix off
output-meta
如果设置为"开",Readline 会直接显示第八位设置的字符,而不是以带有 meta 前缀的转义序列显示。默认值为"关",但如果语言环境包含编码可能包含第八位设置字节的字符,Readline 会将其设置为"开"。该变量依赖于 LC_CTYPE 语言环境类别,如果语言环境发生变化,其值可能会改变。
bash
$ bind -v |grep output-meta
set output-meta on
page-completions
如果设置为"开",Readline 会使用类似于 more(1) 的内部分页器一次显示一屏可能的补全项。该变量默认设置为"开"。
bash
$ bind -v |grep page-completions
set page-completions on
prefer-visible-bell
参看bell-style。
print-completions-horizontally
如果设置为"开启",Readline 会以水平字母顺序显示匹配项的补全,而不是纵向显示。默认是"关闭"。
bash
$ bind -v |grep print-completions-horizontally
set print-completions-horizontally off
revert-all-at-newline
如果设置为"开启",Readline 在执行 accept-line 时会撤销对历史行的所有更改后再返回。默认情况下,历史行可以被修改,并且在多次调用 readline() 时保留各自的撤销列表。默认值为"关闭"。
bash
$ bind -v |grep revert-all-at-newline
set revert-all-at-newline off
search-ignore-case
如果设置为"开",Readline 会以不区分大小写的方式执行增量和非增量的历史记录搜索。默认值为"关"。
bash
$ bind -v |grep search-ignore-case
$
show-all-if-ambiguous
这会改变补全函数的默认行为。如果设置为"开启",当一个单词有多个可能的补全时,会立即列出匹配项,而不是发出提示音。默认值为"关闭"。
bash
$ bind -v |grep show-all-if-ambiguous
set show-all-if-ambiguous off
show-all-if-unmodified
这会以类似于 show-all-if-ambiguous 的方式改变补全的默认行为。如果设置为"on",对于那些没有任何可能的部分补全的多个可能补全(可能的补全没有共同前缀)的单词,会立即列出匹配项,而不是发出提示音。默认值是"off"。
bash
$ bind -v |grep show-all-if-unmodified
set show-all-if-unmodified off
show-mode-in-prompt
如果设置为"开启",则在提示的开头添加一个字符串以指示编辑模式:emacs、vi命令或vi插入模式。模式字符串可以由用户设置(例如,emacs-mode-string)。默认值为"关闭"。
bash
$ bind -v |grep show-mode-in-prompt
set show-mode-in-prompt off
skip-completed-text
如果设置为"开启",当在行中插入单个匹配项时,这会改变默认的补全行为。它仅在在单词中间执行补全时才生效。如果启用,Readline 不会插入与当前光标后单词中字符相匹配的补全字符,因此光标后面的单词部分不会被重复。例如,如果启用此功能,当光标位于"Makefile"中的第一个"e"之后尝试补全时,结果将是"Makefile",而不是"Makefilefile",假设只有一个可能的补全。默认值为"关闭"。
bash
$ bind -v |grep skip-completed-text
set skip-completed-text off
vi-cmd-mode-string
如果启用了 show-mode-in-prompt 变量,当 vi 编辑模式处于命令模式时,这个字符串会在主提示符的最后一行之前立即显示。该值会像键绑定一样展开,因此可以使用标准的元键和控制键前缀以及反斜杠转义序列。'1' 和 '2' 转义用于标记非打印字符序列的开始和结束,可用于将终端控制序列嵌入到模式字符串中。默认值为"(cmd)"。
bash
$ bind -v |grep show-mode-in-prompt
set show-mode-in-prompt off
$ bind -v |grep vi-cmd-mode-string
set vi-cmd-mode-string (cmd)
vi-ins-mode-string
如果启用了 show-mode-in-prompt 变量,当 vi 编辑模式处于活动状态且处于插入模式时,该字符串会显示在主提示符的最后一行之前。其值会像键绑定一样展开,因此可以使用常规的元键和控制键前缀以及反斜杠转义序列。'1' 和 '2' 转义序列用来标记不可打印字符序列的开始和结束,可以用于将终端控制序列嵌入模式字符串中。默认值为 '(ins)'。
bash
$ bind -v |grep show-mode-in-prompt
set show-mode-in-prompt off
$ bind -v |grep vi-ins-mode-string
set vi-ins-mode-string (ins)
visible-stats
如果设置为"开启",在列出可能的补全时,会在文件名后附加一个表示文件类型的字符。默认是"关闭"。
bash
$ bind -v |grep visible-stats
set visible-stats off
按键绑定
在初始化文件中控制键绑定的语法很简单。首先,你需要找到想要更改的命令的名称。接下来的部分包含命令名称、默认键绑定(如果有的话)以及命令功能简要说明的表格。
一旦你知道了命令的名称,只需在初始化文件中输入你想要绑定该命令的键名,后跟冒号,然后是命令名称。键名和冒号之间不能有空格------否则它会被解释为键名的一部分。键名可以以不同的方式表示,取决于你觉得哪种方式最方便。
除了命令名称之外,Readline 还允许将按键绑定到在按下时插入的字符串(宏)。宏和命令的区别在于宏用单引号或双引号括起来。
bind -p 命令以可以直接放入初始化文件的格式显示 Readline 函数名称和绑定。请参阅 Bash 内建命令。
bash
$ bind -p|grep -v ^#|more
"\C-g": abort
"\C-x\C-g": abort
"\e\C-g": abort
"\C-j": accept-line
"\C-m": accept-line
"\C-b": backward-char
"\eOD": backward-char
"\e[D": backward-char
"\C-h": backward-delete-char
"\C-?": backward-delete-char
"\C-x\C-?": backward-kill-line
"\e\C-h": backward-kill-word
"\e\C-?": backward-kill-word
"\eOd": backward-word
...
keyname: function-name or macro
keyname 是用英语拼写的键的名称。例如:
bash
Control-u: universal-argument
Meta-Rubout: backward-kill-word
Control-o: "> output"
在上面的例子中,C-u 被绑定到函数 universal-argument,M-DEL 被绑定到函数 backward-kill-word,而 C-o 被绑定到运行右侧表达的宏(也就是在行中插入文本 '> output')。
这种按键绑定语法识别许多符号字符名称:DEL、ESC、ESCAPE、LFD、NEWLINE、RET、RETURN、RUBOUT(破坏性退格)、SPACE、SPC 和 TAB。
"keyseq": function-name or macro
keyseq 与上面的 keyname 不同之处在于,可以指定表示整个按键序列的字符串,只需将按键序列放在双引号中。一些 GNU Emacs 风格的按键转义可以使用,如下面的示例所示,但不识别任何特殊字符名称。
bash
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file
"\e[11~": "Function Key 1"
在上面的例子中,C-u 再次绑定到函数 universal-argument(就像第一个例子中一样),'C-x C-r' 绑定到函数 re-read-init-file,而 'ESC [ 1 1 ~' 绑定到插入文本 'Function Key 1'。
在指定按键序列时,可以使用以下 GNU Emacs 风格的转义序列:
\C-
控制前缀。
\M-
添加元前缀或将后一个字符转换为元字符,如上文强制元前缀所述(参见Readline Init文件语法中的变量设置)。
\e
Escape字符。
\\
反斜杠。
\"
双引号。
\'
单引号或撇号。
除了 GNU Emacs 风格的转义序列之外,还提供了第二组反斜杠转义序列:
\a
警报(铃声)
\b
退格
\d
删除
\f
换页
\n
新行
\r
回车
\t
水平制表符
\v
垂直制表符
\nnn
值为八进制值nnn(一到三位数字)的八位字符。
\xHH
八位字符,其值为十六进制值HH(一或两个十六进制数字)。
在输入宏的文本时,必须使用单引号或双引号来表示宏定义。未加引号的文本将被视为函数名。宏体中会展开上述描述的反斜杠转义。反斜杠会引用宏文本中的任何其他字符,包括"""和"'"。例如,以下绑定将使按下"C-x "在行中插入一个单独的"\":
bash
"\C-x\\": "\\"
8.3.2 Conditional Init Constructs
Readline 实现了一种类似于 C 预处理器条件编译功能的机制,允许根据测试结果执行按键绑定和变量设置。共有四种可用的解析器指令。
$if
$if 构造允许根据编辑模式、所使用的终端或使用 Readline 的应用程序进行绑定。测试的文本在任何比较运算符之后一直延伸到行末;除非另有说明,否则不需要任何字符来隔开它。
-
mode
$if 指令的 mode= 形式用于测试 Readline 是处于 emacs 模式还是 vi 模式。比如,这可以与 'set keymap' 命令结合使用,仅在 Readline 初始为 emacs 模式时,在 emacs-standard 和 emacs-ctlx 键映射中设置绑定。 -
term
术语=形式 可以用于包括终端特定的按键绑定,可能用于绑定终端功能键输出的按键序列。'=' 右侧的词会同时与终端的完整名称以及终端名称中第一个'-'之前的部分进行匹配。例如,这允许 xterm 同时匹配 xterm 和 xterm-256color。 -
version
版本测试可用于对特定的 Readline 版本进行比较。version 会扩展为当前的 Readline 版本。比较运算符集合包括'='(和'==')、'!='、'<='、'>='、'<'和'>'。运算符右侧提供的版本号由主版本号、可选的小数点和可选的次版本号组成(例如,'7.1')。如果省略次版本号,则默认为'0'。运算符可以通过空格与字符串版本以及版本号参数分隔开。以下示例将在使用的 Readline 版本为 7.0 或更高时设置变量:bash$if version >= 7.0 set show-mode-in-prompt on $endif
$else
如果测试失败,将执行 $if 指令此分支中的命令。
$endif
如前例所示,该命令用于终止一个 $if 命令。
$include
该指令以单个文件名作为参数,并从该文件中读取命令和键绑定。例如,以下指令从 /etc/inputrc 读取内容:
bash
$include /etc/inputrc
8.3.3 Sample Init File
这是一个 inputrc 文件的示例。它说明了键绑定、变量赋值和条件语法。
bash
# This file controls the behavior of line input editing for
# programs that use the GNU Readline library. Existing
# programs include FTP, Bash, and GDB.
#
# You can re-read the inputrc file with C-x C-r.
# Lines beginning with '#' are comments.
#
# First, include any system-wide bindings and variable
# assignments from /etc/Inputrc
$include /etc/Inputrc
#
# Set various bindings for emacs mode.
set editing-mode emacs
$if mode=emacs
Meta-Control-h: backward-kill-word Text after the function name is ignored
#
# Arrow keys in keypad mode
#
#"\M-OD": backward-char
#"\M-OC": forward-char
#"\M-OA": previous-history
#"\M-OB": next-history
#
# Arrow keys in ANSI mode
#
"\M-[D": backward-char
"\M-[C": forward-char
"\M-[A": previous-history
"\M-[B": next-history
#
# Arrow keys in 8 bit keypad mode
#
#"\M-\C-OD": backward-char
#"\M-\C-OC": forward-char
#"\M-\C-OA": previous-history
#"\M-\C-OB": next-history
#
# Arrow keys in 8 bit ANSI mode
#
#"\M-\C-[D": backward-char
#"\M-\C-[C": forward-char
#"\M-\C-[A": previous-history
#"\M-\C-[B": next-history
C-q: quoted-insert
$endif
# An old-style binding. This happens to be the default.
TAB: complete
# Macros that are convenient for shell interaction
$if Bash
# edit the path
"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f"
# prepare to type a quoted word --
# insert open and close double quotes
# and move to just after the open quote
"\C-x\"": "\"\"\C-b"
# insert a backslash (testing backslash escapes
# in sequences and macros)
"\C-x\\": "\\"
# Quote the current or previous word
"\C-xq": "\eb\"\ef\""
# Add a binding to refresh the line, which is unbound
"\C-xr": redraw-current-line
# Edit variable on current line.
"\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y="
$endif
# use a visible bell if one is available
set bell-style visible
# don't strip characters to 7 bits when reading
set input-meta on
# allow iso-latin1 characters to be inserted rather
# than converted to prefix-meta sequences
set convert-meta off
# display characters with the eighth bit set directly
# rather than as meta-prefixed characters
set output-meta on
# if there are 150 or more possible completions for a word,
# ask whether or not the user wants to see all of them
set completion-query-items 150
# For FTP
$if Ftp
"\C-xg": "get \M-?"
"\C-xt": "put \M-?"
"\M-.": yank-last-arg
$endif