'How to remove a single command from bash autocomplete
How do I remove a single "command" from Bash's auto complete command suggestions? I'm asking about the very first argument, the command, in auto complete, not asking "How to disable bash autocomplete for the arguments of a specific command"
For example, if I have the command ls
and the system path also finds ls_not_the_one_I_want_ever
, and I type ls
and then press tab, I want a way to have removed ls_not_the_one_I_want_ever
from every being a viable option.
I think this might be related to the compgen -c
list, as this seems to be the list of commands available.
Background: WSL on Windows is putting all the .dll
files on my path, in addition to the .exe
files that should be there, and so I have many dlls I would like to remove in my bash environment, but I'm unsure how to proceed.
Solution 1:[1]
Bash 5.0's complete
command added a new -I
option for this.
According to man bash
—
complete -pr [-DEI] [name ...]
[...] The
-I
option indicates that other supplied options and actions should apply to completion on the initial non-assignment word on the line, or after a command delimiter such as;
or|
, which is usually command name completion. [...]
Example:
function _comp_commands()
{
local cur=$2
if [[ $cur == ls* ]]; then
COMPREPLY=( $(compgen -c "$cur" | grep -v ls_not_wanted) )
fi
}
complete -o bashdefault -I -F _comp_commands
Solution 2:[2]
Using @pynexj's answer, I came up with the following example that seems to work well enough:
if [ "${BASH_VERSINFO[0]}" -ge "5" ]; then
function _custom_initial_word_complete()
{
if [ "${2-}" != "" ]; then
if [ "${2::2}" == "ls" ]; then
COMPREPLY=($(compgen -c "${2}" | \grep -v ls_not_the_one_I_want_ever))
else
COMPREPLY=($(compgen -c "${2}"))
fi
fi
}
complete -I -F _custom_initial_word_complete
fi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Andy |