简体   繁体   English

OSX 中别名的制表符补全

[英]Tab completion for alias in OSX

I have an alias to change the directory as given below.我有一个别名来更改目录,如下所示。

test(){
cd /Users/newuser/test/sub1/sub2/$1
}

So when running test foldername , it will change the directory to a respective subdirectory in sub2 .因此,在运行test foldername时,它会将目录更改为sub2中的相应子目录。

I used the below code for tab completion我使用下面的代码来完成标签

_cdp () {
  … # code goes here
}
compdef _cdp cdp

as answered here Alias to cd into a subdirectory with completion .在这里回答Alias to cd into a subdirectory with completion However, sourcing the profile triggered an error compdef not found.但是,获取配置文件会触发错误 compdef not found。

NB: It is using a bash shell, not zsh.注意:它使用的是 bash shell,而不是 zsh。

How can I implement a tab completion for the alias so that, if a Tab is done after the alias, it will list the possible folders如何为别名实现选项卡完成,这样,如果在别名之后完成选项卡,它将列出可能的文件夹

compdef is a zsh command only. compdef只是一个 zsh 命令。 For bash use complete .对于 bash 使用complete的 .

You can use programmable completion as in your example, where you specify a function (here _cdp ) that generates the possible completions by adding them to the array COMPREPLY+=(some new entries) .您可以在示例中使用可编程完成,在其中指定 function (此处_cdp ),通过将它们添加到数组COMPREPLY+=(some new entries)来生成可能的完成。 Often, these entries are generated by compgen .通常,这些条目是由compgen生成的。

complete -F _cdp cdp

But in this case there is an easier option: Specify the possible completions directly using但在这种情况下,有一个更简单的选择:直接使用指定可能的完成

IFS=$'\n' complete -W "$(cd /Users/newuser/test/sub1/sub2/ && compgen -G '*/')" cdp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM