我想知道是否有人可以做到这一点。假设我有这个文件夹结构:
Folder A
Folder Apple
Folder Orange
如果我目前在文件夹 A 中,我希望这样如果我键入“cd Ap”并按 Enter,它会自动将我放入“Apple”子文件夹中。本质上,它会尝试根据部分输入自动完成并打开文件夹。
如果我当前在文件夹 A 中,然后键入“cd ap”并按 Enter(小写“a”),我会收到错误消息,因为它无法自动完成实际的子文件夹名称。这可能吗?我在科恩工作。
我不会完全回答你的问题,但我会接近的。在我看来,按 Tab 键对您来说不是障碍,而是大写。我知道那种感觉,就像在驼峰式和不方便的打字之间做出选择。
我只在bash中做过这个,我的道歉。如果我记得,bash 和 ksh 相当接近,所以我希望它对你有用。
set completion-ignore-case on
在 bash 中打开不区分大小写的完成。自然,这会进入您可能想要的任何启动脚本中。
祝你好运,告诉我们它是否适用于 ksh !
这是一个 ksh 函数(未经测试)
cd () {
typeset prefix=$1
typeset destination=""
for f in *; do
[[ -d "$f" ]] || continue
case "$f" in
"$prefix"*) destination="$f"; break ;;
esac
done
if [[ -z "$destination" ]]; then
print -u2 "error: can't find directory with prefix '$prefix'"
else
command cd "$destination"
fi
}
使用 ksh,Esc\相当于 bash 制表符补全。
对于 Bash,您可以将以下内容添加到您的~/.bashrc
. 默认情况下,它将进行不区分大小写的匹配。它有点长,但它应该处理你扔给它的任何东西(除了尝试cd ../my_direc
从符号链接目录自动完成(有关更多信息,请参见此处)。
如果您使用此脚本并将其保留为不区分大小写,您还不如添加bind 'set completion-ignore-case on'
到您的~/.bashrc
以便 TAB 完成也是不区分大小写的。
cd() {
# Attempts to autocomplete the directory name
#
# If it fails to find a match, it'll still execute the input, in case the argument was
# something like "-".
case_insens=1 # set to one if you want it to try case-insensitive matching
# for exact matches, cd immediately
if [ -d "$1" ]; then
builtin cd "$1"
return
fi
# deal with no arguments passed
if [ $# -eq 0 ]; then
builtin cd
return
fi
# first loop for case-sensitive (since we prefer a case-sensitive match)
# for more on this globbing, see: bit.ly/1CZ9qym
for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
# skip if this result is not a directory
[ -d "$element" ] || continue
if [[ "$(basename "$element")" == "$(basename "$1")"* ]]; then
# if there's no ambiguity, switch to that folder
if [ $(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
echo "'$1' matches multiple results: "
echo "$(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null)"
# try to cd anyway
builtin cd "$1" &> /dev/null
unset case_insens element
return
else
builtin cd "$element"
unset case_insens element
return
fi
fi
done
if [ $case_insens -eq 1 ]; then
#case-insensitive argument
ci_arg="${1,,}"
else
builtin cd "$1"
unset case_insens element
return
fi
#Case-insensitive loop
for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
# skip if this result is not a directory
[ -d "$element" ] || continue
ci_element_name="$(basename "${element,,}")"
if [[ "$ci_element_name" == "$(basename "$ci_arg")"* ]]; then
# if there's no ambiguity, switch to that folder
if [ $(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
echo "'$ci_arg' matches multiple results: "
echo "$(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null)"
# try to cd anyway
builtin cd "$1" &> /dev/null
unset ci_arg case_insens ci_element element
return
else
builtin cd "$element"
unset ci_arg case_insens ci_element element
return
fi
fi
done
# we still haven't found a match, so pass the (faulty) argument to the cd command
builtin cd "$1"
unset ci_arg case_insens ci_element element
}
cd ~
cd deskt