web-search.plugin.zsh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # web_search from terminal
  2. function web_search() {
  3. emulate -L zsh
  4. # define search engine URLS
  5. typeset -A urls
  6. urls=(
  7. google "https://www.google.com/search?q="
  8. bing "https://www.bing.com/search?q="
  9. yahoo "https://search.yahoo.com/search?p="
  10. duckduckgo "https://www.duckduckgo.com/?q="
  11. yandex "https://yandex.ru/yandsearch?text="
  12. github "https://github.com/search?q="
  13. )
  14. # define the open command
  15. case "$OSTYPE" in
  16. darwin*) open_cmd="open" ;;
  17. cygwin*) open_cmd="cygstart" ;;
  18. linux*) open_cmd="xdg-open" ;;
  19. *) echo "Platform $OSTYPE not supported"
  20. return 1
  21. ;;
  22. esac
  23. # check whether the search engine is supported
  24. if [[ -z "$urls[$1]" ]]; then
  25. echo "Search engine $1 not supported."
  26. return 1
  27. fi
  28. # search or go to main page depending on number of arguments passed
  29. if [[ $# -gt 1 ]]; then
  30. # build search url:
  31. # join arguments passed with '+', then append to search engine URL
  32. url="${urls[$1]}${(j:+:)@[2,-1]}"
  33. else
  34. # build main page url:
  35. # split by '/', then rejoin protocol (1) and domain (2) parts with '//'
  36. url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
  37. fi
  38. nohup $open_cmd "$url" &>/dev/null
  39. }
  40. alias bing='web_search bing'
  41. alias google='web_search google'
  42. alias yahoo='web_search yahoo'
  43. alias ddg='web_search duckduckgo'
  44. alias yandex='web_search yandex'
  45. alias github='web_search github'
  46. #add your own !bang searches here
  47. alias wiki='web_search duckduckgo \!w'
  48. alias news='web_search duckduckgo \!n'
  49. alias youtube='web_search duckduckgo \!yt'
  50. alias map='web_search duckduckgo \!m'
  51. alias image='web_search duckduckgo \!i'
  52. alias ducky='web_search duckduckgo \!'