With $
as my bash prompt and ⏎
symbolizing me hitting the enter key in the following example, how could I construct a command/alias foo
so that
$ foo bar⏎
would enter/input/pre-fill/type "bar" (or any other string I pass to the command) to the command line, so that I can modify "bar" before hitting Enter? E.g.
$ bar
…
$ barbaz⏎
I've unsuccessfully tried echo bar > /dev/pts/123
and would like to do without xdotool
. Is this possible?
EDIT: Example use case, a "greppy autocomplete":
I often need long commands with many arguments that are hard to remember. I keep examples in a file:
commands.txt:
ffmpeg -i infile.mp4 -c:v copy -c:a copy -y transcoded.mp4
sox in.wav out.wav remix 1 0
now, if I had the command as specified above, let's call it inject
, I could have an alias
grepcomplete () {
inject $(grep $0 commands.txt)
}
so when I remember that I need to remix something, but I don't remember sox and its arguments, I can type grepcomplete remix
, and then have sox in.wav out.wav remix 1 0
sit on my command line, as if I typed it out, ready for me to edit and adapt, before I execute it by hitting enter.
Without the need to select, copy, paste anything.
As Kamil suggests in the comments, I could use bash's history search (Ctrl-R), and provide my own "history" by doing something like history -r commands.txt
in my bashrc.
Still, my approach has the benefit that I can easily hack it, e.g. by displaying all matches with syntax highlighting.
Please note that I've answered this question myself, where I provide an implementation of this inject
command.
foo bar
only to getbar
in the command line clearly takes more effort than simply typingbar
in the first place. Therefore I guess you want to use thisfoo
in some other way. How exactly? Your question is remotely similar to this one, where the gain in effort is indisputable. – Kamil Maciorowski Jan 11 '23 at 08:29snippet remix
will entersox in.wav out.wav remix 1 0 # upmix mono to stereo
into the prompt for me to edit and execute, without the need to select, copy, paste.Still I find this sub-problem worthwhile exploring without the context. – kubi Jan 11 '23 at 08:37
history -r
, if I do that in bashrc, hmm... thanks! – kubi Jan 11 '23 at 09:16