web-search.plugin.zsh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # web_search from terminal
  2. function web_search() {
  3. # get the open command
  4. local open_cmd
  5. if [[ "$OSTYPE" = darwin* ]]; then
  6. open_cmd='open'
  7. else
  8. open_cmd='xdg-open'
  9. fi
  10. # check whether the search engine is supported
  11. if [[ ! $1 =~ '(google|bing|yahoo|duckduckgo)' ]];
  12. then
  13. echo "Search engine $1 not supported."
  14. return 1
  15. fi
  16. local url="http://www.$1.com"
  17. # no keyword provided, simply open the search engine homepage
  18. if [[ $# -le 1 ]]; then
  19. $open_cmd "$url"
  20. return
  21. fi
  22. if [[ $1 == 'duckduckgo' ]]; then
  23. #slightly different search syntax for DDG
  24. url="${url}/?q="
  25. else
  26. url="${url}/search?q="
  27. fi
  28. shift # shift out $1
  29. while [[ $# -gt 0 ]]; do
  30. url="${url}$1+"
  31. shift
  32. done
  33. url="${url%?}" # remove the last '+'
  34. nohup $open_cmd "$url" >/dev/null 2&>1
  35. }
  36. alias bing='web_search bing'
  37. alias google='web_search google'
  38. alias yahoo='web_search yahoo'
  39. alias ddg='web_search duckduckgo'
  40. #add your own !bang searches here
  41. alias wiki='web_search duckduckgo \!w'
  42. alias news='web_search duckduckgo \!n'
  43. alias youtube='web_search duckduckgo \!yt'
  44. alias map='web_search duckduckgo \!m'
  45. alias image='web_search duckduckgo \!i'
  46. alias ducky='web_search duckduckgo \!'