简体   繁体   English

如何从 bash 自动完成中删除单个命令

[英]How to remove a single command from bash autocomplete

How do I remove a single "command" from Bash's auto complete command suggestions?如何从 Bash 的自动完成命令建议中删除单个“命令”? 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 "我问的是第一个参数,命令,自动完成,而不是问“ 如何为特定命令的参数禁用 bash 自动完成

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.例如,如果我有命令ls并且系统路径也找到ls_not_the_one_I_want_ever ,并且我键入ls然后按 Tab,我想要一种方法从每个可行选项中删除ls_not_the_one_I_want_ever

I think this might be related to the compgen -c list, as this seems to be the list of commands available.我认为这可能与compgen -c列表有关,因为这似乎是可用命令的列表。


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.背景:Windows 上的 WSL 将所有.dll文件放在我的路径上,除了应该在那里的.exe文件,所以我想在我的 bash 环境中删除许多 dll,但我不确定如何继续。

Bash 5.0's complete command added a new -I option for this. Bash 5.0 的complete命令为此添加了一个新的-I选项。

According to man bash根据man bash

  • complete -pr [-DEI] [ name ...]完成 -pr [-DEI] [名称...]

    [...] 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 ; [...] -I选项表示其他提供的选项和操作应适用于在该行的初始非赋值字上或在命令分隔符之后完成,例如; 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

Using @pynexj's answer, I came up with the following example that seems to work well enough:使用@pynexj 的答案,我想出了以下示例,该示例似乎运行良好:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM