我正在使用bash
版本4.3.48
:
$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
我正在尝试使用键序列定义键绑定,C-x r
以重新加载 的配置bash + readline
。感谢@Gilles,我将此代码包含在我的~/.bashrc
:
bind '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": . ~/.bashrc'
bind '"\C-xr": "\e[99i~\e[99b~"'
它有效,除了bash
当我点击 时会写入一条错误消息C-x r
,我不明白:
\udccf\udccf\udccf\udccf\udccf\udccf: command not found
我认为这可能是由于与我更改的其他一些设置的交互所致,因此我删除了 中的所有内容~/.bashrc
,除了定义键绑定的 3 行C-x r
。
但仍然有错误信息:
: command not found
因此,我尝试了一种更简单的方法bashrc
,只有一个键绑定:
bind -x '"\C-xr": . ~/.bashrc'
这次,点击C-x r
会产生错误:
\udccf
: command not found
bind -x '"\C-xr": . ~/.bashrc'
但是,如果我注释文件中的行bashrc
,并在交互式 shell 中执行该命令bash
,则一切正常。没有错误消息,并且配置已正确重新配置。
所以,我想我应该在这个键绑定周围写一个保护,以防止bash
重新定义它(如果已经完成了)。但我不知道如何测试键绑定是否存在。此外,我想更好地了解这些错误会发生什么。
我不知道这是否重要,但我从不bash
直接开始,我总是从 if 开始zsh
。当我想测试要在 bash 脚本中编写的某些命令时,我打开一个终端(用作默认 shell),然后从那里zsh
执行。bash
那么,也许继承了一些导致第一条错误消息bash
的环境变量?zsh
另外,正如评论部分所询问的,这里是hexdump -C ~/.bashrc
仅使用 3 个键绑定的输出:
00000000 62 69 6e 64 20 20 20 20 27 22 5c 65 5b 39 39 69 |bind '"\e[99i|
00000010 7e 22 3a 20 72 65 2d 72 65 61 64 2d 69 6e 69 74 |~": re-read-init|
00000020 2d 66 69 6c 65 27 0a 62 69 6e 64 20 2d 78 20 27 |-file'.bind -x '|
00000030 22 5c 65 5b 39 39 62 7e 22 3a 20 2e 20 7e 2f 2e |"\e[99b~": . ~/.|
00000040 62 61 73 68 72 63 27 0a 62 69 6e 64 20 20 20 20 |bashrc'.bind |
00000050 27 22 5c 43 2d 78 72 22 3a 20 20 20 22 5c 65 5b |'"\C-xr": "\e[|
00000060 39 39 69 7e 5c 65 5b 39 39 62 7e 22 27 0a |99i~\e[99b~"'.|
0000006e
bashrc
这是仅包含 的十六进制转储bind -x '"\C-xr": . ~/.bashrc'
:
00000000 62 69 6e 64 20 2d 78 20 27 22 5c 43 2d 78 72 22 |bind -x '"\C-xr"|
00000010 3a 20 2e 20 7e 2f 2e 62 61 73 68 72 63 27 0a |: . ~/.bashrc'.|
0000001f
这是我的完整内容bashrc
:
case "$-" in
*i*) ;;
*) return ;;
esac
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
alias cdr='cd "$(ls -dt */ | head -1)"'
export HISTCONTROL="ignoreboth:erasedups"
export HISTIGNORE="clear:history:"
export HISTFILE="${HOME}/.bash_eternal_history"
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
cd() {
if [ $# -eq 2 ]; then
builtin cd "${PWD/$1/$2}"
elif [ $# -eq 1 ]; then
builtin cd "$1"
else
builtin cd
fi
}
shopt -s autocd
shopt -s checkwinsize
shopt -s globstar
shopt -s histappend
shopt -s histreedit
shopt -s histverify
. /usr/share/bash-completion/bash_completion
. ~/.fzf.bash
. ~/.shrc
bind '" ": magic-space'
bind '"\C-xc": "\C-a\C-kvimdiff <() <()\C-b\C-b\C-b\C-b\C-b"'
bind '"\C-xf": character-search'
bind '"\C-xF": character-search-backward'
bind '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": . ~/.bashrc'
bind '"\C-xr": "\e[99i~\e[99b~"'
bind '"\C-x\C-s": "sudo !!\C-m\C-m"'
bind -x '"\C-x\C-t": fzf-file-widget'
bind '"\C-t": transpose-chars'
bind '"\C-x\C-v": "\C-e | vim -R -\C-m"'
bind '"\em": "\C-aman \ef\C-k\C-m"'
bind '"\e\C-b": "\C-e >/dev/null 2>&1 &\C-m"'
编辑:
我尝试将命令包装. ~/.bashrc
在函数内,并在键绑定中调用后者。使用以下语法:
bind '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": MyFunc'
bind '"\C-xr": "\e[99i~\e[99b~"'
MyFunc () {
. ~/.bashrc
}
我发现该错误受到函数名称长度的影响。这是一个表,其中左列包含函数的不同名称,右列包含错误消息(如果有):
┌───────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────┐
│ a │ \udc99: command not found │
│ ab │ \udc92: command not found │
│ abc │ : command not found │
│ abcd │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcde │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdef │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefg │ no error │
│ abcdefgh │ : command not found │
│ abcdefghi │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghij │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijk │ \udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijkl │ \udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijklm │ \udcdf\udcdf\udcdf: command not found │
│ abcdefghijklmn │ \udcdf\udcdf: command not found │
│ abcdefghijklmno │ \udcdf: command not found │
│ abcdefghijklmnop │ : command not found │
│ abcdefghijklmnopq │ no error │
└───────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────┘
我还尝试了一个不同的功能,将命令的输入重新连接. ~/.bashrc
到终端(灵感来自这里):
bind '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": MyFunc'
bind '"\C-xr": "\e[99i~\e[99b~"'
MyFunc () {
. ~/.bashrc </dev/tty
}
结果如下:
┌──────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ a │ 9}: command not found │
│ ab │ \udcdd: command not found │
│ abc │ : command not found │
│ abcd │ no error │
│ abcde │ no error │
│ abcdef │ no error │
│ abcdefg │ no error │
│ abcdefgh │ : command not found │
│ abcdefghi │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghij │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijk │ \udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijkl │ \udcdf\udcdf\udcdf\udcdf: command not found │
│ abcdefghijklm │ \udcdf\udcdf\udcdf: command not found │
│ abcdefghijklmn │ \udcdf\udcdf: command not found │
│ abcdefghijklmno │ \udcdf: command not found │
│ abcdefghijklmnop │ : command not found │
│ abcdefghijklmnopq │ no error │
│ abcdefghijklmnopqr │ no error │
│ abcdefghijklmnopqrs │ no error │
│ abcdefghijklmnopqrst │ \udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf$: command not found │
└──────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘