导航到目录中ZSH(庆典)

问题描述 投票:2回答:2

我使用Oh-My-ZSH创造一些ailises和功能对于缓解我的重复性的工作负荷。

我需要从任何地方在我的电脑浏览到我的前端目录。这是我有:

frontend(){
  cd ~/Desktop/Work/Frontend
  cd $1
}

现在这个效果很好,当我键入frontendfrontend myProject,但是,我的所有项目文件夹是由类似.m.tablet,后缀等。

我怎么能写的东西:

  • 让我自动定位到后跟.something文件夹
  • 当有多个选项(如project.mproject.tablet)会提示我有相似的,如果你打标签在你的终端,并给出了自动完成多种选择方案。

我希望我的问题是有道理的。

谢谢。

shell zsh oh-my-zsh
2个回答
4
投票

查找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

1
投票

我强烈建议你使用AutoJump

但是,如果你要,也许你想使用alias 就像你~/.zshrc添加:

alias fend='cd path/to/frontend'    
© www.soinside.com 2019 - 2024. All rights reserved.