私はbash v4.3.46を搭載したUbuntu 16.04 LTSを使用しています。
エイリアス「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
動いているようですが、最初と同じ結果が得られました。
-------追加--------
/usr/share/bash-completion/completions/python3
で_pythonの定義を見つけました
# 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]} != [email protected](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
これを編集しても問題ないようです。
これをどのように編集するか教えていただけますか?
これを編集するには、sudoが必要です。sudoを使用しない他の方法はありますか?
ありがとうございました。
問題は、このケースステートメント:
!(?(*/)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]} != [email protected](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=sh