Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Created February 23, 2017 17:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thcipriani/3b67432310ac6bfba26f01c16e931f75 to your computer and use it in GitHub Desktop.
Save thcipriani/3b67432310ac6bfba26f01c16e931f75 to your computer and use it in GitHub Desktop.
I keep this function sourced from my .bashrc—it is incredibly useful.
spell() {
# /me futilely avoids googling words to find their correct spelling
local candidates oldifs word array_pos
oldifs="$IFS"
IFS=':'
# Parse the apsell format and return a list of ":" separated words
read -a candidates <<< "$(printf "%s\n" "$1" \
| aspell -a \
| awk -F':' '/^&/ {
split($2, a, ",")
result=""
for (x in a) {
gsub(/^[ \t]/, "", a[x])
result = a[x] ":" result
}
gsub(/:$/, "", result)
print result
}')"
# Reverse number and print the parsed bash array because the list comes
# out of gawk backwards
for item in "${candidates[@]}"; do
printf '%s\n' "$item"
done \
| tac \
| nl \
| less -FirSX
printf "[ $(tput setaf 2)?$(tput sgr0) ]\t%s" \
'Enter the choice (empty to cancel, 0 for input): '
read index
[[ -z "$index" ]] && return
[[ "$index" == 0 ]] && word="$1"
[[ -z "$word" ]] && {
array_pos=$(( ${#candidates[@]} - index ))
word="${candidates[$array_pos]}"
}
[[ -n "$word" ]] && {
printf "$word" | xsel -p
printf "Copied '%s' to clipboard!\n" "$word"
} || printf "[ $(tput setaf 1):($(tput sgr0) ] %s\n" 'No match found'
IFS="$oldifs"
}
@thcipriani
Copy link
Author

spell demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment