5

I'm wondering if it's possible to write a zsh script that will write a command to the prompt but NOT execute it, i.e. leave it there for me to edit and then execute when I'm ready. I can do something like this with keybindings by leaving off the final '\C-m'. eg:

bindkey -s "\e[1;3C" "howdy!"

... I press Alt+RightArrow and the text "howdy!" is printed at the prompt and just left there.

I can also do something like what I want by writing my command to the history file and then recalling it with the up arrow. I've tried 'echo -n sometext' but it doesn't work.

Can I write a script that would exit leaving (say) " howdy! " on the command line? In actual fact I want the script to build up a complex command based on several things, but I want the script to leave it on the CLI for final editing, so automatic execution must be prevented.

Thanks in advance.

4

2 回答 2

5

原来答案很简单:

print -z $string-to-print
于 2012-04-15T04:53:40.370 回答
2

如果您指的是 zsh 函数而不是外部脚本,则可以编写一个 zle(zsh line editor的缩写)小部件并将其绑定到某个键。

# define to function to use
hello () {
 BUFFER=hello
 zle end-of-line
}
# create a zle widget, which will invoke the function.
zle -N hello
# bindkey Alt-a to that widget
bindkey "\ea" hello

您可以从Z-Shell 用户指南第 4 章了解更多信息。

于 2012-04-13T11:12:44.933 回答