我使用Oh-My-ZSH创造一些ailises和功能对于缓解我的重复性的工作负荷。
我需要从任何地方在我的电脑浏览到我的前端目录。这是我有:
frontend(){
cd ~/Desktop/Work/Frontend
cd $1
}
现在这个效果很好,当我键入frontend
或frontend myProject
,但是,我的所有项目文件夹是由类似.m
,.tablet
,后缀等。
我怎么能写的东西:
.something
文件夹project.m
和project.tablet
)会提示我有相似的,如果你打标签在你的终端,并给出了自动完成多种选择方案。我希望我的问题是有道理的。
谢谢。
查找zsh
解决方案第一,其次是bash
解决方案。
更新:原来,一个zsh
实现(基于内置compctl
)比bash
实现(基于内置complete
)要简单得多。
感兴趣的代码保存到文件中(例如,frontend
)和源它(例如,. ./frontend
);以交互方式或,优选地,从您的bash / zsh的轮廓。
一旦到位,~/Desktop/Work/Frontend
子目录名称的自动完成将工作如下:
frontend myProject
和按TAB键。myProject
是那么前缀匹配反对~/Desktop/Work/Frontend
子目录的名称:
如果只有1场,myProject
将立即扩大到全子目录名。
否则,发出哔哔声,表明有多个匹配:
zsh
:所有匹配的子目录的名称列出的时候了。
bash
:按TAB键再次列出所有匹配的子目录的名称
继续输入直到前缀匹配是明确的,然后再按TAB。注:在bash
,也只需要按下TAB一次列出多个场比赛,以下内容添加到您的壳轮廓bind "set show-all-if-ambiguous on"
。
zsh的解决方案:
# Define the shell function.
frontend(){
cd ~/Desktop/Work/Frontend/"${1:-}"
}
# Tell zsh to autocomplete directory names in the same directory as
# the function's when typing a command based on the shell function.
compctl -/ -W ~/Desktop/Work/Frontend frontend
击的解决方案:
注:complete -o dirnames
不带任何参数,遗憾的是 - 为当前目录它总是自动完成。因此,定制的外壳的函数,返回的潜在匹配,与-o filenames
组合,是必需的。
# Define the main shell function.
frontend(){
local BASEDIR=~/Desktop/Work/Frontend
cd "$BASEDIR/${1:-}"
}
# Define the custom completion function.
_frontend_completions() {
local BASEDIR=~/Desktop/Work/Frontend
# Initialize the array variable through which
# completions must be passed out.
COMPREPLY=()
# Find all matching directories in the base folder that start
# with the name prefix typed so far and return them.
for f in "$BASEDIR/${COMP_WORDS[COMP_CWORD]}"*; do
[[ -d $f ]] && COMPREPLY+=( "$(basename "$f")" )
done
}
# Tell bash to autocomplete directory names as returned by the
# _frontend_completions() helper functoin when typing a command
# based on the main shell function.
complete -o filenames -F _frontend_completions frontend fe