web-search.plugin.zsh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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"
  35. rm nohup.out
  36. }
  37. alias bing='web_search bing'
  38. alias google='web_search google'
  39. alias yahoo='web_search yahoo'
  40. alias ddg='web_search duckduckgo'
  41. #add your own !bang searches here
  42. alias wiki='web_search duckduckgo \!w'
  43. alias news='web_search duckduckgo \!n'
  44. alias youtube='web_search duckduckgo \!yt'
  45. alias map='web_search duckduckgo \!m'
  46. alias image='web_search duckduckgo \!i'
  47. alias ducky='web_search duckduckgo \!'