I know it's been more than 9 years, but for anyone who stumbled upon this question while searching:
Yes you can, the way depends on your shell (so check its manual first), i.g. ZSH supports it natively with print -z [args]
pushing the arguments onto the editing buffer stack, separated by spaces (check the docs). While other shells don't, i.g. BASH. So you need to hack your way. I'd assume you're using BASH.
You need to use read
with readline(3)
, $PS1
as the prompt (not necessary, just aesthetics) and place your desired command in the editing buffer, then execute it. Thus:
read -e -p "${PS1@P}" -i "[COMMAND]" cmd; bash -c "$cmd"
To understand the command refer to read
's section in the BASH manual.
There's multiples ways of executing this idea, and they may differentiate based on the shell used, so don't take anything literally, and prioritize your shell's documentation above all.
PS: For better experience, you can enable vi mode in readline
by appending set editing-mode vi
in ~/.inputrc
, and more.