Bash 为您提供了一种指定关键字的方法,并使用它们
为您的应用程序自动完成命令行参数。我用 vim
作为 wiki、任务管理器和联系人。 vim helptags 系统让
我索引内容而不是搜索它,以及速度
显示它。我想添加的一个功能是访问这些标签
从外部 vim。
这可以直接完成:
$ vim -t tagname
这会将我直接带到使用此标签标记的特定内容。但是,如果我能提供,这将更有成效
标签的自动完成。
我首先为 vim 命令行定义了一个 Bash 函数。我添加了
在我的 .bashrc 文件中添加以下代码:
function get {
vim -t $1
} Now I can use get tagname command to get to the content.
Bash 可编程完成是通过采购
/etc/bash 完成脚本。该脚本让我们添加我们的
自动完成脚本 /etc/bash-completion.d/ 目录并执行
每当它被调用时。所以我添加了一个名为 get 的脚本文件
该目录中的以下代码。
_get()
{
local cur
COMPREPLY=()
#Variable to hold the current word
cur="${COMP_WORDS[COMP_CWORD]}"
#Build a list of our keywords for auto-completion using
#the tags file
local tags=$(for t in `cat /home/anadgouda/wiki/tags | \
awk '{print $1}'`; do echo ${t}; done)
#Generate possible matches and store them in the
#array variable COMPREPLY
COMPREPLY=($(compgen -W "${tags}" $cur))
}
#Assign the auto-completion function _get for our command get.
complete -F _get get Once the /etc/bash-completion is sourced, you will get auto-completion for the tags when you use the get command.
连同我的 wiki,我将它用于所有文档工作,并在
也是代码的倍数。我还使用从我的代码创建的标签文件。这
索引系统让我记住上下文而不是文件名
和目录。
您可以针对您使用的任何工具调整此系统。一切你需要的
要做的是获取命令的关键字列表并将其提供给
Bash 可编程补全系统。