重击
set completion-ignore-case on
在~/.inputrc
(或bind 'set completion-ignore-case on'
中~/.bashrc
)将是我的建议。如果要输入全名,为什么不按几次Shift键就不知道呢?
但是,如果您真的想要它,这里有一个包装,cd
它尝试进行完全匹配,如果没有,则寻找不区分大小写的匹配,如果唯一则执行匹配。它使用nocaseglob
shell选项进行不区分大小写的glob,并通过追加@()
(不匹配任何内容,并要求extglob
)将参数转换为glob 。extglob
定义函数时必须打开该选项,否则bash甚至无法解析它。此功能不支持CDPATH
。
shopt -s extglob
cd () {
builtin cd "$@" 2>/dev/null && return
local options_to_unset=; local -a matches
[[ :$BASHOPTS: = *:extglob:* ]] || options_to_unset="$options_to_unset extglob"
[[ :$BASHOPTS: = *:nocaseglob:* ]] || options_to_unset="$options_to_unset nocaseglob"
[[ :$BASHOPTS: = *:nullglob:* ]] || options_to_unset="$options_to_unset nullglob"
shopt -s extglob nocaseglob nullglob
matches=("${!#}"@()/)
shopt -u $options_to_unset
case ${#matches[@]} in
0) # There is no match, even case-insensitively. Let cd display the error message.
builtin cd "$@";;
1)
matches=("$@" "${matches[0]}")
unset "matches[$(($#-1))]"
builtin cd "${matches[@]}";;
*)
echo "Ambiguous case-insensitive directory match:" >&2
printf "%s\n" "${matches[@]}" >&2
return 3;;
esac
}
sh
当我在使用它时,这是ksh93的类似功能。该~(i)
修改为不区分大小写的匹配似乎是不兼容的/
后缀来只匹配目录(这可能是我在KSH释放的错误)。因此,我使用另一种策略来清除非目录。
cd () {
command cd "$@" 2>/dev/null && return
typeset -a args; typeset previous target; typeset -i count=0
args=("$@")
for target in ~(Ni)"${args[$(($#-1))]}"; do
[[ -d $target ]] || continue
if ((count==1)); then printf "Ambiguous case-insensitive directory match:\n%s\n" "$previous" >&2; fi
if ((count)); then echo "$target"; fi
((++count))
previous=$target
done
((count <= 1)) || return 3
args[$(($#-1))]=$target
command cd "${args[@]}"
}
sh
最后,这是zsh版本。同样,允许不区分大小写的完成可能是最好的选择。如果没有精确的大小写匹配,则以下设置将退回到不区分大小写的状态:
zstyle ':completion:*' '' matcher-list 'm:{a-z}={A-Z}'
删除''
以显示所有不区分大小写的匹配项,即使存在完全区分大小写的匹配项也是如此。您可以从的菜单界面进行设置compinstall
。
cd () {
builtin cd "$@" 2>/dev/null && return
emulate -L zsh
setopt local_options extended_glob
local matches
matches=( (#i)${(P)#}(N/) )
case $#matches in
0) # There is no match, even case-insensitively. Try cdpath.
if ((#cdpath)) &&
[[ ${(P)#} != (|.|..)/* ]] &&
matches=( $^cdpath/(#i)${(P)#}(N/) ) &&
((#matches==1))
then
builtin cd $@[1,-2] $matches[1]
return
fi
# Still nothing. Let cd display the error message.
builtin cd "$@";;
1)
builtin cd $@[1,-2] $matches[1];;
*)
print -lr -- "Ambiguous case-insensitive directory match:" $matches >&2
return 3;;
esac
}
backUP
和backUp
,那么您将backup
不希望进入哪个目录?