I have a little helper command in ~/.zshrc
called stfu
.
stfu() {
if [ -z "$1" ]; then
echo "Usage: stfu <program> [arguments...]"
return 1
fi
nohup "$@" &>/dev/null &
disown
}
complete -W "$(ls /usr/bin)" stfu
stfu
will run some other command but also detach it from the terminal and make any output shut up. I use it for things such as starting a browser from the terminal without worrying about CTRL+Z
, bg
, and disown
.
$ stfu firefox -safe-mode
# Will not output stuff to the terminal, and
# I can close the terminal too.
Here’s my issue:
On the second argument and above, when I hit tab, how do I let autocomplete suggest me the arguments and command line switches for the command I’m passing in?
e.g. stfu ls -<tab>
should show me whatever ls’s completion function is, rather than listing every /usr/bin
command again.
# Intended completion
$ stfu cat -<TAB>
-e -- equivalent to -vE
--help -- display help and exit
--number -n -- number all output lines
--number-nonblank -b -- number nonempty output lines, overrides -n
--show-all -A -- equivalent to -vET
--show-ends -E -- display $ at end of each line
--show-nonprinting -v -- use ^ and M- notation, except for LFD and TAB
--show-tabs -T -- display TAB characters as ^I
--squeeze-blank -s -- suppress repeated empty output lines
-t -- equivalent to -vT
-u -- ignored
# Actual completion
$ stfu cat <tab>
...a list of all /usr/bin commands
$ stfu cat -<tab>
...nothing, since no /usr/bin commands start with -
Is GNU readline what you’re looking for?
No idea, but I bet you could get some ideas by figuring out how
sudo
andtime
do it.All I can tell you is that this is done differently for each shell. So decide whether you want completions for bash, zsh, fish, all of the above, or whatever, and look at the docs for the relevant shells.
For people using bash that are thinking “how do I do that”:
The
bash-complete
package adds the_command
function for recursive completion on commands that accept other commands with their own arguments. It’s what sudo uses last I checked. You can addcomplete -F _command stfu
to your bashrc to link it to the stfu command.https://man.archlinux.org/man/bash.1#Programmable_Completion
Glad you have it working. This may also work:
_stfu () { shift words (( CURRENT-=1 )) _normal -P } compdef _stfu stfu
Stunningly simple, solely a shift. I love MVPs.