I'm using bash and I was wondering if there is any plugin in bash or other shell which allows me to do this cat tfnislong.txt
+ [tab]
completes to cat ThisFileNameIsSoLong.txt
just like tab completion in Sublime Text.
Answer
I know of https://github.com/mgalgs/fuzzy_bash_completion which is a bunch of shell functions used with complete
.
I think we could probably do a little better though, be more concise. Time to bust open your .bashrc or .profile...
The basic hook we use is this:
complete -o nospace -o filenames -F fuzzypath cd ls cat
-F option means use a shell function for the tab-completion, where the options are set in the COMPREPLY
variable. So for example:
function fuzzypath() {
if [ -z $2 ]
then
COMPREPLY=( `ls` )
else
DIRPATH=`echo "$2" | gsed 's|[^/]*$||'`
BASENAME=`echo "$2" | gsed 's|.*/||'`
FILTER=`echo "$BASENAME" | gsed 's|.|\0.*|g'`
COMPREPLY=( `ls $DIRPATH | grep -i "$FILTER" | gsed "s|^|$DIRPATH|g"` )
fi
}
where DIRPATH
is everything before the last / on your path that you're typing and BASENAME
is where your cursor is currently.
FILTER
is where the fuzzy-ness comes in. Basically, take BASENAME
and insert a .*
between every char to produce our fuzz regex.
The rest is just combining the regex with ls to produce COMPREPLY
. You may find more clever things to do here, maybe with find
.
I also found http://fahdshariff.blogspot.com/2011/04/writing-your-own-bash-completion.html to be useful.
No comments:
Post a Comment