代码之家  ›  专栏  ›  技术社区  ›  schorsch312

如何在bash中不使用tilde和tab completion来解析所有用户

  •  0
  • schorsch312  · 技术社区  · 7 年前

    进入我键入的主文件夹的子文件夹 cd ~/sub[tab] . 此操作完成于 cd ~/subfolder . 一切都很好。

    如果我不小心忘了斜杠和打字 cd ~sub[tab] 正在解析所有用户并搜索匹配项。这需要一些时间,因为我们公司有一个中心用户服务器和许多用户。

    我可以关闭所有用户的解析,但仍使用平铺吗 ~

    我使用bash版本4.3.0。

    cd ~someUser .

    1 回复  |  直到 7 年前
        1
  •  1
  •   Samit    7 年前

    打开文件 /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
    }
    

    试试看。