具有参数和自动补全功能的bash别名 [英] bash alias with argument and autocompletion

查看:67
本文介绍了具有参数和自动补全功能的bash别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在路径中的目录中有一堆脚本,所以 无论我身在何处,我都可以访问它们.有时候,那些是非常简单的util脚本, "vims"文件.我不时希望快速查看脚本文件的内容,并查看脚本打开的文件路径(然后制作cat,grep ...).

I have a bunch of scripts in directory that exists on the path, so I can access each wherever I am. Sometime those are very simple util scripts that "vims" the file. From time to time I would like to quickly see the content of script file and see path to file the script opens (then make cat, grep ...).

无论我在哪里,我都想创建一个别名,该别名将捉住"给定脚本.
给定一个不起作用:
alias a="cat `which \$1`"
如果我放置脚本名称而不是参数number($ 1),它可以正常工作.但 参数不是.

I would like to make an alias which will "cat" given script wherever I am.
Given one is not working:
alias a="cat `which \$1`"
If I place script name instead of parameter number($1) it works fine. But with parameter not.

第二个问题(我希望生活如此美好!)将会越来越 该别名的脚本名称自动完成.
使用我的"bin"目录中可能存在的脚本,这是我可以采用的另一种方法.

The second question (I wish life be so so beautiful!) would be getting auto-completion of script name for that alias.
Using a script that could exist in my "bin" directory would another approach which I can take.

推荐答案

如果您的函数名为"foo",则您的完成函数应如下所示:

If your function is called "foo" then your completion function could look like this:

如果您已安装了Bash完成包:

If you have the Bash completion package installed:

_foo () { local cur; cur=$(_get_cword); COMPREPLY=( $( compgen -c -- $cur ) ); return 0; }

如果不这样做:

_foo () { local cur; cur=${COMP_WORDS[$COMP_CWORD]}; COMPREPLY=( $( compgen -c -- $cur ) ); return 0; }

然后启用它:

complete -F _foo foo

命令compgen -c将使完成内容包括系统上的所有命令.

The command compgen -c will cause the completions to include all commands on your system.

您的函数"foo"可能看起来像这样:

Your function "foo" could look like this:

foo () { cat $(type -P "$@"; }

cat一个或多个文件名称作为参数传递的文件.

which would cat one or more files whose names are passed as arguments.

这篇关于具有参数和自动补全功能的bash别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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