Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Created April 3, 2021 03:21
Show Gist options
  • Save SomajitDey/3fdec7fd084601ae10132697dcbb9d9e to your computer and use it in GitHub Desktop.
Save SomajitDey/3fdec7fd084601ae10132697dcbb9d9e to your computer and use it in GitHub Desktop.
Shell-function to give current word of Bash readline, i.e. "read -e". To be used with key-binding for custom TAB completion
### Source me
readline_cword(){
# Gives current word of Bash readline, viz. the word where the insertion point/cursor is currently at
set -- ${READLINE_LINE}
local length=${#1}
while ((length < READLINE_POINT)); do
shift
length+=${#1}
done
echo -n "${1}"
}; export -f readline_cword
# Use case:
# Custom TAB completion:
# Single TAB: bind -x '"\t":custom__completion_function "$(readline_cword)"'
# Double TAB: bind -x '"\t\t":custom__list_suggestions_function "$(readline_cword)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment