分页: 1 / 1

自定义bash补全

发表于 : 2012-10-03 20:57
funicorn
配置文件路径~/.bash_completion

基本格式:

----------------------------------------------------------------------
# ~/.bash_completion, auto sourced by login shell

# function staement
_script_func(){

# local variable statment
local var1 var2 ..

# completion initialize
_init_completion || return

# completion reply message
COMPREPLY=( $( cmd arguments $var >/dev/null ) ) |string_parse_scrpt
# string_parse commands includes 'grep', 'cut', 'sort', 'tr', etc.

return 0

# key word insertion
COMPREPLY=( $( compgen -W 'keywords list' -- "$cur" ) )
return 0

}&&


# completion triger
complete -F _script_func script

_script2_func(){

...
...
} &&

complete -F _script2_fun script2

...
...

-------------------------------------------------------------------

例子:
--------------------------------------------------------------------------------
# ~/.bash_completion
# Debian user-defined completion -*- shell-script -*-

# bash_script_changeview: view changlog of debian package
# auto completion target: pkgnames in apt-cache repository

_changeview()
{
local cur
_init_completion || return

COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" 2> /dev/null ) )
return 0
} &&

complete -F _changeview changeview
--------------------------------------------------------------------------------
Usage:
~$ changeview linux-image
<tab> <tab>
linux-image linux-image-extra-virtual
linux-image-3.5.0-15-generic linux-image-generic
linux-image-3.5.0-16-generic linux-image-generic-pae
linux-image-3.5.0-16-lowlatency linux-image-lowlatency
linux-image-3.6.0-030600-generic linux-image-lowlatency-pae
linux-image-extra-3.5.0-15-generic linux-image-server
linux-image-extra-3.5.0-16-generic linux-image-virtual
linux-image-extra-3.6.0-030600-generic

Re: 自定义bash补全

发表于 : 2012-10-03 20:58
funicorn
chsdir项目作者写的一个较为完整的例子:


_filedir()
{
local i IFS=$'\n' xspec

_tilde "$cur" || return 0

local -a toks
local quoted tmp

_quote_readline_by_ref "$cur" quoted
toks=( $(
compgen -d -- "$quoted" | {
while read -r tmp; do
# TODO: I have removed a "[ -n $tmp ] &&" before 'printf ..',
# and everything works again. If this bug suddenly
# appears again (i.e. "cd /b<TAB>" becomes "cd /"),
# remember to check for other similar conditionals (here
# and _filedir_xspec()). --David
printf '%s\n' $tmp
done
}
))

if [[ "$1" != -d ]]; then
# Munge xspec to contain uppercase version too
# http://thread.gmane.org/gmane.comp.shel ... ocus=15306
xspec=${1:+"!*.@($1|${1^^})"}
toks+=( $( compgen -f -X "$xspec" -- $quoted ) )
fi

# If the filter failed to produce anything, try without it if configured to
[[ -n ${COMP_FILEDIR_FALLBACK:-} && \
-n "$1" && "$1" != -d && ${#toks[@]} -lt 1 ]] && \
toks+=( $( compgen -f -- $quoted ) )

if [[ ${#toks[@]} -ne 0 ]]; then
# 2>/dev/null for direct invocation, e.g. in the _filedir unit test
compopt -o filenames 2>/dev/null
COMPREPLY+=( "${toks[@]}" )
fi

chs=($(chsdir "x$1" "$cur"))
COMPREPLY=( "${COMPREPLY[@]}" "${toks[@]}" "${chs[@]}" )
} # _filedir()

_filedir_xspec()
{
local cur prev words cword
_init_completion || return

_expand || return 0

local IFS=$'\n' xspec=${_xspecs[${1##*/}]} tmp
local -a toks

toks=( $(
compgen -d -- "$(quote_readline "$cur")" | {
while read -r tmp; do
# see long TODO comment in _filedir() --David
printf '%s\n' $tmp
done
}
))

# Munge xspec to contain uppercase version too
# http://thread.gmane.org/gmane.comp.shel ... ocus=15306
eval xspec="${xspec}"
local matchop=!
if [[ $xspec == !* ]]; then
xspec=${xspec#!}
matchop=@
fi
xspec="$matchop($xspec|${xspec^^})"

toks+=( $(
eval compgen -f -X "!$xspec" -- "\$(quote_readline "\$cur")" | {
while read -r tmp; do
[[ -n $tmp ]] && printf '%s\n' $tmp
done
}
))

if [[ ${#toks[@]} -ne 0 ]]; then
compopt -o filenames
COMPREPLY=( "${toks[@]}" )
fi

chs=($(chsdir "x$1" "$cur"))
COMPREPLY=( "${toks[@]}" "${chs[@]}" )
}

complete -o filenames -F _filedir_xspec file

Re: 自定义bash补全

发表于 : 2012-10-03 21:04
eexpress
我一直使用自己的补全。
哪里要这么复杂。。。