如何使zsh函数作为文件自动完成? [英] How do I make a zsh function autocomplete as a file?
问题描述
我使用zsh并编写了一个函数来替换cd函数.在一些帮助下,我得到了它的工作,就像我想要的那样(大多数情况下).这是对我的另一个问题的跟进.该功能几乎可以按我希望的方式工作,但是语法突出显示和自动完成功能仍然存在一些问题.
I use zsh and wrote a function to replace cd function. With some help I got it to work like I want it to (mostly). This is a followup to one of my other question. The function almost works like I want it to, but I still have some problems with syntax highlighting and autocompletion.
对于示例,假设您的目录如下:
For the examples, lets say your directories look like this:
/
a/
b/
c/
d/
some_dir/
我还假设已获取以下代码:
I am also assuming the following code has been sourced:
cl () {
local first=$( echo $1 | cut -d/ -f1 )
if [ -d $first ]; then
pushd $1 >/dev/null # If the first argument is an existing normal directory, move there
else
pushd ${PWD%/$first/*}/$1 >/dev/null # Otherwise, move to a parent directory or a child of that parent directory
fi
}
_cl() {
_cd
pth=${words[2]}
opts=""
new=${pth##*/}
local expl
# Generate the visual formatting and store it in `$expl`
_description -V ancestor-directories expl 'ancestor directories'
[[ "$pth" != *"/"*"/"* ]] && middle="" || middle="${${pth%/*}#*/}/"
if [[ "$pth" != *"/"* ]]; then
# If this is the start of the path
# In this case we should also show the parent directories
local ancestor=$PWD:h
while (( $#ancestor > 1 )); do
# -f: Treat this as a file (incl. dirs), so you get proper highlighting.
# -Q: Don't quote (escape) any of the characters.
# -W: Specify the parent of the dir we're adding.
# ${ancestor:h}: The parent ("head") of $ancestor.
# ${ancestor:t}: The short name ("tail") of $ancestor.
compadd "$expl[@]" -fQ -W "${ancestor:h}/" - "${ancestor:t}"
# Move on to the next parent.
ancestor=$ancestor:h
done
else
# $first is the first part of the path the user typed in.
# it it is part of the current direoctory, we know the user is trying to go back to a directory
first=${pth%%/*}
# $middle is the rest of the provided path
if [ ! -d $first ]; then
# path starts with parent directory
dir=${PWD%/$first/*}/$first
first=$first/
# List all sub directories of the $dir/$middle directory
if [ -d "$dir/$middle" ]; then
for d in $(ls -a $dir/$middle); do
if [ -d $dir/$middle/$d ] && [[ "$d" != "." ]] && [[ "$d" != ".." ]]; then
compadd "$expl[@]" -fQ -W $dir/ - $first$middle$d
fi
done
fi
fi
fi
}
compdef _cl cl
问题:回到父目录时,补全通常有效.但是,当您转到父目录的子目录时,建议是错误的(显示的是您键入的完整路径,而不仅仅是子目录).
The problem: When going back to a parent directory, complition mostly works. But when you go to a child of the parent directory, the suggestion is wrong (the full path you typed is displayed, not just the child directory).
示例:
$ cd /a/c/d
$ cl a/[tab] # What it currently displays
a/b a/c a/some_dir
$ cl a/[tab] # What I want it to display
b c some_dir
如何显示第二条建议?
推荐答案
通过提供带有 -d
选项:
local -a disp
# (N) gives an empty array when there are no matches, rather than an error.
# (:t) returns the last segment of each path.
for d in $dir/$middle*(N:t); do
disp=( "$d" )
compadd "$expl[@]" -fQ -W $dir/ -d disp - $first$middle$d
done
文档:
- http://zsh.sourceforge.net/Doc/Release/Completion-Widgets.html#Completion-Builtin-Commands
- http://zsh.sourceforge.net/Doc/Release/Expansion.html#Glob-Qualifiers
这篇关于如何使zsh函数作为文件自动完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!