英文:
Edit a command, and then execute upon pressing a keybinding in zsh
问题
我尝试在按键绑定"Control Space"时执行任意命令,使用zsh。
我有以下代码
function space-run() {
$BUFFER=" $BUFFER"
zle reset-prompt
zle accept-line
}
zle -N space-run
bindkey '^ ' space-run # bind to control space
换句话说,我想在命令之前添加一个空格,然后执行它,因为在我的zsh配置中,以空格开头的命令不会添加到历史记录中。
上述代码不起作用,如果我键入ls并按Control Space,它会一直说"command not found: ls=ls"。
我希望$BUFFER=" $BUFFER"会添加空格,但我不确定发生了什么,因为它显然没有这样做。
我也不确定zle reset-prompt 做什么,或者是否需要它。
我该如何修复我的代码以获得所需的行为?
英文:
I am trying to execute an arbitrary command upon the keybinding "Control Space" with zsh.
I have the following code
function space-run() {
$BUFFER=" $BUFFER"
zle reset-prompt
zle accept-line
}
zle -N space-run
bindkey '^ ' space-run # bind to control space
In other words, I want to add a space before a command, and then execute it, because in my zsh configuration, a command beginning with a space does not get added to history.
The above code does not work, and it keep saying "command not found: ls=ls" if I typed ls and pressed Control Space.
I expected $BUFFER=" $BUFFER" would add the space, but I'm not sure what happens, because it clearly doesn't do that.
I am also not sure what zle reset-prompt does, or if it's needed there.
How should I fix my code so that I get the desired behavior?
答案1
得分: 1
这没有意义。
$BUFFER=" $BUFFER"
假设BUFFER
的值为_ls_。该行将会被展开为
"ls= ls"
并被解释为一个要执行的命令。
在zsh中,变量名赋值的语法是
NAME=value
因此,在你的情况下
BUFFER=" $BUFFER"
英文:
This does not make sense.
$BUFFER=" $BUFFER"
Assume that BUFFER
has the value ls. The line would be expanded into
"ls= ls"
and this would be interpreted as a command to be executed.
Variable name assignment in zsh has the syntax
NAME=value
Therefore in your case
BUFFER=" $BUFFER"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论