Fall back to _filedir, compgen -f, etc. if no matches are returned
Programs that use omelette completion can no longer complete files and directories. I modified my completion script to fall back to _filedir (it relies on the $cur and $prev variables):
_p_complette() {
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${prev}" "${COMP_LINE}")' -- "$cur") )
[[ $COMPREPLY ]] && return
_filedir
}
complete -F _p_complette p
In the same way compgen -f with compopt -o filenames could be used, I think.
+1
@beaugunderson Is there a way I can use your solution in my own omelette integration?
@wcandillon yeah, just modify the completion function after omelette generates it; here's the diff of mine:
--- projects-unmodified.sh 2015-03-17 18:23:39.000000000 -0700
+++ projects-modified.sh 2014-12-13 14:00:02.000000000 -0800
@@ -6,8 +6,39 @@
compdef _projects_complette projects
elif type complete &>/dev/null; then
_projects_complette() {
- COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${COMP_WORDS[COMP_CWORD-1]}" "${COMP_LINE}")' -- "${COMP_WORDS[COMP_CWORD]}") )
+ local cur prev
+
+ cur=${COMP_WORDS[COMP_CWORD]}
+ prev=${COMP_WORDS[COMP_CWORD-1]}
+
+ COMPREPLY=( $(compgen -W '$(projects --compbash --compgen "${COMP_CWORD}" "${prev}" "${COMP_LINE}")' -- "$cur") )
+
+ [[ $COMPREPLY ]] && return
+
+ _filedir
}
complete -F _projects_complette projects
fi
### projects completion - end ###
projects here is the name of the command I'm adding tab completion to.
The changes I made were:
- Use the
curandprevvariables that the bash builtins like_filedirrely on - Try the omelette completion, and if it returns anything, return from the function
- If it doesn't return anything, try completing with
_filedir, which is a builtin that completes files and directories
@beaugunderson thks a lot!
@beaugunderson On the fallback I'm getting: _filedir: command not found
@wcandillon What operating system?
On OS X you can brew install bash-completion if you have homebrew installed.
On Ubuntu you can apt-get install bash-completion.
You could also try replacing the line _filedir with:
compopt -o filenames
compgen -f
But I think _filedir provides some additional niceties so it's worth installing bash-completion.