如何使一个自定义的功能BASH cd到某个目录与自动完成 [英] How to make a custom BASH function to cd into a certain directory with autocomplete

查看:319
本文介绍了如何使一个自定义的功能BASH cd到某个目录与自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的项目在〜/文档/ EP_example_project 命名方案。他们都住在〜/文稿

I have my projects in a ~/Documents/EP_example_project naming scheme. They all live under ~/Documents.

我想创建一个bash功能,可以让我cd到从无论我是在系统上的项目目录;与自动完成。像这样:

I would like to create a bash-function that allows me to cd into a project-directory from whereever I am on the system; with autocomplete. Like so:

cdp E<tab>
# ... EAP_example_additional_project EP_example_project
P<tab>
# prompt now says:
cdp EP_example_project<enter>
# ... And I am in the project-directory.

这将是一个更容易一点,然后键入 CD〜/走走LT;标签&gt;&EP LT;标签&gt; 每次。

That would be a bit easier then typing cd ~/Do<tab>EP<tab> everytime.

我知道如何创建一个简单的功能的基础知识,但无法找到如何标签的自动完​​成补充。

I do know the basics about creating a simple function, but cannot find how to add tab-autocompletion to that.

注:在上面的推理意义不大:额外的〜/做的不是的的开销很大,那是另外的事情,我可以在这样的功能呢,一定像打印内容文件,如果找到(.birdseye),列出项目的内容,最后访问日期等,使这更有趣的未来。

NOTE: while above reasoning makes little sense: the additional ~/Do is not that much overhead, it is the additional things I can do in such a function, like print the contents of a certain file if found (.birdseye), list the projects contents, last accessed dates and so on, that make this more interesting in future.

推荐答案

您可以考虑 CDPATH 。您可以出口CDPATH =〜/文稿来都在增加了自动完成〜/文件的目录。你可以为这个命令导出的函数或别名,并设置与放大器;未设置 CDPATH ,当你需要的。结果
希望这有助于!

You could consider CDPATH. You can export CDPATH=~/Documents to have all the directories under ~/Documents added for autocompletion. You can have a function or alias for this export command and set & unset CDPATH as and when you need.
Hope this helps!

这篇关于如何使一个自定义的功能BASH cd到某个目录与自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆