编写自定义 bash 完成规则
我的目录充满了具有相同前缀的文件,我希望能够在 vim 中快速打开它们。例如,我可能有:
$ ls *
bar:
bar_10 bar_20 bar_30
foo:
foo_10 foo_20 foo_30
我想要的是能够位于这些目录之一并输入:
$ vim <TAB>
并且它自动完成为:
$ vim bar_
为了实现这一点,我很高兴每个目录都有一个名为“.completion”的文件,其中包含“bar_” ”在其中。
我遇到的问题是我想要以下行为
* "vim <TAB>" --> "vim bar_" // no space
* "vim bar_1" --> "vim bar_10 " // space
:是光标,因此如果文件匹配,则在末尾添加空格。如果我们要匹配前缀,请不要添加空格。
到目前为止我所拥有的最好的就是这种行为减去在末尾添加空格。我尝试了各种方法,但都无济于事。以下是我所拥有的:
_vim()
{
local cur opts
local -a toks
cur="${COMP_WORDS[COMP_CWORD]}"
if [ -f .completion ]; then
opts=`cat .completion`
if [[ ${opts} = ${cur} ]]; then
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
else
if [[ -z ${cur} ]]; then
toks=( $(compgen -W "${opts}" -- ${cur}) )
else
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
fi
fi
else
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
fi
COMPREPLY=( "${toks[@]}" )
}
complete -F _vim -o nospace vim
任何关于如何让它在文件名完成后添加空格但不在前缀完成后添加空格的想法将不胜感激。
I have directories full of files with the same prefix, which I want to be able to quickly open in vim. For example, I might have:
$ ls *
bar:
bar_10 bar_20 bar_30
foo:
foo_10 foo_20 foo_30
What I want is to be able to be in one of these directories and type:
$ vim <TAB>
and it autocomplete to:
$ vim bar_
To achieve this I am happy to have a file per directory called ".completion" which has "bar_" in it.
The issue I have is I would like the following behaviour:
* "vim <TAB>" --> "vim bar_" // no space
* "vim bar_1" --> "vim bar_10 " // space
Where | is the cursor, so if a file matches, add the space on the end. If we're matching the prefix, don't add a space.
The best I have so far is this behaviour minus the adding a space at the end. I've tried all sorts of things, all to no avail. The following is what I have:
_vim()
{
local cur opts
local -a toks
cur="${COMP_WORDS[COMP_CWORD]}"
if [ -f .completion ]; then
opts=`cat .completion`
if [[ ${opts} = ${cur} ]]; then
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
else
if [[ -z ${cur} ]]; then
toks=( $(compgen -W "${opts}" -- ${cur}) )
else
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
fi
fi
else
toks=( $(compgen -f ${cur} | sed -e 's/$/ /') )
fi
COMPREPLY=( "${toks[@]}" )
}
complete -F _vim -o nospace vim
Any ideas on how I can get it to add the space after the file name completion, but not after the prefix completion would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sed
添加的尾随空格被删除。试试这个:The trailing space that
sed
is adding is getting dropped. Try this: