I have a bash
terminal window where I execute exclusively one command foo
. Because of that I'd like every line (after the prompt of course) to begin with „foo ” so that I just have to type the functions' options and arguments, but not the recurrent function name. Of course it would be nice to be able to alter the automatically inserted string too, but that's not essential for me.
Example
When I open the terminal, without typing anything what I want to see is:
user@host:~/ $ foo
Then I type --option argument
and when I press Enter the function foo
is called with the given --option
and argument
.
What I tried
I tried to fiddle around with $PS1
and $PROMPT_COMMAND
using xdotool type "foo "
and astonishingly that actually works, but unfortunately it also prints “foo ” before the prompt, which is quite ugly:
user@host:~/ $ PROMPT_COMMAND='xdotool type "foo "'
foo user@host:~/ $ foo
I also found and tried the preexec
function from Ryan Caloras' bash-preexec script, but it has exactly the same problem.
How to echo (a (executable-)string) to the prompt, so that the cursor flashes at the end of the line? is related, but the answers there don't make it possible to add something (--option argument
) to the command to be executed. I didn't test zsh
though – there ought to be a bash
solution for such a simple thing, don't you think?
Best Answer
With
zsh
, it should just be a matter of:Or to avoid it overriding commands queued by the user with Alt+q:
With
bash
, instead ofxdotool
, you could use theTIOCSTI
ioctl
:It's preferable to
xdotool
because it's inserting those characters directly in the input buffer of the devicebash
is reading from.xdotool
would only work if there's a X server running (wouldn't work on the console or real terminals or overssh
(without-X
) for instance), that it is the one identified by$DISPLAY
and that's the one you're interacting with, and that the terminal emulatorbash
is running in has the focus when$PROMPT_COMMAND
is evaluated.Now, like in your
xdotool
case, because theioctl()
is done before the prompt is displayed and the tty terminal line discipline is put out oficanon+echo
mode by readline, you're likely to see the echo of thatfoo
by the tty line discipline messing up the display.You could work around that by instead inserting a string whose echo is invisible (like U+200B if using exclusively Unicode locales) and bind that to an action that inserts
"foo "
:Or you could delay the
TIOCSTI
ioctl enough for readline to have time to initialise:If, like in the
zsh
approaches, you want to handle the case where the user enters text before the prompt is displayed, you could eithertcflush(0, TCIFLUSH)
inperl
before theTIOCSTI
ioctl (also needs a-MPOSIX
option), orlike in the
zsh
approach, ensure thefoo
is inserted at the start of the buffer by inserting a^A
(assuming you use theemacs
(default) editing mode where that moves the cursor to the beginning of the line) beforefoo
and^E
after (to move to the end):or