Bash下,如果使用的是shell脚本文件,快捷键不一定是一样的,有的时候会出现同一个快捷键有不同的表现,这种情况一般是由于Bash所处的模式不同而引起的,你可以通过set命令来调整模式:
set -o emacs ##切到emacs模式 set -o vi ##切到vi模式 set -o ## 查看当前选项的设置状态
不是自己个人使用的电脑建议用创建文件存储bind命令,每次登陆执行一遍:
因为bind命令,只是针对当前的会话有效,一旦会话丢失,这样设置的快捷键就会丢失,为了能够让设置的快捷键永久有效,我们就需要将快捷键的配置写入文件Inputrc。
注意:用bind时,:后不能有空格
bind:Bash - bind builtin function (Key to function, Shortcut) | Edition | Datacadamia - Data and Co
如下命令存入bind.sh ,然后命令行输入sh bind.sh
set -o emacs #不加会报错bind: warning: line editing not enabled
bind "\C-p":history-search-backward
bind "\C-n":history-search-forward
bind "\C-[D":forward-word
bind "\C-[C":backward-word
bind -P |grep history查看变化
注意:我的系统如果执行:
bind "\e[A":history-search-backward
bind "\e[B":history-search-forward
会出bug,会出现如下玩意儿,原因未知。但这两句放到~/.inputrc中式没问题的。
history-search-backward can be found on "\C-p", "\e[5~", "\\".
Inputrc 用来处理键盘映射表。这个文件在启动的时候被一个bash和其他shell使用的库,readline,调用。
全局的设定值包含在 /etc/inputrc中。 每个用户自己的值设定于~/.inputrc。 ~/.inputrc 若存在,则会忽略全局设定值(~/.bashrc确定?)。前面bash设定中缺省调用 /etc/inputrc 如果你想让系统同时使用两个设定,可以把一个 .inputrc放到/etc/skel 中给每一个新建立的用户使用。
## 本例来自CentOS6.4的默认配置文件
$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~": quoted-insert
"/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
说明:
在配置文件中,/C代表CTRL,/M代表ALT,/e代表ESC,//是反斜杠/,/'是单引号,/"是双引号;
/C- control prefix
/M- meta prefix
/e an escape character
// backslash
/" literal ", a double quote
/’ literal ’, a single quote
如果要查看某一个功能键的字符序列可以通过[CTRL-V]来实现,或者输入cat后回车,进入编辑中,直接按快捷键
但有些CTRL-V不起作用,则在命令行输入read命令,再按键,则会显示对应字符。ctrl+c退出
The \e is the escape character, displayed as ^[ in the shell output
ESC键\e的值为 ^[
配置文件中可能会使用八进制或者十六进制来表示字符。
(HowTo) Как настроить работу клавиш Shift+Стрелки в Midnight Commander в Konsole - Ubuntu.ru
bind -l 列出所有function
- abort
- accept-line
- alias-expand-line
- arrow-key-prefix
- backward-byte
- backward-char
- backward-delete-char
- backward-kill-line
- backward-kill-word
- backward-word
- beginning-of-history
- beginning-of-line
- call-last-kbd-macro
- capitalize-word
- character-search
- character-search-backward
- clear-screen
- complete
- complete-command
- complete-filename
- complete-hostname
- complete-into-braces
- complete-username
- complete-variable
- copy-backward-word
- copy-forward-word
- copy-region-as-kill
- dabbrev-expand
- delete-char
- delete-char-or-list
- delete-horizontal-space
- digit-argument
- display-shell-version
- do-lowercase-version
- downcase-word
- dump-functions
- dump-macros
- dump-variables
- dynamic-complete-history
- edit-and-execute-command
- emacs-editing-mode
- end-kbd-macro
- end-of-history
- end-of-line
- exchange-point-and-mark
- forward-backward-delete-char
- forward-byte
- forward-char
- forward-search-history
- forward-word
- glob-complete-word
- glob-expand-word
- glob-list-expansions
- history-and-alias-expand-line
- history-expand-line
- history-search-backward
- history-search-forward
- insert-comment
- insert-completions
- insert-last-argument
- kill-line
- kill-region
- kill-whole-line
- kill-word
- magic-space
- menu-complete
- menu-complete-backward
- next-history
- non-incremental-forward-search-history
- non-incremental-forward-search-history-again
- non-incremental-reverse-search-history
- non-incremental-reverse-search-history-again
- old-menu-complete
- operate-and-get-next
- overwrite-mode
- possible-command-completions
- possible-completions
- possible-filename-completions
- possible-hostname-completions
- possible-username-completions
- possible-variable-completions
- previous-history
- quoted-insert
- redraw-current-line
- re-read-init-file
- reverse-search-history
- revert-line
- self-insert
- set-mark
- shell-backward-kill-word
- shell-backward-word
- shell-expand-line
- shell-forward-word
- shell-kill-word
- skip-csi-sequence
- start-kbd-macro
- tab-insert
- tilde-expand
- transpose-chars
- transpose-words
- tty-status
- undo
- universal-argument
- unix-filename-rubout
- unix-line-discard
- unix-word-rubout
- upcase-word
- vi-append-eol
- vi-append-mode
- vi-arg-digit
- vi-back-to-indent
- vi-backward-bigword
- vi-backward-word
- vi-bword
- vi-bWord
- vi-change-case
- vi-change-char
- vi-change-to
- vi-char-search
- vi-column
- vi-complete
- vi-delete
- vi-delete-to
- vi-editing-mode
- vi-end-bigword
- vi-end-word
- vi-eof-maybe
- vi-eword
- vi-eWord
- vi-fetch-history
- vi-first-print
- vi-forward-bigword
- vi-forward-word
- vi-fword
- vi-fWord
- vi-goto-mark
- vi-insert-beg
- vi-insertion-mode
- vi-match
- vi-movement-mode
- vi-next-word
- vi-overstrike
- vi-overstrike-delete
- vi-prev-word
- vi-put
- vi-redo
- vi-replace
- vi-rubout
- vi-search
- vi-search-again
- vi-set-mark
- vi-subst
- vi-tilde-expand
- vi-yank-arg
- vi-yank-to
- yank
- yank-last-arg
- yank-nth-arg
- yank-pop
参数详解:
# emacs模式的一些默认/修改
$if mode=emacs
# 允许使用Home/End键
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# 允许使用Delete/Insert 键
"\e[3~": delete-char
"\e[2~": quoted-insert
# “page up”和“page down”的映射将逐步进入历史的开始/结束
"\e[5~": beginning-of-history
"\e[6~": end-of-history
# “page up”和“page down”搜索历史的替代映射
"\e[5~": history-search-backward
"\e[6~": history-search-forward
#ctrl -左箭头和ctrl -右箭头用于单词移动的映射
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
$if term=rxvt
"\e[7~": beginning-of-line
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word
$endif
# 对于非RH/Debian xterm,对RH/Debian xterm没有害处
# "\eOH": beginning-of-line
# "\eOF": end-of-line
# 支持freebsd控制台
# "\e[H": beginning-of-line
# "\e[F": end-of-line
$endif
set horizontal-scroll-mode Off # 确认我们不会在同一行中输出所有的东西。
set meta-flag On # 开启8比特输入
set input-meta On
set convert-meta Off # 禁止对第8个比特过滤
set output-meta On # 在显示时保存第8个比特
set bell-style none # 关闭可视和声音提示
# 函数第一部分中包括的换码符序列的值
"\eOd": backward-word
"\eOc": forward-word
# 在控制台使用
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": beginning-of-history
"\e[6~": end-of-history
"\e[3~": delete-char
"\e[2~": quoted-insert
# xterm使用
"\eOH": beginning-of-line
"\eOF": end-of-line
参考资料:
Bash - inputrc | Edition | Datacadamia - Data and Co
keyseq: Bash - Shortcut Key Character Sequence (keyseq) | Edition | Datacadamia - Data and Co
bind: bind Man Page - Linux - SS64.com
bind:Bash - bind builtin function (Key to function, Shortcut) | Edition | Datacadamia - Data and Co