My Dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
2.6 KiB

  1. # This file provides my current word completion
  2. # Unlike standard completion provided by zsh, this completion is based on the current word rather than the current command
  3. # Prompts the user to select a wordlist from the wordlists folder
  4. wordlistSelect() {
  5. fd -a --type f --hidden --follow --color=always --exclude .git --exclude \*.md --exclude \*.gif --exclude \*.jpg --exclude \*.png --exclude \*.lua --exclude \*.jar --exclude \*.pl '' /usr/share/wordlists/ | fzf --preview 'bat --color=always {}'
  6. }
  7. word_replace(){
  8. local ret=1
  9. local word="$1"
  10. local cmd="$2"
  11. case "$word" in
  12. wl) wordlistSelect; return 0 ;;
  13. myip) ip route | grep -oE '(dev|src) [^ ]+' | sed 'N;s/\n/,/;s/src //;s/dev //' | awk -F',' '{print $2 " " $1}' | sort -u | fzf -1 --no-preview | cut -d' ' -f1; return 0 ;;
  14. esac
  15. return "$ret"
  16. }
  17. currentwordcomplete(){
  18. local tokens cmd swap ret=1 lastWord
  19. # http://zsh.sourceforge.net/FAQ/zshfaq03.html
  20. # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
  21. # Use zsh's shell parsing to split lbuffer into items
  22. # This takes into account quoting and escaping
  23. tokens=(${(z)LBUFFER})
  24. # If there isn't anythng typed, fall back to old tab binding
  25. if [ ${#tokens} -lt 1 ]; then
  26. zle ${currentword_default_completion:-expand-or-complete}
  27. return
  28. fi
  29. # Assume the first element
  30. # TODO: make this work for multiple commands chained with | or > or && etc.
  31. # TODO: make this work when command prepended with variables eg a=2 foo bar
  32. # foo is the command name
  33. cmd="${tokens[1]}"
  34. lastWord="${tokens[-1]}"
  35. # Check we haven't pushed space
  36. if [ "${LBUFFER[-1]}" != " " ]; then
  37. swap="$(word_replace "$lastWord" "$cmd")"
  38. ret="$?"
  39. # This part checks if the part after an = is completable
  40. if [ "$ret" -ne "0" ]; then
  41. local afterEqual="${lastWord##*=}"
  42. local beforeEqual="${lastWord%=*}"
  43. # If they are different, there is an equals in the word
  44. if [ "$afterEqual" != "$lastWord" ]; then
  45. swap="${beforeEqual}=$(word_replace "$afterEqual" "$cmd")"
  46. ret="$?"
  47. fi
  48. fi
  49. fi
  50. if [ "$ret" -eq "0" ]; then
  51. if [ -n "$swap" ]; then
  52. tokens[-1]="$swap"
  53. LBUFFER="${tokens[@]}"
  54. fi
  55. zle reset-prompt
  56. return 0
  57. else
  58. zle ${currentword_default_completion:-expand-or-complete}
  59. return
  60. fi
  61. }
  62. # Record what ctrl+i is currently set to
  63. # That way we can call it if jhcompletion doesn't result in anything
  64. [ -z "$currentword_default_completion" ] && {
  65. binding=$(bindkey '^I')
  66. [[ $binding =~ 'undefined-key' ]] || currentword_default_completion=$binding[(s: :w)2]
  67. unset binding
  68. }
  69. zle -N currentwordcomplete
  70. bindkey '^I' currentwordcomplete