use*_*381 3 bash zsh configuration
我想使用键序列创建一个键绑定,以重新加载存储在 中C-x r
的配置以及存储在 中的 readline 库之一。bash
~/.bashrc
~/.inputrc
要重新加载 readline 的配置,我想我可以使用re-read-init-file
以下中描述的函数man 3 readline
:
re-read-init-file (C-x C-r)
Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there.
Run Code Online (Sandbox Code Playgroud)
要重新加载 的配置bash
,我可以使用source
or.
命令。但是,我不确定将 shell 命令与 readline 函数结合起来的最佳方法是什么。因此,我想出了 2 个键绑定的组合:
bind '"\C-xr": ". ~/.bashrc \C-x\C-z1\C-m"'
bind '"\C-x\C-z1": re-read-init-file'
Run Code Online (Sandbox Code Playgroud)
当我点击C-x r
bash 时,会发生以下情况:
. ~/.bashrc `~/.bashrc` is inserted on the command line
C-x C-z 1 `C-x C-z 1` is typed which is bound to `re-read-init-file`
C-m `C-m` is hit which executes the current command line
Run Code Online (Sandbox Code Playgroud)
它似乎有效,因为在 tmux 内部,如果我有 2 个窗格,一个用于编辑~/.inputrc
或~/.bashrc
,另一个用于 shell,并且我更改配置文件,在敲入C-x r
shell 后,我可以看到更改生效(无论是新别名或新键绑定),无需关闭窗格即可重新打开新 shell。
但是,有没有更好的方法来达到相同的结果呢?特别是,是否可以执行命令而不在历史记录中留下条目?因为如果我点击C-p
回忆最后执行的命令,我会得到. ~/.bashrc
,而我更愿意直接获取在重新获取 shell 配置之前执行的命令。
我有同样的问题zsh
:
bindkey -s '^Xr' '. ~/.zshrc^M'
Run Code Online (Sandbox Code Playgroud)
同样,点击 后C-x r
,该命令. ~/.zshrc
将记录在历史记录中。有没有更好的方法来重新配置 的源zsh
?
不要将命令插入命令行来运行它!这是非常脆弱的\xe2\x80\x94\xc2\xa0你正在尝试的假设在当前提示符下还没有输入任何内容。相反,请将密钥绑定到 shell 命令,而不是将其绑定到行编辑命令。
\n\n在 bash 中,使用bind -x
.
bind -x \'"\\C-xr": . ~/.bashrc\'\n
Run Code Online (Sandbox Code Playgroud)\n\n如果您还想重新读取 readline 配置,则没有一种简单的方法可以在键绑定中混合 readline 命令和 bash 命令。一种笨拙的方法是将键绑定到包含两个键序列的 readline 宏,一个绑定到要执行的 readline 命令,一个绑定到 bash 命令。
\n\nbind \'"\\e[99i~": re-read-init-file\'\nbind -x \'"\\e[99b~": . ~/.bashrc\'\nbind \'"\\C-xr": "\\e[99i~\\e[99b~"\'\n
Run Code Online (Sandbox Code Playgroud)\n\n在 zsh 中,使用zle -N
将函数声明为小部件,然后bindkey
将该小部件绑定到键。
reread_zshrc () {\n . ~/.zshrc\n}\nzle -N reread_zshrc\nbindkey \'^Xr\' reread_zshrc\n
Run Code Online (Sandbox Code Playgroud)\n