web-search.plugin.zsh 1.6 KB

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