#Bash 的 bind 命令
bind [OPTION]... [KEY:ACT]
功能
设置 Readline 快捷键绑定和变量。
快捷键可能被终端模拟器处理,而不发送到 Readline,使得此命令不起作用。
类型
Bash 内置命令。
参数
OPTION- 选项:-m keymap- 在本命令执行期间,使用KEYMAP作为键盘映射;可接受的键盘映射名称包括emacs、emacs-standard、emacs-meta、emacs-ctlx、vi、vi-move、vi-command和vi-insert-l- 列出函数名-P- 列出函数名称和绑定-p- 以命令行的格式列出函数和绑定-S- 列出调用宏的键序列及其值-s- 以命令行的格式列出调用宏的键序列及其值-V- 列出变量名称和值-v- 以命令行的格式列出变量名称和值-q func- 查询绑定func的快捷键-u func- 取消所有绑定func的快捷键-r keyseq- 取消所有keyseq快捷键绑定的操作-f filename- 从filename文件读取绑定-x keyseq:command- 绑定快捷键keyseq和命令command-X- 以命令行的格式列出-x绑定的快捷键
KEY- 快捷键ACT- 快捷键触发的动作;可以是命令或函数
#示例
查看绑定
$ bind -p
"\C-g": abort
"\C-x\C-g": abort
"\e\C-g": abort
"\C-j": accept-line
"\C-m": accept-line
...
查询绑定
$ bind -q yank # 查看哪个快捷键是“粘贴”:Ctrl + L
yank can be invoked via "\C-y".
$ bind -q kill-line # 查看哪个快捷键是“剪切到行末”:Ctrl + K
kill-line can be invoked via "\C-k".
$ bind -q backward-kill-line # 查看哪个快捷键是“剪切到行首”:Ctrl + X + ESC
backward-kill-line can be invoked via "\C-x\C-?".
设置绑定
$ bind '\C-x:backward-kill-line' # 将“剪切到行首”绑定到 Ctrl + X
#手册
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
Set Readline key bindings and variables.
Bind a key sequence to a Readline function or a macro, or set a
Readline variable. The non-option argument syntax is equivalent to
that found in ~/.inputrc, but must be passed as a single argument:
e.g., bind '"\C-x\C-r": re-read-init-file'.
Options:
-m keymap Use KEYMAP as the keymap for the duration of this
command. Acceptable keymap names are emacs,
emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
vi-command, and vi-insert.
-l List names of functions.
-P List function names and bindings.
-p List functions and bindings in a form that can be
reused as input.
-S List key sequences that invoke macros and their values
-s List key sequences that invoke macros and their values
in a form that can be reused as input.
-V List variable names and values
-v List variable names and values in a form that can
be reused as input.
-q function-name Query about which keys invoke the named function.
-u function-name Unbind all keys which are bound to the named function.
-r keyseq Remove the binding for KEYSEQ.
-f filename Read key bindings from FILENAME.
-x keyseq:shell-command Cause SHELL-COMMAND to be executed when
KEYSEQ is entered.
-X List key sequences bound with -x and associated commands
in a form that can be reused as input.
Exit Status:
bind returns 0 unless an unrecognized option is given or an error occurs.