我在Ubuntu16.04LTS上使用bash v4.3.46。
我将别名"p“设置为"python3”。通过在.bashrc中写这个
alias p='python3'问题:
当别名化时,完成不能正常工作。(它也显示了python的可执行文件)
$p <tab>
a.py a.txt b.py b.txt c.txt hoge/与普通命令相比:(这只显示可执行文件)
$python3 <tab>
a.py b.py hoge/我尝试了什么:
当我检查python3的完成时,
$complete -p python3
complete -F _python python3因此,我在.bashrc中添加了以下内容
complete -F _python p 现在,我得到:
$complete -p p
complete -F _python p这似乎是有效的,然而,我得到了同样的结果与开始。
我在_python上找到了/usr/share/bash-completion/completions/python3的定义
# bash completion for python -*- shell-script -*-
_python_modules()
{
COMPREPLY+=( $( compgen -W "$( ${1:-python} -c 'import pkgutil
for mod in pkgutil.iter_modules(): print(mod[1])' )" 2>/dev/null -- "$cur" ) )
}
_python()
{
local cur prev words cword
_init_completion || return
case $prev in
-'?'|-h|--help|-V|--version|-c)
return 0
;;
-m)
_python_modules "$1"
return 0
;;
-Q)
COMPREPLY=( $( compgen -W "old new warn warnall" -- "$cur" ) )
return 0
;;
-W)
COMPREPLY=( $( compgen -W "ignore default all module once error" \
-- "$cur" ) )
return 0
;;
!(?(*/)python*([0-9.])|-?))
[[ $cword -lt 2 || ${words[cword-2]} != -@(Q|W) ]] \
&& _filedir
;;
esac
# if '-c' is already given, complete all kind of files.
local i
for (( i=0; i < ${#words[@]}-1; i++ )); do
if [[ ${words[i]} == -c ]]; then
_filedir
fi
done
if [[ "$cur" != -* ]]; then
_filedir 'py?([co])'
else
COMPREPLY=( $( compgen -W '$( _parse_help "$1" -h )' -- "$cur" ) )
fi
return 0
} &&
complete -F _python python python2 python3
# ex: ts=4 sw=4 et filetype=sh编辑这个似乎没问题。
发布于 2017-04-25 03:39:32
问题是,这个case语句:!(?(*/)python*([0-9.])|-?))将制表符完成限制为只包含单词python的别名/命令。
如果您将这个case语句更改为:!(*|-h)),甚至是!(p|-h)),那么您的自动完成应该可以工作。
我会亲自将修改后的代码添加到我的.bashrc中.如下所示,只需重命名函数并执行混叠操作:
# bash completion for python -*- shell-script -*-
_python_modules_cust()
{
COMPREPLY+=( $( compgen -W "$( ${1:-python} -c 'import pkgutil
for mod in pkgutil.iter_modules(): print(mod[1])' )" 2>/dev/null -- "$cur" ) )
}
_python_cust()
{
local cur prev words cword
_init_completion || return
case $prev in
-'?'|-h|--help|-V|--version|-c)
return 0
;;
-m)
_python_modules_cust "$1"
return 0
;;
-Q)
COMPREPLY=( $( compgen -W "old new warn warnall" -- "$cur" ) )
return 0
;;
-W)
COMPREPLY=( $( compgen -W "ignore default all module once error" \
-- "$cur" ) )
return 0
;;
!(*|-?))
[[ $cword -lt 2 || ${words[cword-2]} != -@(Q|W) ]] \
&& _filedir
;;
esac
# if '-c' is already given, complete all kind of files.
local i
for (( i=0; i < ${#words[@]}-1; i++ )); do
if [[ ${words[i]} == -c ]]; then
_filedir
fi
done
if [[ "$cur" != -* ]]; then
_filedir 'py?([co])'
else
COMPREPLY=( $( compgen -W '$( _parse_help "$1" -h )' -- "$cur" ) )
fi
return 0
} &&
alias p='python3' &&
complete -F _python_cust p
# ex: ts=4 sw=4 et filetype=shhttps://stackoverflow.com/questions/43463902
复制相似问题