首页 > 解决方案 > 如何不使用 bash 中的波浪号和制表符完成来解决所有用户

问题描述

要进入我的主文件夹的某个子文件夹,我键入cd ~/sub[tab]. 这样就完成了cd ~/subfolder。一切安好。

如果我不小心忘记了斜线并键入cd ~sub[tab]所有用户,则将搜索匹配项。这需要一些时间,因为在我们公司,我们有一个中央用户服务器和很多用户。

我可以关闭所有用户的解析,但仍然使用波浪号~吗?

我使用 bash 版本 4.3.0。

我永远不需要通过cd ~someUser.

标签: bashautocomplete

解决方案


打开文件/usr/share/bash-completion/bash_completion并找到函数tilde()(在第 941 行附近)。在这里,只需注释掉行号946。最后,该函数应如下所示:

# Perform tilde (~) completion
# @return  True (0) if completion needs further processing,
#          False (> 0) if tilde is followed by a valid username, completions
#          are put in COMPREPLY and no further processing is necessary.
_tilde()
{
    local result=0
    if [[ $1 == \~* && $1 != */* ]]; then
        # Try generate ~username completions
        #COMPREPLY=( $( compgen -P '~' -u "${1#\~}" ) )
        result=${#COMPREPLY[@]}
        # 2>/dev/null for direct invocation, e.g. in the _tilde unit test
        [[ $result -gt 0 ]] && compopt -o filenames 2>/dev/null
    fi
    return $result
}

试试看。


推荐阅读