简体   繁体   English

创建别名(或类似名称),该别名在cd进入特定文件夹后会激活

[英]Creating alias (or something similar) that activates when cd into a specific folder

Is it possible to create aliases when I enter a certain folder? 输入某个文件夹时可以创建别名吗?

What I want: 我想要的是:

I use composer a lot (a PHP package manager), which installs binaries in ./vendor/bin . ./vendor/bin使用composer (一个PHP包管理器),它将二进制文件安装在./vendor/bin I would like to run the binaries directly from . 我想直接从运行二进制文件. .

For example: 例如:

/path/to/project
 | - composer.json     // dictates dependencies for the project
 | - vendor            // libs folder for composer, is created by composer
 |    | - bin          // if lib has bin, composer creates this folder
 |    |    | phpunit   // binary
 |    |    | phinx     // binary
 |    | - somelib1     // downloaded by composer
 |    | - somelib2     // downloaded by composer

Is it possible to get this to work: 是否可以使它工作:

> cd /path/to/project
> phpunit

And get phpunit to execute? 并让phpunit执行?

Something like "sensing" the composer.json file and dynamically find the binaries in ./vendor/bin and then do something like alias="./vendor/bin/<binary-name> $@" automatically? 诸如“感应” composer.json文件并在./vendor/bin动态查找二进制文件,然后自动执行诸如alias="./vendor/bin/<binary-name> $@"类的事情?

I use OS X 10.9 and the boxed in Terminal app. 我使用OS X 10.9,并在“ 终端”应用中装箱。

You can override cd , trap my_function DEBUG to run something on every command, or add a command into PS1 or PROMPT_COMMAND . 您可以覆盖cdtrap my_function DEBUG以便在每个命令上运行某些命令,或者将命令添加到PS1PROMPT_COMMAND

These have different behaviour and caveats, and I can't recommend doing any of them for this use case (after having used each of them at some point). 它们具有不同的行为和警告,因此我不建议针对此用例对它们进行任何处理(在某些时候使用了它们之后)。 They are bad solutions to XY problems . 它们是解决XY问题的不好的方法。

An alternative which is much less likely to break things horribly is to create a custom function to do both things: 一个不太可能造成严重破坏的替代方法是创建一个自定义函数来完成这两项工作:

cdp() {
    cd "$@" && phpunit
}

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

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