编辑一个命令,然后在按下 zsh 中的键绑定后执行。

huangapple go评论61阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2023年4月4日 04:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定