Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh create a command line within a script but do not execute it

Tags:

zsh

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.

like image 564
Ray Andrews Avatar asked Apr 12 '12 01:04

Ray Andrews


2 Answers

Turns out the answer is simple:

print -z $string-to-print
like image 54
Ray Andrews Avatar answered Sep 30 '22 13:09

Ray Andrews


If you mean a zsh function and not an external script, you can write a zle (short for zsh line editor) widget and bind it to some key.

# 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

You can learn more from A User's Guide to the Z-Shell, Chapter 4.

like image 30
lilydjwg Avatar answered Sep 30 '22 14:09

lilydjwg