给大家推荐一个终端下面的中文补全工具

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
jiandan23
帖子: 86
注册时间: 2010-12-17 22:31
系统: Mint 19.2

给大家推荐一个终端下面的中文补全工具

#1

帖子 jiandan23 » 2023-05-24 15:57

我最近在某个终端下碰到不能使用中文输入法的问题,导致遇到中文目录时,cd 进去比较麻烦,于是在github上找到这个小工具:
https://github.com/bailiangcn/tools/tre ... ir_install
它的功能如下:

user@host:temp$ ls
SVN02 SVN培训 全球眼 浙江建行 浙江农信
user@host:temp$ cd zj #按两下TAB,自动补全中会同时列出中文文件名
浙江建行/ 浙江农信/
user@host:temp$



安装:
只需要下载https://github.com/bailiangcn/tools/tree/master/bin/chsdir_install这个链接下的chsdir这个文件。
补全脚本不要使用链接里的chs_completion,版本比较老,我的Mint系统就有问题。
其实只需要改动下/usr/share/bash-completion/bash_completion这个补全脚本里的_filedir和_filedir_xspec两个函数就可以了:

_filedir ()
{
.......
while read -r tmp; do
toks+=("$tmp");
done <<< "$x";
fi;
chs=($(/opt/chsdir/chsdir "x$1" "$cur")); #红色这两行是添加的,chsdir路径要写对,其他不变
toks+=("${chs[@]}")

if [[ ${#toks[@]} -ne 0 ]]; then
compopt -o filenames 2> /dev/null;
COMPREPLY+=("${toks[@]}");
fi
}

_filedir_xspec ()
{
......
toks+=($(
eval compgen -f -X "'!$xspec'" -- "\$(quote_readline "\$cur")" | {
while read -r tmp; do
[[ -n $tmp ]] && printf '%s\n' $tmp
done
}
));
chs=($(/opt/chsdir/chsdir "x$1" "$cur")); #红色这两行是添加的,其他不变
toks+=("${chs[@]}")

if [[ ${#toks[@]} -ne 0 ]]; then
compopt -o filenames;
COMPREPLY=("${toks[@]}");
fi
}
回复