bash alias with argument and autocompletion

前端 未结 2 1579
青春惊慌失措
青春惊慌失措 2021-02-02 01:30

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 tim

2条回答
  •  误落风尘
    2021-02-02 02:21

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

    If you have the Bash completion package installed:

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

    If you don't:

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

    Then to enable it:

    complete -F _foo foo
    

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

    Your function "foo" could look like this:

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

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

提交回复
热议问题