Created
July 29, 2016 11:13
-
-
Save izabera/ac53bf9ac6d59cf89db402cb08d393a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
autocompletion () { | |
local pre word rest i list expanded | |
pre=${READLINE_LINE:0:READLINE_POINT} | |
word=${pre##* } | |
rest=${pre%"$word"} | |
list=(correct horse battery staple startup hotel) # possible completions | |
(( ! ${#word} )) && return | |
for i in "${list[@]}"; do | |
[[ ${#word} -lt ${#i} && $i = "$word"* ]] && expanded+=("$i") # found a match | |
done | |
case ${#expanded[@]} in | |
0) ;; | |
1) READLINE_LINE=$rest$expanded${READLINE_LINE:READLINE_POINT} # replace | |
(( READLINE_POINT += ${#expanded} - ${#word} )) ;; # move cursor | |
*) printf "%s\\n" "${expanded[@]}" -- | |
esac | |
} | |
set -o emacs | |
bind -x '"\C-i":autocompletion' | |
while read -ep "> " ; do | |
[[ $REPLY ]] || continue # ignore empty lines | |
if hist=$(history -p "$REPLY") 2>/dev/null; then # can perform history expansion? | |
REPLY=$hist # if so, ok let's do it | |
fi | |
history -s -- "$REPLY" # add this line to history | |
printf "your line was: %s\n" "$REPLY" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment