Skip to content

Instantly share code, notes, and snippets.

@izabera
Created July 29, 2016 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izabera/ac53bf9ac6d59cf89db402cb08d393a2 to your computer and use it in GitHub Desktop.
Save izabera/ac53bf9ac6d59cf89db402cb08d393a2 to your computer and use it in GitHub Desktop.
#!/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